【发布时间】:2014-05-24 11:46:28
【问题描述】:
我做了很多研究并尝试了很多东西,但我的目标是这样做:我目前有三个按钮。对于按钮 1 - 播放按钮,我想单击它,结果是删除屏幕上的所有按钮。
这是我现在的代码
public class Menu extends JFrame implements ActionListener {
//Variables here
public void createUI() {
frame = new JFrame();
frame.setTitle("Maze Game");
frame.setSize(500, 500);
frame.setVisible(true);
panel.setLayout(new GridLayout(3, 1, 0, 19));
panel.setVisible(true);
JButton b = new JButton("Play");
b.setSize(20, 10);
b.setBackground(new Color(59, 89, 182));
b.setForeground(Color.WHITE);
b.setFocusPainted(false);
b.setFont(new Font("Tahoma", Font.BOLD, 12));
panel.add(b);
JButton b0 = new JButton("Help");
b0.setBackground(new Color(59, 89, 182));
b0.setForeground(Color.WHITE);
b0.setFocusPainted(false);
b0.setFont(new Font("Tahoma", Font.BOLD, 12));
b0.addActionListener(this);
panel.add(b0);
JButton b1 = new JButton("Quit");
b1.setBackground(new Color(59, 89, 182));
b1.setForeground(Color.WHITE);
b1.setFocusPainted(false);
b1.setFont(new Font("Tahoma", Font.BOLD, 12));
b1.addActionListener(this);
panel.add(b1);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
现在我的问题是:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Quit")) {
frame.dispose();
} else if (e.getActionCommand().equals("Help")) {
JFrame j1 = new JFrame();
j1.setSize(200, 200);
j1.setVisible(true);
} else if (e.getActionCommand().equals("Play")) {
//panel.removeAll();
//revalidate();
//repaint();
//frame.getContentPane().remove(panel);
}
}
我想要它,所以当我单击“播放”按钮时,它会删除该按钮以及“帮助”按钮和“退出”按钮。
我已经尝试了注释的代码位,但没有成功。希望我已经把事情说清楚了。重申一下,我想在点击播放后删除所有按钮(基本上是一个空白窗口)。
【问题讨论】:
-
是的,我知道它是重复的,但我已经尝试过提到的那些事情。除非我错过了什么。
-
您是否真的将 ActionListener 添加到“播放”按钮? (根据您发布的代码,答案是否定的)此外,从面板中添加/删除组件的标准代码是:
panel.removeAll(); panel.revalidate(); panel.repaint(); -
“我已经尝试过提到的那些事情” 您的代码中没有提到卡片布局,这是解决此类问题的正确方法。
标签: java swing user-interface