2

I am a newbie in java. I am working on a code. It is a swing piece for form. I have created the form but my text boxes and buttons are not present when I run my code. Please help me with this problem. Thanks

    import java.awt.*;
     class Test1
    {
    Frame fr;
    Button b1,b2,b3;
    TextField tf1,tf2,tf3,tf4,tf5,tf6,tf7;
    Label lb1,lb2,lb3,lb4,lb5,lb6,lb7;
    Test1()
    {
    fr=new Frame("Student Form");
    fr.setLayout(null);

        lb1=new Label("Name");
        lb2=new Label("Address");
        lb3=new Label("Course");
        lb4=new Label("Phone");
        lb5=new Label("Gmail");
        lb6=new Label("Pincode");
        lb7=new Label("State");
        tf1=new TextField();
        tf2=new TextField();
        tf3=new TextField();
        tf4=new TextField();
        tf5=new TextField();
        tf6=new TextField();
        tf7=new TextField();

        b1=new Button("Submit");
        b2=new Button("Reset");
        b3=new Button("Cancel");


        lb1.setBounds(30,50,100,50);
        lb2.setBounds(30,120,100,50);
        lb3.setBounds(30,190,100,50);
        lb4.setBounds(30,260,100,50);
        lb5.setBounds(30,330,100,50);
        lb6.setBounds(300,260,100,50);
        lb7.setBounds(300,330,100,50);
        tf1.setBounds(150,50,100,50);
        tf2.setBounds(150,120,100,50);
        tf3.setBounds(150,190,100,50);
        tf4.setBounds(150,260,100,50);
        tf5.setBounds(150,330,100,50);
        tf6.setBounds(450,260,100,50);
        tf7.setBounds(450,330,100,50);
        b1.setBounds(30,500,100,50);
        b2.setBounds(150,500,100,50);
        b3.setBounds(300,500,100,50);

    fr.setSize(700,700);
    fr.setVisible(true);
    }

    public static void main(String s[])
    {
        new Test1();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    You will need to add your components to your frame with `fr.add(lb1);` and so on – pzaenger Sep 24 '14 at 15:40
  • @pzaenger do i need to add each and every component ? – Vanessa Hall Sep 24 '14 at 15:45
  • Yes, you will need to do so. The frame itself doesn't know which components are part of it without adding these components. – pzaenger Sep 24 '14 at 15:50
  • Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 24 '14 at 16:38
  • As an aside, that code is not Swing, but AWT. Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Sep 24 '14 at 16:39

2 Answers2

2

You have not added your button and text boxes to the form. Please add lines code in your code.

fr.add(tf1);fr.add(tf2);fr.add(tf3);fr.add(tf4);fr.add(tf5);fr.add(tf6);fr.add(tf7);fr.add(lb1);fr.add(lb2);fr.add(lb3);fr.add(lb4);fr.add(lb5);fr.add(lb6);fr.add(lb7);fr.add(b1);fr.add(b2);fr.add(b3);

Add this piece of code after b3.setBounds(300,500,100,50);

Thanks

Mohit
  • 1,185
  • 2
  • 11
  • 25
2
  1. Do not call setBounds() method ever. You could use setXXXSize() only (where XXX - minimum, preferred, maximum).
  2. Use LayoutManager to arrange your components inside container.
  3. You should add the components to container using add(Component) method.
SeniorJD
  • 6,946
  • 4
  • 36
  • 53