Results 1 to 3 of 3
Thread: GuessGame error
- 07-29-2008, 03:50 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 9
- Rep Power
- 0
GuessGame error
I am getting this error but I cant figure out where it is, please take a look at the code, thanks.
Error:
C:\Users\Documents\Test.java:49: cannot find symbol
symbol : class GuessHandler
location: class Test
guessInputJTextField.addActionListener( new GuessHandler() );
^
1 error
Tool completed with exit code 1
Code:
//Guess.java
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class Guess extends JFrame
{
int guessOld = 0;
private int number; // number chosen by application
private JTextField guessInputJTextField; // for guessing
private JLabel prompt1JLabel; // first prompt to user
private JLabel prompt2JLabel; // second prompt to user
private JLabel messageJLabel; // displays message of game status
private JLabel random1;
private JButton newGameJButton; // creates new game
private Color background; // background color of application
// set up GUI and initialize values
public Guess()
{
super( "Guessing Game" );
setLayout(new FlowLayout());
background = Color.LIGHT_GRAY; // set background to light gray
prompt1JLabel = new JLabel( "I have a number between 1 and 1000." ); // describe game
add(prompt1JLabel);
prompt2JLabel = new JLabel( "Can you guess my number? Enter your Guess:" ); // prompt user
add(prompt2JLabel);
guessInputJTextField = new JTextField( 5 ); // to enter guesses
guessInputJTextField.addActionListener( new GuessHandler() );
//messageJLabel = new JLabel( "Guess result appears here." );
add(guessInputJTextField);
messageJLabel = new JLabel( "" );
add(messageJLabel);
newGameJButton = new JButton( "New Game" ); // button with text
add ( newGameJButton ); // add newGame button to JFrame
Random generator = new Random();
int number = generator.nextInt(1001);
random1 = new JLabel( " " + number);
add( random1 );
newGameJButton.addActionListener(
new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent e )
{
guessInputJTextField.setText("");
Random generator = new Random();
int number = generator.nextInt(1001);
random1.setText("" + number );
SwingUtilities.updateComponentTreeUI(random1);
messageJLabel.setText("");
guessInputJTextField.setEditable(true);
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
theGame(); // start new game
} // end GuessGameFrame constructor
// choose a new random number
public void theGame()
{
} // end method theGame
// change background color
public void paint( Graphics g )
{
super.paint( g );
getContentPane().setBackground( background ); // set background
} // end method paint
// react to new guess
//public void react( int guess )
public void actionPerformed( ActionEvent e )
{
int guess;
guess = Integer.parseInt(guessInputJTextField.getText());
number = Integer.parseInt((random1.getText()).trim());
if ( Math.abs( number - guess ) < Math.abs( number - guessOld) ){
// Hotter
getContentPane().setBackground(Color.RED);
}
else{
// Colder
getContentPane().setBackground(Color.BLUE);
}
guessOld = guess;
if ( guess >= number )
{
messageJLabel.setText( "Too High." );
SwingUtilities.updateComponentTreeUI(messageJLabel );
}
if( guess <= number )
{
messageJLabel.setText( "Too Low." );
SwingUtilities.updateComponentTreeUI(messageJLabel );
} // end if
if ( guess < number + 1 && guess > number-1 ) // guess is too low
{
messageJLabel.setText( "Correct!" );
SwingUtilities.updateComponentTreeUI(messageJLabel );
guessInputJTextField.setEditable(false);
}
// react to new guess
class GuessHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
int guess;
guess = Integer.parseInt(guessInputJTextField.getText());
number = Integer.parseInt((random1.getText()).trim());
if ( Math.abs( number - guess ) < Math.abs( number - guessOld) )
{
// Hotter
getContentPane().setBackground(Color.RED);
}
else
{
// Colder
getContentPane().setBackground(Color.BLUE);
}
guessOld = guess;
if ( guess >= number )
{
messageJLabel.setText( "Too High." );
SwingUtilities.updateComponentTreeUI(messageJLabel );
}
if( guess <= number )
{
messageJLabel.setText( "Too Low." );
SwingUtilities.updateComponentTreeUI(messageJLabel );
} // end if
if ( guess < number + 1 && guess > number-1 ) // guess is too low
{
messageJLabel.setText( "Correct!" );
SwingUtilities.updateComponentTreeUI(messageJLabel );
guessInputJTextField.setEditable(false);
}
}
}
}
}
--------------------------
To test the code:
//GuessTest.java
import javax.swing.JFrame;
public class GuessTest
{
public static void main(String args[]) throws Exception
{
GuessTest guessgame = new GuessTest();
guessgame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
guessgame.setSize(550, 150);
guessgame.setVisible(true);
}
}
- 07-29-2008, 06:51 AM #2
See the two comments in this next code block.Java Code:C:\jexp>javac guessrx.java guessrx.java:47: cannot find symbol symbol : class GuessHandler location: class GuessRx guessInputJTextField.addActionListener( new GuessHandler() ); ^ 1 error
Change all of the above to this:Java Code:// The enclosing class does not implement the ActionListener // interface so this method will not be called. public void actionPerformed( ActionEvent e ) { int guess; guess = Integer.parseInt(guessInputJTextField.getText()); number = Integer.parseInt((random1.getText()).trim()); if ( Math.abs( number - guess ) < Math.abs( number - guessOld) ){ // Hotter getContentPane().setBackground(Color.RED); } else{ // Colder getContentPane().setBackground(Color.BLUE); } guessOld = guess; if ( guess >= number ) { messageJLabel.setText( "Too High." ); SwingUtilities.updateComponentTreeUI(messageJLabel ); } if( guess <= number ) { messageJLabel.setText( "Too Low." ); SwingUtilities.updateComponentTreeUI(messageJLabel ); } // end if if ( guess < number + 1 && guess > number-1 ) // guess is too low { messageJLabel.setText( "Correct!" ); SwingUtilities.updateComponentTreeUI(messageJLabel ); guessInputJTextField.setEditable(false); } // A class appearing inside the actionPerformed method. // This hides the class and is why the compiler could // not find the symbol. class GuessHandler implements ActionListener { public void actionPerformed( ActionEvent e ) { int guess = Integer.parseInt(guessInputJTextField.getText()); number = Integer.parseInt((random1.getText()).trim()); if(Math.abs(number - guess) < Math.abs(number - guessOld)) { // Hotter getContentPane().setBackground(Color.RED); } else { // Colder getContentPane().setBackground(Color.BLUE); } guessOld = guess; if ( guess >= number ) { messageJLabel.setText( "Too High." ); SwingUtilities.updateComponentTreeUI(messageJLabel ); } if( guess <= number ) { messageJLabel.setText( "Too Low." ); SwingUtilities.updateComponentTreeUI(messageJLabel ); } // end if if ( guess < number + 1 && guess > number-1 ) { messageJLabel.setText( "Correct!" ); SwingUtilities.updateComponentTreeUI(messageJLabel ); guessInputJTextField.setEditable(false); } } } }
Java Code:class GuessHandler implements ActionListener { public void actionPerformed( ActionEvent e ) { int guess = Integer.parseInt(guessInputJTextField.getText()); number = Integer.parseInt((random1.getText()).trim()); if(Math.abs(number - guess) < Math.abs(number - guessOld)) { // Hotter getContentPane().setBackground(Color.RED); } else { // Colder getContentPane().setBackground(Color.BLUE); } guessOld = guess; if ( guess >= number ) { messageJLabel.setText( "Too High." ); SwingUtilities.updateComponentTreeUI(messageJLabel ); } if( guess <= number ) { messageJLabel.setText( "Too Low." ); SwingUtilities.updateComponentTreeUI(messageJLabel ); } // end if if ( guess < number + 1 && guess > number-1 ) { messageJLabel.setText( "Correct!" ); SwingUtilities.updateComponentTreeUI(messageJLabel ); guessInputJTextField.setEditable(false); } } }
- 07-29-2008, 10:49 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
error 530 error authentication required
By rgale in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-12-2008, 04:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks