Button expanding with no layout

86 Views Asked by At

I'm working on a GUI and i decided that I want to build the GUI with no layout. Instead, I'm using the setBounds() function. When I'm placing only one button it's all work fine, but when i place the other button, he's expanding and fills the hole screen.I can still click on the TextField but I can't see it (when I click on certain place it shows up). Here is my code:

//Graphical part
private JFrame loginFrame;
private JTextField userField;
private JButton send;
private JButton reg;
private JTextField passField;
public void graphics() {
    setRegister();
    int sizeX=120,sizeY=20,bSizeX=80,bSizeY=sizeY;
    int locationX=80,locationY=40;
    loginFrame=new JFrame("Login");
    loginFrame.setVisible(true);
    loginFrame.setSize(300,200);
    userField=new JTextField("");
    passField=new JTextField("");
    loginFrame.add(userField);
    loginFrame.add(passField);
    userField.setBounds(locationX, locationY, sizeX, sizeY);
    passField.setBounds(locationX, locationY+10+sizeY, sizeX, sizeY);
    send=new JButton("Send");
    send.addActionListener(this);
    loginFrame.add(send);
    send.setBounds(locationX+40,10+2*(locationY+10), bSizeX, bSizeY);
    reg=new JButton("Register");
    reg.setBounds(0, 0, bSizeX, bSizeY);
    loginFrame.add(reg);
    //reg.addActionListener(this);
}
1

There are 1 best solutions below

1
On

By default JFrames have BorderLayout as layout manager, so your code really does use it. If you want to test your code without layout, you need to specify it as null:

 loginFrame.setLayout(null);