我有一个问题,当我尝试按下按钮级别1时,我的整个gui会冻结。我的主要问题是设置一个现在为空的新面板。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Guibach extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel mainPanel;
private JPanel choosingPanel;
private JPanel x;
private JButton exit;
private JButton but1;
private JButton but2;
private JButton but3;
private JButton but4;
private JButton but5;
private JButton but6;
private JButton but7;
private JButton but8;
private JButton but9;
private JButton but10;
public Guibach() {
this.setSize(900, 900);
this.setLayout(null);
mainPanel = new JPanel();
mainPanel.setSize(900, 900);
mainPanel.setBackground(Color.GRAY);
mainPanel.setLayout(null);
x = new JPanel();
x.setSize(900, 900);
x.setBackground(Color.RED);
x.setLayout(new BorderLayout());
choosingPanel = new JPanel();
choosingPanel.setSize(900, 450);
choosingPanel.setBackground(Color.BLUE);
choosingPanel.setLayout(new FlowLayout());
but1 = new JButton();
but1.setText("Level 1");
but1.setSize(10, 5);
but1.setActionCommand("but1");
but1.addActionListener(this);
choosingPanel.add(but1);
but2 = new JButton();
but2.setText("Level 2");
but2.setActionCommand("but2");
but2.addActionListener(this);
choosingPanel.add(but2);
but3 = new JButton();
but3.setText("Level 3");
but3.setActionCommand("but3");
but3.addActionListener(this);
choosingPanel.add(but3);
but4 = new JButton();
but4.setText("Level 4");
but4.setActionCommand("but4");
but4.addActionListener(this);
choosingPanel.add(but4);
but5 = new JButton();
but5.setText("Level 5");
but5.setActionCommand("but5");
but5.addActionListener(this);
choosingPanel.add(but5);
but6 = new JButton();
but6.setText("Level 6");
but6.setActionCommand("but6");
but6.addActionListener(this);
choosingPanel.add(but6);
but7 = new JButton();
but7.setText("Level 7");
but7.setActionCommand("but7");
but7.addActionListener(this);
choosingPanel.add(but7);
but8 = new JButton();
but8.setText("Level 8");
but8.setActionCommand("but8");
but8.addActionListener(this);
choosingPanel.add(but8);
but9 = new JButton();
but9.setText("Level 9");
but9.setActionCommand("but9");
but9.addActionListener(this);
choosingPanel.add(but9);
but10 = new JButton();
but10.setText("Level 10");
but10.setActionCommand("but10");
but10.addActionListener(this);
choosingPanel.add(but10);
exit = new JButton();
exit.setText("Exit");
exit.setActionCommand("exit");
exit.addActionListener(this);
choosingPanel.add(exit);
mainPanel.add(choosingPanel);
this.add(mainPanel);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("but1")) {
choosingPanel.setVisible(false);
mainPanel.removeAll();
getContentPane().removeAll();
getContentPane().add(x);
x.setVisible(true);
revalidate();
}
if (e.getActionCommand().equals("but2")) {
}
if (e.getActionCommand().equals("but3")) {
}
if (e.getActionCommand().equals("but4")) {
}
if (e.getActionCommand().equals("but5")) {
}
if (e.getActionCommand().equals("but6")) {
}
if (e.getActionCommand().equals("but7")) {
}
if (e.getActionCommand().equals("but8")) {
}
if (e.getActionCommand().equals("but9")) {
}
if (e.getActionCommand().equals("but10")) {
}
if (e.getActionCommand().equals("exit")) {
WindowDestroyer myListener = new WindowDestroyer();
this.addWindowListener(myListener);
System.exit(0);
}
}
public static void main(String[] args) {
new Guibach();
}
}
我需要清除所有内容并在我的代码中放置一个新的空面板。