0
因此,現在我試圖創建一個基本的登錄GUI。我想看起來像這樣我想創建一個很好的JFrame登錄面板
UserID Password
<EDITABLE AREA FOR USERID> <EDITABLE AREA FOR Password>
"Error Msg displayed here if there exists error, otherwise NO SPACE"
[Login Button]
下面是我目前正在嘗試使用的代碼來實現此目的。我明白目前沒有任何功能,我將在稍後添加監聽器並提供功能。我目前的問題是,間距似乎已關閉,我不知道如何改善它。
public class Login extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1023608189590961526L;
JTextField userName, passWord, userNameLabel, passWordLabel;
JTextField errorMsg;
JButton login;
JPanel userNamePanel, passWordPanel, errorMsgPanel, loginPanel;
public Login(String Title){
super(Title);//Set Frame Title
login = new JButton("Login");
initTextFields();
setLayout(new GridBagLayout());
userNamePanel = new JPanel(new GridBagLayout());
passWordPanel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
constraints.weighty = 1;
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridx = 0;
constraints.gridy = 0;
userNamePanel.add(userNameLabel, constraints);
passWordPanel.add(passWordLabel, constraints);
constraints.gridy++;
userNamePanel.add(userName, constraints);
passWordPanel.add(passWord, constraints);
constraints.gridwidth = GridBagConstraints.RELATIVE;
constraints.fill = GridBagConstraints.NONE;
this.add(userNamePanel, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridx = 2;
this.add(passWordPanel, constraints);
constraints.gridy++;
constraints.gridy++;
constraints.gridx--;
this.add(login, constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridx = 0;
constraints.gridy--;
this.add(errorMsg, constraints);
errorMsg.setVisible(false);
}
private boolean initTextFields(){
userName = new JTextField(20);
passWord = new JTextField(20);
userNameLabel = new JTextField("UserID", 20);
userNameLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder());
userNameLabel.setEditable(false);
userNameLabel.setHorizontalAlignment(JTextField.CENTER);
passWordLabel = new JTextField("Password", 20);
passWordLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder());
passWordLabel.setEditable(false);
passWordLabel.setHorizontalAlignment(JTextField.CENTER);
errorMsg = new JTextField("", 50);
errorMsg.setBorder(javax.swing.BorderFactory.createEmptyBorder());
errorMsg.setEditable(false);
errorMsg.setForeground(Color.red);
passWordLabel.setHorizontalAlignment(JTextField.CENTER);
return true;
}
}
我在我的主要方法的初始化這個如下:
Login testLog = new Login("Login GUI");
testLog.setSize(525, 200);// Sets the Frame Size
testLog.setMinimumSize(new Dimension(525,200));
testLog.setResizable(false);//Makes the size fixed
testLog.setLocationRelativeTo(null); // Make the Frame appear centered
testLog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Tells program to exit when the 'x'(close) button is pressed
testLog.pack();
testLog.setVisible(true);
的代碼工作,只要你想。 prb在哪裏? – CMPS
這不是,登錄按鈕的頂部和密碼和用戶名字段的底部之間仍有很大的空間 –
爲了更快地獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve) (最小完整和可驗證示例)。 –