我正在编写一个程序,其中有一个带有 JPanel 和登录 gui 的 JFrame。登录成功后,调用一个清除JFrame的方法。我遇到的问题是,JPanel 中的东西仍然可见,而不是清除 JFrame,它只是被冻结了。
创建登录 gui 的方法:
public void logingui() {
JPanel loginpanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
JLabel gamename = new JLabel("InvestGame By Ama291");
gamename.setFont(new Font("Arial", 1, 22));
loginpanel.add(gamename, c);
c.ipady = 20;
c.gridx = 0;
c.gridy = 1;
loginpanel.add(new JPanel(), c);
c.ipady = 10;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 2;
loginpanel.add(new JLabel("Username: "), c);
c.gridx = 1;
final JTextField userfield = new JTextField(10);
loginpanel.add(userfield, c);
c.gridx = 0;
c.gridy = 3;
loginpanel.add(new JPanel(), c);
c.gridx = 0;
c.gridy = 4;
loginpanel.add(new JLabel("Password: "), c);
c.gridx = 1;
final JPasswordField passfield = new JPasswordField(10);
loginpanel.add(passfield, c);
c.gridx = 0;
c.gridy = 5;
c.ipady = 20;
loginpanel.add(new JPanel(), c);
c.ipady = 10;
c.gridx = 0;
c.gridy = 6;
c.gridwidth = 1;
JButton createacc = new JButton("Create Account");
loginpanel.add(createacc, c);
c.gridx = 1;
JButton login = new JButton("Log In");
loginpanel.add(login, c);
add(loginpanel);
}
清除JFrame的方法:
public void gamegui() {
JPanel gamepanel = new JPanel();
removeAll();
invalidate();
validate();
repaint();
add(gamepanel);
}
有谁知道我怎样才能成功地从 JFrame 中删除 JPanel 而不会将 JPanel 中的按钮和东西冻结在窗口内?这是我遇到的问题。