简体   繁体   中英

Java Swing text boxes and buttons not showing in the form?

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();
    }
}
  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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM