按钮“closebutton”没有响应。我是java的新手,我知道错误与嵌套if中的actionpreformed有关。我已经设置了一个测试,以查看按下关闭按钮时是否会打印控制台。他们没有。任何帮助表示赞赏
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myGuiTester implements ActionListener
{
JPanel pane = new JPanel();
JPanel mainPanel = new JPanel();
JFrame MenuFrame = new JFrame("Main Menu");
JFrame Instructions = new JFrame("Instructions");
JFrame game = new JFrame("Game");
JPanel gamepan = new JPanel();
JFrame inst = new JFrame("Instructions");
JPanel instpan = new JPanel(new FlowLayout());
JButton playButton = new JButton("Play");
JButton instButton = new JButton("Instructions");
JButton exitButton = new JButton("Exit");
JButton closebutton = new JButton("Close");
public static void main(String[] args)
{
new myGuiTester ();
}
public myGuiTester ()
{
MenuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inst.setVisible(false);
game.setVisible(false);
JLabel title = new JLabel(" Slot Machine");
JLabel blank = new JLabel("");
JLabel blank1 = new JLabel("");
JLabel blank2 = new JLabel("");
JLabel blank3 = new JLabel("");
playButton.addActionListener(this);
instButton.addActionListener(this);
JLabel game1 = new JLabel("Test");
JLabel inst1 = new JLabel();
exitButton.addActionListener(this);
closebutton.addActionListener(this);
pane.setLayout(new GridLayout(9,1));
pane.add(blank);
pane.add(title);
pane.add(blank1);
pane.add(playButton);
pane.add(blank2);
pane.add(instButton);
pane.add(blank3);
pane.add(exitButton);
mainPanel.add(pane);
MenuFrame.setContentPane(mainPanel);
MenuFrame.setSize(500,400);
MenuFrame.setVisible(true);
//Game Panel
game.setContentPane(gamepan);
game.setSize(500,400);
game1.setText("Game goes here");
gamepan.add(game1);
//Instructions Panel
inst.setContentPane(instpan);
inst.setSize(500,300);
inst1.setText("Instructions go here.");
instpan.add(inst1);
instpan.add(closebutton);
}
//ActionListener
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals("Play"))
{
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);
MenuFrame.setVisible(false);
System.out.print("test play");
}
else if (e.getActionCommand().equals("Instructions"))
{
inst.setVisible(true);
//suspected error
if (e.getActionCommand().equals("Close"))
{
System.out.print("Test inst");
inst.setVisible(false);
}
}
else if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
}
答案 0 :(得分:0)
你必须在elseif语句中处理“Close”,但不能在“Instructions”中处理。我应该像它自己的块一样:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Play")) {
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setVisible(true);
MenuFrame.setVisible(false);
System.out.print("test play");
} else if (e.getActionCommand().equals("Instructions")) {
inst.setVisible(true);
// suspected error
}else if (e.getActionCommand().equals("Close")) {
System.out.print("Test inst");
inst.setVisible(false);
} else if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
答案 1 :(得分:0)
当您检查调用哪个按钮时,您正在将事件的ActionCommand与按钮的标签进行比较,这不是同一个事情:
e.getActionCommand().equals("Close")
你应该做的是检查按钮是否是事件的来源
e.getSource() == closeButton