Java JFrame在尝试用另一个面板替换面板时崩溃
我有一个JFrame,其初始化如下:Java JFrame在尝试用另一个面板替换面板时崩溃,java,swing,Java,Swing,我有一个JFrame,其初始化如下: setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setPreferredSize(PREFERRED_SIZE); setTitle("Game Set"); setLayout(new BorderLayout()); background = new JLabel(new ImageIcon("images/gameSetBackground.jpg")); background.set
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(PREFERRED_SIZE);
setTitle("Game Set");
setLayout(new BorderLayout());
background = new JLabel(new ImageIcon("images/gameSetBackground.jpg"));
background.setPreferredSize(PREFERRED_SIZE);
add(background);
loginPane = new LoginPane();
background.setLayout(new GridBagLayout());
loginPane.setOpaque(false);
background.add(loginPane, constraints);
pack();
setVisible(true);
public class LoginPane extends JPanel {
JLabel label = new JLabel("Login");
JLabel secondLabel = new JLabel("If you don't have a profile, click below to create one!");
JTextField userName;
JPasswordField password;
public LoginPane() {
setLayout(new GridBagLayout());
userName = new JTextField(20);
password = new JPasswordField(20);
constraints.insets = new Insets(2, 2, 2, 2);
constraints.gridx = 0;
constraints.gridy = 0;
add(label, constraints);
// etc add all of them the same way
constraints.gridy = 3;
add(secondLabel, constraints);
userName.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
password.requestFocus();
}
});
password.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (userName.getText().equals("Mike")) {
}
}
});
}
}
我这样做是因为它允许我将背景设置为我指定的jpg。现在我有了loginPane()类,如下所示:
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(PREFERRED_SIZE);
setTitle("Game Set");
setLayout(new BorderLayout());
background = new JLabel(new ImageIcon("images/gameSetBackground.jpg"));
background.setPreferredSize(PREFERRED_SIZE);
add(background);
loginPane = new LoginPane();
background.setLayout(new GridBagLayout());
loginPane.setOpaque(false);
background.add(loginPane, constraints);
pack();
setVisible(true);
public class LoginPane extends JPanel {
JLabel label = new JLabel("Login");
JLabel secondLabel = new JLabel("If you don't have a profile, click below to create one!");
JTextField userName;
JPasswordField password;
public LoginPane() {
setLayout(new GridBagLayout());
userName = new JTextField(20);
password = new JPasswordField(20);
constraints.insets = new Insets(2, 2, 2, 2);
constraints.gridx = 0;
constraints.gridy = 0;
add(label, constraints);
// etc add all of them the same way
constraints.gridy = 3;
add(secondLabel, constraints);
userName.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
password.requestFocus();
}
});
password.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (userName.getText().equals("Mike")) {
}
}
});
}
}
我想做的是,当我在输入用户名和密码后按enter键时,我会检查这是否是有效的用户名/密码对。之后,我想删除这里的所有内容,并显示一个不同的JPanel
。在password.ActionListener中,我尝试了以下操作:
loginPane.removeAll();
background.removeAll();
这是两种不同的情况,但都会导致JFrame崩溃,文本字段没有响应,我必须退出。我做错了什么
“JFrame在尝试用另一个面板替换面板时崩溃”
简单/正确的解决方案是使用卡片布局
在面板之间切换。请参阅中的详细信息,并查看一个简单的示例
CardLayout
具有如下方法:按名称显示某个面板的show()
、显示下一个面板的next()
和显示上一个面板的previous()
。与移除和添加面板相比,这是首选方法
“JFrame在尝试用另一个面板替换面板时崩溃”
简单/正确的解决方案是使用卡片布局
在面板之间切换。请参阅中的详细信息,并查看一个简单的示例
CardLayout
具有如下方法:按名称显示某个面板的show()
、显示下一个面板的next()
和显示上一个面板的previous()
。与移除和添加面板相比,这是首选方法
“JFrame在尝试用另一个面板替换面板时崩溃”
简单/正确的解决方案是使用卡片布局
在面板之间切换。请参阅中的详细信息,并查看一个简单的示例
CardLayout
具有如下方法:按名称显示某个面板的show()
、显示下一个面板的next()
和显示上一个面板的previous()
。与移除和添加面板相比,这是首选方法
“JFrame在尝试用另一个面板替换面板时崩溃”
简单/正确的解决方案是使用卡片布局
在面板之间切换。请参阅中的详细信息,并查看一个简单的示例
CardLayout
具有如下方法:按名称显示某个面板的show()
、显示下一个面板的next()
和显示上一个面板的previous()
。与移除和添加面板相比,这是首选方法