?
Solved

simple interface

Posted on 2002-05-08
8
Medium Priority
?
216 Views
Last Modified: 2010-03-31
i am quite new to java programming and would like to know how i could make a simple interface, GUI  

Program title

 Label 1      textBox 1    Button 1  
 Label 2      textBox 2    Button 2  
 Label 3      textBox 3    Button 3

                           Button 4



 
0
Comment
Question by:stevenglass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Expert Comment

by:InSaNiE
ID: 6996117
class simpleGui extends JFrame{
  JLabel label1 = new JLabel("Label1");
  JLabel label2 = new JLabel("Label2");
  JLabel label3 = new JLabel("Label3");
  JTextField field1 = new JTextField();
  JTextField field2 = new JTextField();
  JTextField field3 = new JTextField();
  JButton button1 = new JButton("button1");
  JButton button2 = new JButton("button2");
  JButton button3 = new JButton("button3");
  JButton button4 = new JButton("button4");


  GridBaglayout gridbag = new GridBagLaout();
  GridBagConstraints c = new GridBagConstraints();
JPanel pane = new JPanel();

  public void constrain(Components c,int x,int y,int w,int h,int wx,int wy){
       c.gridx = x;
       c.gridy =y;
       c.gridwidth = w;
       c.gridheight = h;
       c.weightx = wx;
       c.weighty = wy;
 }

     public simpleGui(){
         super("Program title");
         this.setSize(200,100);
     
     pane.setLayout(gridBag);
     constrain(label1,0,0,1,1,33,25);
     pane.getLayout().setConstraints(label1,c);
     pane.add(label1);
     constrain(label1,0,1,1,1,33,25);
     pane.getLayout().setConstraints(label2,c);
     pane.add(label2);
     constrain(label1,0,2,1,1,33,25);
     pane.getLayout().setConstraints(label3,c);
     pane.add(label3);
     constrain(field1,1,0,1,1,33,25);
     pane.getLayout().setConstraints(field1,c);
     pane.add(label3);
     constrain(field2,1,1,1,1,33,25);
     pane.getLayout().setConstraints(field2,c);
     pane.add(label3);
     constrain(field3,1,2,1,1,33,25);
     pane.getLayout().setConstraints(field3,c);
     pane.add(label3);
     constrain(button1,2,0,1,1,33,25);
     pane.getLayout().setConstraints(button1,c);
     pane.add(button1);
     constrain(button2,2,1,1,1,33,25);
     pane.getLayout().setConstraints(button2,c);
     pane.add(button2);
     constrain(button3,2,2,1,1,33,25);
     pane.getLayout().setConstraints(button3,c);
     pane.add(button3);
     constrain(button4,3,3,1,1,100,25);
     pane.getLayout().setConstraints(button4,c);
     pane.add(button4);

    this.getContentPane().add(pane);
 
     }

public static void main(String arg[]){
       simpleGui s = new simpleGui();
       s.show();
}
}

try this - I dont' have java on this machine so i can't test it. You will also have to see what classes to import.
If you have any questions ask - I will eventually answer them.
0
 

Expert Comment

by:InSaNiE
ID: 6996129
this.getContentPane().add(pane);

    the above should be -

this.getContentPane().setContentPane(pane);

sorry about that....

0
 

Expert Comment

