我正在開發一個GUI,我決定要構建不帶佈局的GUI。相反,我使用setBounds()函數。當我只放置一個按鈕時,它一切正常,但是當我放置另一個按鈕時,他正在擴展並填充洞屏幕。我仍然可以單擊TextField,但無法看到它(當我點擊某個位置時它顯示出來)。這裏是我的代碼:無佈局的按鈕擴展
//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);
}
的錯誤是沒有使用佈局管理與設計,與他們使用的框架。這些組件具有默認的佈局管理器,如果您不考慮該佈局管理器會導致衝突。真的,使用絕對位置的唯一原因是自我教導自己是什麼大錯誤。 – kiheru
一旦你完成了向容器中添加組件的操作,總是會在最後調用像'pack()/ setVisible()'這樣的方式,以更好的方式實現它的大小。 –