gpt4 book ai didi

java - 程序似乎没有运行并且没有标记任何错误

转载 作者:行者123 更新时间:2023-12-01 23:49:53 26 4
gpt4 key购买 nike

尝试在 Netbeans 中编译“猜数字”程序时,该程序似乎继续运行,但该程序并未执行。当用户单击“Play”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 主类的一部分有关吗?我似乎只能在实例化相关类时从主类访问类的构造函数......

最佳答案

您的程序实际上正在编译,并且似乎也在运行,但看起来您正在使用 while (true) 绑定(bind) 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 事件线程(或事件分派(dispatch)线程或 edt)上运行,从而阻止 edt 与用户交互。这将导致程序完全无响应。

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

关于java - 程序似乎没有运行并且没有标记任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16450874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com