此问题已经在这里有了答案:
我在设计程序时,用户按下按钮,它打开一个新框架,然后从那里他们可以执行许多任务,例如更改密码。
public class customerPanel extends JFrame {
private JPanel Panel = new JPanel(new GridLayout(0,2,0,20));
public customerPanel(User userInput, UserManagement managementDatabase) {
//Menu buttons
JButton passwordFrame = new JButton("Change Password");
//action buttons
JButton getName = new JButton("Show your full name");
JButton changePassword = new JButton("Change your password");
JButton updatefullName = new JButton("Update your full name");
JButton LogOut = new JButton("Log out");
JTextField oldPassword = new JTextField();
JTextField newPassword = new JTextField();
JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JLabel nameLabel = new JLabel("");
JLabel oldpasswordLabel = new JLabel("Old password");
JLabel newpasswordLabel = new JLabel("New password");
JLabel firstnameLabel = new JLabel("First name");
JLabel lastnameLabel = new JLabel("Last name");
JFrame passwordChange = new JFrame("Change password");
JFrame updateDetails = new JFrame("Update fullname");
LogOut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame mainFrame = (JFrame) SwingUtilities.getWindowAncestor(Panel);
Panel.setVisible(false);
userLogin newLogin = new userLogin(managementDatabase);
mainFrame.add(newLogin.getPanel());
}
});
getName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent clicked) {
JOptionPane.showMessageDialog(null, "Name of User is " + managementDatabase.getUserFullNameByUserName(userInput.getUserName()), "Full name", HEIGHT);
}
});
passwordFrame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent clicked) {
passwordChange.setVisible(true);
passwordChange.setSize(500, 500);
}
});
changePassword.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent clicked) {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
boolean result = (managementDatabase.changePassword(userInput.getUserName(), oldPassword.getText(), newPassword.getText()));
if (result == true) {
JOptionPane.showMessageDialog(null, "Password Sucessfully Channged");
} else {
JOptionPane.showMessageDialog(null, "Passowrd cannot be changed ", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
});
Panel.add(getName);
Panel.add(nameLabel);
Panel.add(passwordFrame);
Panel.add(new JLabel());
passwordChange.add(oldpasswordLabel);
passwordChange.add(newpasswordLabel);
passwordChange.add(oldPassword);
passwordChange.add(newPassword);
passwordChange.add(changePassword);
Panel.add(updatefullName);
Panel.add(new JLabel());
Panel.add(firstnameLabel);
Panel.add(lastnameLabel);
Panel.add(firstName);
Panel.add(lastName);
Panel.add(LogOut);
Panel.setVisible(true);
}
public JPanel getPanel() {
return Panel;
}
public String getFullName(User input) {
return input.getFullName();
}
}
目前,在将元素添加到passwordChange JFrame时遇到问题。当我添加按钮时(显示在代码的底部),changePassword按钮将占用整个JFrame空间(显示在此处https://i.imgur.com/x5nC1kl.png)
如何解决此问题,以便可以更改并确定尺寸和位置?谢谢(请不要将此标记为重复的ive尝试阅读其他解决方案,但我听不懂)(PS是我问题的唯一相关代码)。
有很多方法可以解决此问题,其中一些最简单的方法涉及布局技巧。如果使用BorderLayout
,则可以根据您希望按钮出现的位置将按钮向北,向南,向左或向西挂勾。分别为上,下,右或左。然后在中间添加一个空的JLabel
。这将使按钮仅出现在一个角上。
如果使用GridLayout
,则可以使用相同的逻辑。将按钮放在所需的网格段中,然后用虚拟标签或面板填充其余的段。