Hi this isnt wrking i need help!!!!
import javax.swing.*;
import java.awt.event.*;
import java.awt.*; //used for layout manager
import java.util.Random;;
class GuessGame extends JFrame
{
private JButton newButton;
private JTextField gField; //stands for guess field
private JLabel firstPrompt, enterLabel, userMessageLabel;
private int randomNumber, userGuess;
private int counter = 0;
private int lastGuess = 0;
public GuessGame()
{
super("Guessing Game");
setLayout(new FlowLayout());
firstPrompt = new JLabel("I have a number between 1 and 1000 can you guess");
enterLabel = new JLabel("Please enter your first guess.");
userMessageLabel = new JLabel("");
//need to have ranom number
randomNumber = new Random().nextInt(1000) + 1;
setBackground(Color.WHITE);
newButton = new JButton("New Game");
newButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
setBackground(Color.WHITE);
userMessageLabel.setText("");
randomNumber = new Random().nextInt(1000) + 1;
}
}
);
setSize( 600, 250 );
setVisible(true);
randomNumber = new Random().nextInt(1000)+1;
gField = new JTextField(4); //setting it to four because the guessed number should$
gField.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event)
{
counter++;
userGuess = Integer.parseInt( gField.getText());
if (userGuess == randomNumber)
{
userMessageLabel.setText("You are correct, it took you: " + counter $
setBackground(Color.GREEN);
}
else if (userGuess > randomNumber)
{
userMessageLabel.setText("Too high");
}
else if (userGuess < randomNumber)
{
userMessageLabel.setText("Too Low");
}
if (counter > 1)
{
if ((randomNumber - userGuess) > (randomNumber - lastGuess))
{
setBackground(Color.RED);
}
else if ((randomNumber - userGuess) < (randomNumber - lastGuess))
{
setBackground(Color.BLUE);
}
}
else
{
setBackground(Color.WHITE);
}
lastGuess = userGuess;
}
}
);
Container c = getContentPane();
c.setLayout( new FlowLayout());
c.add(newButton);
c.add(gField);
c.add(firstPrompt);
c.add(enterLabel);
c.add(userMessageLabel);
repaint();
}
public static void main(String[] args)
{
GuessGame myGuessGame = new GuessGame(); //instantiate a GUI object
}
}
it is attached!!!!