程序似乎没有运行,也没有标记任何错误

时间:2013-05-08 21:55:22

标签: java swing

在尝试编译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()方法来查看到底出了什么问题。尝试在与JButton关联的匿名ActionListener内部类中使用步骤(F7)来访问playGame(),并且我已将光标指向playGame的开头,然后指向Run To Cursor命令。在这两个实例中,调试器都返回到测试应用程序主类。希望有意义......

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

2 个答案:

答案 0 :(得分: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事件循环将是您需要的所有游戏循环。

答案 1 :(得分:0)

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

再次感谢您的帮助。 ç