simple interface

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



 
stevenglassAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InSaNiECommented:
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
InSaNiECommented:
this.getContentPane().add(pane);

    the above should be -

this.getContentPane().setContentPane(pane);

sorry about that....

0
InSaNiECommented:
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
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

stevenglassAuthor Commented:
cannot get it to compile properly using KAWA with JDK 1.3
its full off errors!
0
sanjay_thakurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
raid999Commented:
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
girionisCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.