【问题标题】:Can't get Java Swing GUI Guessing Game to work [duplicate]无法让 Java Swing GUI 猜谜游戏工作 [重复]
【发布时间】:2016-05-18 13:44:50
【问题描述】:

我正在做关于 Java Swing 的作业,并且正在制作一个 GUI 猜谜游戏程序。我的问题是当我按下“猜测”按钮时没有任何反应,我无法通过单击 X 来关闭它,我必须用 Eclipse 终止它。我做错什么了? GuessHandler 是正确实现 ActionListener 还是我在那里做错了什么?

public class GuessingGameGui extends JFrame
{
    public static final int WIDTH = 600;
    public static final int HEIGHT = 400;
    private JTextField theText;
    private JLabel message;
    private JPanel p1;
    private JPanel p2;
    private int guess;
    private int numberOfTries = 0;

    public GuessingGameGui()
    {
        super();
        setSize(WIDTH, HEIGHT);
        //set the window title to "Guessing Game"
        setTitle("Guessing Game");
        Container c = getContentPane();
        c.setLayout(new BorderLayout( ));
        c.setBackground(Color.WHITE);

        p1 = new JPanel();
        p2 = new JPanel();

        p1.setBackground(Color.WHITE);
        p2.setBackground(Color.BLUE);

        //"add a JButton called "Guess"
        JButton guessButton = new JButton("Guess");
        GuessHandler ghandler = new GuessHandler();
        guessButton.addActionListener(ghandler);
        p1.add(guessButton);

        //The north panel will have a JLabel with the text "Guess a number between 1 and 10?"
        JLabel label1 = new JLabel("Guess a number between 1 and 10?");
        c.add(label1, BorderLayout.NORTH);

        //The south panel will have a JLabel for displaying if the user guessed correctly or not 
        message = new JLabel("");
        p2.add(message, BorderLayout.SOUTH);
        c.add(p2, BorderLayout.SOUTH);


        JPanel textPanel = new JPanel( );
        textPanel.setBackground(Color.LIGHT_GRAY);

        //In the center panel, add a JTextField where the user can enter a number to guess
        theText = new JTextField(10);
        theText.setBackground(Color.WHITE);
        textPanel.add(theText);
        textPanel.add(p1);
        c.add(textPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args)
    {
        GuessingGameGui guessGame = new GuessingGameGui();
        guessGame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        guessGame.setSize(WIDTH, HEIGHT);
        guessGame.setVisible(true);
    }

    class GuessHandler implements ActionListener {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            int numberToGuess = (int) (Math.random() * 10 + 1);
            Scanner input = new Scanner (System.in);
            boolean win = false;
            while (win == false){

                guess = input.nextInt();
                numberOfTries++;
                if (guess < 1 || guess > 10)
                {
                    //Make the south panel background color RED if they entered an invalid number (not between 1 and 10)
                    p2.setBackground(Color.RED);
                }
                else if (guess == numberToGuess)
                {
                    win = true;
                    //Make the south panel background color YELLOW if they guessed right
                    p2.setBackground(Color.YELLOW);
                    //and display "YOU GOT IT (n attempts)", where n is the number of attempts the user guessed
                    message.setText("YOU GOT IT (" +numberOfTries + "attempts)");
                }
                else
                {
                    //Make the south panel background color GREY if they guessed wrong
                    p2.setBackground(Color.GRAY);
                    //display "Sorry try again"
                    message.setText("Sorry try again");
                }
            }
        }
    }
}

【问题讨论】:

  • 由于您的程序被锁定,我认为您的动作处理程序中有一个无限循环,因此 win 永远不会设置为 true。此外,您从控制台而不是您想要的 JTextfield 获取输入

标签: java swing button action


【解决方案1】:

您的程序中有几个问题会导致您的问题:

  1. 您的方法 actionPerformed 中不应包含 while 循环,否则您的 Swing 应用程序将冻结。
  2. 您不需要Scanner,因为猜测是从您的文本字段中提取的值,因此guess = input.nextInt() 应该是guess = Integer.parseInt(theText.getText())。事实上,否则您的应用程序将冻结,直到 standard input stream 中提供 integer,这不是您所期望的。

【讨论】:

  • 就是这样。这两点都会杀死您的应用程序:while 循环将无限期地继续,而无法响应新的输入。并且扫描器将永远阻塞等待控制台输入。
【解决方案2】:

始终将其添加到您的框架中会很有用:

 frame.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
 frame.addWindowListener( new WindowAdapter() {
    @Override
    public void windowClosing( final WindowEvent e ) {
        handleExitRequest();
    }
 } );

然后创建一个方法 handleExitRequest() 来执行实际的关闭(可能只是调用 System.exit(0);) 如果有的话,您还可以从“退出”菜单项的 actionPerformed() 调用该方法。这允许您在退出之前清理应该完成的任何事情(例如提交或回滚任何数据库操作等)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多