Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 程序似乎没有运行,也没有标记任何错误_Java_Swing - Fatal编程技术网

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

Java 程序似乎没有运行,也没有标记任何错误,java,swing,Java,Swing,当试图在Netbeans中编译一个“猜数字”程序时,该程序似乎一直在运行,但程序没有执行。当用户单击“Play”JButton时,playGame方法应该运行,但似乎什么也没有发生,也没有引发异常 代码如下。 谢谢 西亚兰 /* *第11章GUI程序问题 * 11.15 */ 包第11章图形用户界面; 导入java.awt.BorderLayout; 导入java.awt.FlowLayout; 导入java.awt.event.ActionListener; 导入java.awt.Active

当试图在Netbeans中编译一个“猜数字”程序时,该程序似乎一直在运行,但程序没有执行。当用户单击“Play”JButton时,playGame方法应该运行,但似乎什么也没有发生,也没有引发异常

代码如下。 谢谢 西亚兰

/*
*第11章GUI程序问题
* 11.15
*/
包第11章图形用户界面;
导入java.awt.BorderLayout;
导入java.awt.FlowLayout;
导入java.awt.event.ActionListener;
导入java.awt.ActiveEvent;
导入javax.swing.JButton;
导入javax.swing.JPanel;
导入javax.swing.JTextArea;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.event.ActionEvent;
导入javax.swing.JLabel;
导入javax.swing.JTextField;
导入javax.swing.JFrame;
导入javax.swing.ImageIcon;
导入javax.swing.Icon;
导入javax.swing.SwingConstants;
导入java.util.Random;
公共类猜谜游戏扩展JFrame{
//实例变量/文件
私人JLabel简介;
私人JLabel highorlow;
私人JButton游戏;
私有JTextField输入应答;
私人内部回答1;
私人内部回答2;
私有图像图标icon1;
私有图像图标icon2;
私人JPanel小组;
专用随机数发生器;
私人回答;
//构造函数将创建一个JFrame GUI并注册事件处理程序
公众猜谜游戏{
超级(“猜谜游戏”);
setLayout(新的BorderLayout());
图标icon1=新的图像图标(getClass().getResource(“bug1.gif”);
Icon icon2=新的图像图标(getClass().getResource(“travelbug.gif”);
//获取用户输入的JtextField
enterAnswer=新的JTextField(5);
//enterAnswer.addActionListener(处理程序);
输入response.setText(“猜测”);
输入response.setEditable(false);
//JLabel以显示标题
简介=新的JLabel(“按播放开始…”),icon1,SwingConstants.CENTER;
highorlow=新的JLabel();
//使用flowlayout管理器将组件放置在面板上,以仅保存文本aea
panel=新的JPanel();
面板。添加(输入答案);
面板。添加(高或低);
//JButton再次播放,并使用anoumonus类调用gameMethod
play=新的JButton(“play”);
添加(面板、边框布局、中心);
添加(简介,BorderLayout.NORTH);
添加(播放,边界布局。南部);
play.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
//如果点击按钮,则设置艺术游戏
play.setText(“享受”);
游戏();
//抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}//结束行动
});//从addAcionlistener方法结束annmous类ActionListener调用
}//端盖
//一种玩游戏的方法
公共游戏{
//在1和100之间生成randown编号
numberGenerator=新随机数();
correctAnswer=numberGenerator.nextInt(100);
面板。背景(颜色为洋红色);
回答1=0;
回答2=0;
输入response.setEditable(true);
输入response.setText(“”);
而((回答1!=正确回答)和&(回答2!=正确回答)){
setText(“我有一个数字是1和一个100。”
+“你能猜出我的号码吗?”);
输入response.setEditable(true);
enterAnswer.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
如果(如getSource()==enterAnswer){
answer1=Integer.parseInt(enterAnswer.getText());
}
//将背景设置为第一次猜测时的红色
面板.立根背景(颜色.红色);
//不,是定得太高还是太低
如果(回答1>正确回答){
highorlow.setText(“猜得太高”);
高或低设置图标(icon2);
}否则如果(回答1<正确回答){
setText(“你喷得太低了”);
高或低设置图标(icon2);
}//end esle if语句
}//结束行动
});//结束ActionListener
//得到答案2
intro.setText(“重试…”);
enterAnswer.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
如果(如getSource()==enterAnswer){
answer2=Integer.parseInt(enterAnswer.getText());
}
//检查是热的还是冷的
//第二个答案比第一个答案更接近,所以变红
如果((数学abs(回答2-正确答案))>(数学abs(回答2-正确答案))){
面板.立根背景(颜色.红色);
}否则{
面板.立根背景(颜色.蓝色);
}
//现在加太多太多太少
//不,是定得太高还是太低
如果(回答2>正确回答){
highorlow.setText(“猜得太高”);
高或低设置图标(icon2);
}否则如果(回答2<正确回答){
setText(“你喷得太低了”);
高或低设置图标(icon2);
}//end esle if语句
}//已执行的结束操作
});//结束ActionLIstener
}//答案正确时结束循环
//如果答案正确怎么办
面板.立根背景(橙色);
enterAnsw
 /*
 * 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
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