0

尝试在 Netbeans 中编译“猜数字”程序时,该程序似乎继续运行,但程序没有执行。当用户单击“播放”JButton 时,playGame 方法应该运行,但似乎什么也没有发生,也没有抛出异常。

代码如下。谢谢夏兰

 /*
 * chpter 11 GUI programmong questions
 * 11.15
 */
package Chapter11GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.ActiveEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.SwingConstants;
import java.util.Random;

public class GuessGame extends JFrame {
    //instance varibles/files

    private JLabel intro;
    private JLabel highorlow;
    private JButton play;
    private JTextField enterAnswer;
    private int answer1;
    private int answer2;
    private ImageIcon icon1;
    private ImageIcon icon2;
    private JPanel panel;
    private Random numberGenerator;
    private int correctAnswer;

    //constructor willll create GUI o a JFrame and register event handlers
    public GuessGame() {
        super("GuessGAme");
        setLayout(new BorderLayout());

        Icon icon1 = new ImageIcon(getClass().getResource("bug1.gif"));
        Icon icon2 = new ImageIcon(getClass().getResource("travelbug.gif"));

        //JtextField to get user input
        enterAnswer = new JTextField(5);
        //enterAnswer.addActionListener(handler);
        enterAnswer.setText("Guess");
        enterAnswer.setEditable(false);

        //JLabel to display heading
        intro = new JLabel("Press Play to begin....", icon1, SwingConstants.CENTER);
        highorlow = new JLabel();

        //place componments on a panel useing flowlayout manager to hold text aea only

        panel = new JPanel();
        panel.add(enterAnswer);
        panel.add(highorlow);


        //JButton to play again and use anoumonus class to call gameMethod
        play = new JButton("Play");
        add(panel, BorderLayout.CENTER);
        add(intro, BorderLayout.NORTH);
        add(play, BorderLayout.SOUTH);

        play.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //if button clicked then setart game

                play.setText("Enjoy");
                playGame();

                //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }//end actionPerfomred
        });//end annmous class ActionListener call from addAcionlistener method

    }//end construtor

    //a method to play the game
    public void playGame() {

        //generate randown number bewteen 1 and 100
        numberGenerator = new Random();
        correctAnswer = numberGenerator.nextInt(100);
        panel.setBackground(Color.magenta);
        answer1 = 0;
        answer2 = 0;
        enterAnswer.setEditable(true);
        enterAnswer.setText("");

        while ((answer1 != correctAnswer) && (answer2 != correctAnswer)) {

            intro.setText("I have a number bewteen 1 and a 100."
                    + "Can you guess my number?");
            enterAnswer.setEditable(true);
            enterAnswer.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == enterAnswer) {
                        answer1 = Integer.parseInt(enterAnswer.getText());
                    }




                    //set background as red as it is first guess
                    panel.setBackground(Color.red);

                    //noe set wether too high or two low
                    if (answer1 > correctAnswer) {
                        highorlow.setText("Guess Too High");
                        highorlow.setIcon(icon2);

                    } else if (answer1 < correctAnswer) {
                        highorlow.setText("You are gussing too low");
                        highorlow.setIcon(icon2);

                    }//end esle if statement
                }//end actionPerfomred
            });//end ActionListener

            //get answer number 2
            intro.setText("Try Again...");
            enterAnswer.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == enterAnswer) {
                        answer2 = Integer.parseInt(enterAnswer.getText());
                    }



                    //check if gettingb hot or cold
                    //2nd answer is closer than first so turn red
                    if ((Math.abs(answer2 - correctAnswer)) > (Math.abs(answer2 -              correctAnswer))) {
                        panel.setBackground(Color.red);
                    } else {
                        panel.setBackground(Color.BLUE);
                    }
                    //now add too hihg ot tewo low
                    //noe set wether too high or two low
                    if (answer2 > correctAnswer) {
                        highorlow.setText("Guess Too High");
                        highorlow.setIcon(icon2);

                    } else if (answer2 < correctAnswer) {
                        highorlow.setText("You are gussing too low");
                        highorlow.setIcon(icon2);

                    }//end esle if statement

                }//end actionPerformed
            });//end ActionLIstener

        }//end while loop when answer is correct
        //what to do if answer is correct
        panel.setBackground(Color.orange);
        enterAnswer.setText("Correct!!");
        enterAnswer.setEditable(false);
        play.setText("PLay Again?");




    }//end method playGame
}//end class

/*
 *chapter 11
 * porgramming question 11.14
 */
package Chapter11GUI;

import javax.swing.JFrame;

public class GuessGameTest {


    public static void main(String[] args) {
        // instanistae frame form GuessGAme
        GuessGame frame = new GuessGame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(350, 350);
    }//end main
}//end clas test

谢谢。删除了 while 循环,程序现在正在运行,但不是我想要的。为了解决我正在使用 netbeans 调试器的问题,我遇到的问题是我似乎无法进入 playGame() 方法来查看到底出了什么问题。已尝试在匿名 ActionListener 内部类关联中使用步骤 (F7) 与 JButton 来访问 playGame(),并且我已将光标指向 playGame 的开头,然后运行到光标命令。在这两种情况下,调试器都会返回到测试应用程序主类。希望这是有道理的...

已尝试在 playGame() 处插入断点但无法访问。这可能与 playGame() 方法不是 java 主类的一部分有关吗?当有问题的类被实例化时,我似乎只能从主类访问类的构造函数.....

4

2 回答 2

3

您的程序实际上正在编译并且实际上似乎也在运行,但看起来您正在用while (true)循环捆绑 Swing 事件线程。

public void playGame() {

    //generate randown number bewteen 1 and 100
    numberGenerator = new Random();

    // .... etc...

    // ***** this will lock your GUI!! *******
    while ((answer1 != correctAnswer) && (answer2 != correctAnswer)) {

        intro.setText("I have a number bewteen 1 and a 100."
                + "Can you guess my number?");
        // ....
    }

}//end method playGame

当您这样做时,while 循环将继续在 Swing 事件线程(或事件调度线程或 edt)上运行,从而阻止 edt 与用户交互。这将导致程序完全没有响应。

解决方案,不要这样做。为您的项目使用事件驱动的编程代码而不是线性控制台程序代码。让您的程序根据 answer1 和 answer2 字段的状态更改其行为,但不要在 while 循环中执行此操作。换句话说,根据这些和其他类字段的状态,让 ActionListener 的 actionPerformed 方法表现不同。Swing 事件循环将是您需要的所有游戏循环。

于 2013-05-08T21:57:28.493 回答
0

好的,通过在调试器中使用 GUI 快照通过 JButton actionListener 匿名类访问该方法。

再次感谢您的帮助。C

于 2013-05-11T18:06:15.703 回答