【发布时间】:2012-01-10 22:53:00
【问题描述】:
我在最后才提出这个问题,因为我无法就我在这个随机数游戏中遇到的问题找到任何帮助。
问题是当我尝试运行程序时,ActionListener 中出现错误,提示“guessResult 无法解析”。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class Project8 extends JFrame
{
private JTextField numGuessFld;
private JButton guessBtn, guessAgainBtn, playAgainBtn;
private int randNum;
private Container c = getContentPane();
private int numGuessed;
private Random rn = new Random();
public static void main(String[] args)
{
Project8 frm = new Project8();
frm.setSize(400,150);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//end main
public Project8()
{
c.setLayout(new FlowLayout());
setTitle("Number Guessing Game");
JLabel gameDescriptLbl = new JLabel("I have a number between 1 and 1000. Can you guess my number?");
c.add(gameDescriptLbl);
JLabel numGuessLbl = new JLabel("Please enter your first guess. ");
c.add(numGuessLbl);
numGuessFld = new JTextField(4);
c.add(numGuessFld);
guessBtn = new JButton("Guess");
c.add(guessBtn);
guessAgainBtn = new JButton("Guess Again");
guessAgainBtn.setEnabled(false);
c.add(guessAgainBtn);
playAgainBtn = new JButton("Play Again?");
playAgainBtn.setEnabled(false);
c.add(playAgainBtn);
JLabel guessResult = new JLabel("");
c.add(guessResult);
Handler handler = new Handler();
numGuessFld.addActionListener(handler);
guessBtn.addActionListener(handler);
guessAgainBtn.addActionListener(handler);
playAgainBtn.addActionListener(handler);
randNum = rn.nextInt(1000)+1;
}//end constructor Project8
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == guessBtn)
{
String input = numGuessFld.getText();
numGuessed = Integer.parseInt(input);
System.out.println(numGuessed);
System.out.println(randNum);
if (numGuessed < randNum)
{
c.setBackground(Color.blue);
guessResult.setText("Too low!");
guessAgainBtn.setEnabled(true);
}
else if (numGuessed > randNum)
{
c.setBackground(Color.red);
guessResult.setText("Too high!");
guessAgainBtn.setEnabled(true);
}
else if (numGuessed == randNum)
{
c.setBackground(Color.yellow);
guessResult.setText("Correct!");
guessAgainBtn.setEnabled(false);
guessBtn.setEnabled(false);
numGuessFld.setBackground(Color.green);
numGuessFld.setEditable(false);
playAgainBtn.setEnabled(true);
}
}//end guessBtn if
if (e.getSource() == guessAgainBtn)
{
numGuessFld.setText("");
}//end guessAgainBtn if
if (e.getSource() == playAgainBtn)
{
c.setBackground(null);
numGuessFld.setText("");
guessAgainBtn.setEnabled(false);
guessBtn.setEnabled(true);
numGuessFld.setBackground(null);
numGuessFld.setEditable(true);
playAgainBtn.setEnabled(false);
randNum = rn.nextInt(1000)+1;
}//end playAgainBtn if
}//end ActionPerformed
}//end Handler
}//end Project8
任何帮助将不胜感激...尤其是如果它在 4:30 之前到达:) 谢谢!
【问题讨论】:
-
似乎您正在使用一些简单的文本编辑器。如果您使用 Eclipse、Netbeans 或 IntelliJ IDEA 等 IDE,它们可以为您提供很多帮助,因为它们能够在您运行代码之前突出显示代码中的此类错误并提供一些有用的提示。
-
@hage 我正在使用 Eclipse,这就是我在运行它之前遇到问题的地方。还是)感谢你的建议!我的“教授”(不是一个很好的教授恕我直言)让我们在课堂上使用 JGrasp,但我在我的闪存驱动器上将 eclipse 作为便携式应用程序,所以我在其中编写代码并为了他的缘故复制粘贴到 JGrasp 中。
-
尤其是如果它在 4:30 之前到达 - 哈哈,作业截止日期在哪个时区?
标签: java swing user-interface scope jlabel