Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFrame在尝试用另一个面板替换面板时崩溃_Java_Swing - Fatal编程技术网

Java 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

我有一个JFrame,其初始化如下:

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()
。与移除和添加面板相比,这是首选方法