by:InSaNiE
ID: 6996364
public void constrain(Components c,int x,int y,int w,int h,int wx,int wy){

should be..

public void constrain(Component c,int x,int y,int w,int h,int wx,int wy){

- no s on the end of Component.
     
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:stevenglass
ID: 6997104
cannot get it to compile properly using KAWA with JDK 1.3
its full off errors!
0
 
LVL 4

Accepted Solution

by:
sanjay_thakur earned 800 total points
ID: 6997406
Hi,

This is a tested code

import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JPanel;
import javax.swing.JLayeredPane;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

public class Test   {

    protected JFrame JFrame2 ;
    protected JRootPane JRootPane4 ;
    protected JPanel glassPane6 ;
    protected JLayeredPane JLayeredPane6 ;
    protected JPanel contentPane6 ;
    protected JPanel JPanel3 ;
    protected JLabel JLabel7 ;
    protected JLabel JLabel8 ;
    protected JLabel JLabel9 ;
    protected JButton JButton7 ;
    protected JButton JButton8 ;
    protected JButton JButton9 ;
    protected JTextField JTextField7 ;
    protected JTextField JTextField8 ;
    protected JTextField JTextField9 ;
    protected JPanel JPanel4 ;
    protected JButton JButton10 ;

    public  Test(){
        super();

        JFrame2 = new JFrame();
        JRootPane4 = JFrame2.getRootPane();
        glassPane6 = (JPanel)JRootPane4.getGlassPane();
        JLayeredPane6 = JRootPane4.getLayeredPane();
        contentPane6 = (JPanel)JRootPane4.getContentPane();
        JPanel3 = new JPanel();
        JLabel7 = new JLabel();
        JLabel8 = new JLabel();
        JLabel9 = new JLabel();
        JButton7 = new JButton();
        JButton8 = new JButton();
        JButton9 = new JButton();
        JTextField7 = new JTextField();
        JTextField8 = new JTextField();
        JTextField9 = new JTextField();
        JTextField9.setText("This is just a demo");
        JPanel4 = new JPanel();
        JButton10 = new JButton();
        JFrame2.setTitle( "JFrame2" );
        contentPane6.setLayout( new java.awt.BorderLayout() );
        JPanel3.setLayout( new java.awt.GridBagLayout() );
        JLabel7.setText( "JLabel7" );
        JLabel8.setText( "JLabel8" );
        JLabel9.setText( "JLabel9" );
        JButton7.setText( "JButton7" );
        JButton8.setText( "JButton8" );
        JButton9.setText( "JButton9" );
        {
            GridBagConstraints grid = new GridBagConstraints();
            grid.insets = new Insets(5,12,5,12);
            JPanel3.add(JLabel7, grid, -1);
            grid.gridx = 0;
            grid.gridy = 1;
            JPanel3.add(JLabel8, grid, -1);
            grid.gridy = 2;
            JPanel3.add(JLabel9, grid, -1);
            grid.gridx = 2;
            grid.gridy = 0;
            JPanel3.add(JButton7, grid, -1);
            grid.gridy = 1;
            JPanel3.add(JButton8, grid, -1);
            grid.gridy = 2;
            JPanel3.add(JButton9, grid, -1);
            grid.fill = GridBagConstraints.HORIZONTAL;
            grid.gridx = 1;
            grid.gridy = 1;
            grid.weightx = 1.0;
            JPanel3.add(JTextField7, grid, -1);
            grid.gridy = 0;
            JPanel3.add(JTextField8, grid, -1);
            grid.gridy = 2;
            JPanel3.add(JTextField9, grid, -1);
        }
        JPanel4.setLayout( new java.awt.GridBagLayout() );
        JButton10.setText( "JButton10" );
        {
            GridBagConstraints grid = new GridBagConstraints();
            grid.anchor = GridBagConstraints.SOUTHEAST;
            JPanel4.add(JButton10, grid, -1);
        }
        {
            String strConstraint;
            strConstraint = "Center";
            contentPane6.add(JPanel3, strConstraint, -1);
            strConstraint = "South";
            contentPane6.add(JPanel4, strConstraint, -1);
        }
        JFrame2.pack();
        JFrame2.show();

    }


    public static void main(String args[])
    {
      Test obj = new Test();
      obj.JFrame2.setSize(400,200);

    }


}

Also I think you should refer to various layout managers
used in Java

As in the example above the GridBagLayout
may look bit complex initially but it is the most flexible
one once you know how to use it


hope this helps
0
 
LVL 1

Expert Comment

by:raid999
ID: 6997686
Well these codes are really nice, but i remamber when i was new in JAVA i wanted something fast an easy if that what you are looking for you can try Forte from Sun it is a program were you can drag and drop items and learn wile doing that.

------------------------
Cheers
0
 
LVL 35

Expert Comment

by:girionis
ID: 8740738
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to sanjay_thakur

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 8 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question