newGameJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { guessInputJTextField.setText(""); Random generator = new Random(); number = generator.nextInt(1001); random1.setText(""+ number); SwingUtilities.updateComponentTreeUI(random1); messageJLabel.setText(""); guessInputJTextField.setEditable(true); Guesses =0; message2JLabel.setText("NUMBER OF GUESSES: "+ Guesses); Guesses++; } } ); theGame(); } public void theGame() { } class GuessHandler implements ActionListener { public void actionPerformed(ActionEvent e) { int Guess; Guess = Integer.parseInt(guessInputJTextField.getText()); if (Math.abs(number - Guess) < Math.abs(number - GuessOld)) { getContentPane().setBackground(Color.RED); } else { 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); } if (Guess == number) { messageJLabel.setText("CORRECT! "); getContentPane().setBackground(Color.YELLOW); SwingUtilities.updateComponentTreeUI(messageJLabel); guessInputJTextField.setEditable(false); } message2JLabel.setText("NUMBER OF GUESSES: " + Guesses++); } } } GuessGame.java import javax.swing.JFrame; public class GuessGame { public static void main(String args[]) throws Exception { Gameframe guessgame = new Gameframe(); guessgame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); guessgame.setSize(550, 150); guessgame.setVisible(true); } }
Need Help Java Code Error
SEE IMAGE FOR ERROR
JAVA CODES:
Gameframe.java
import java.awt.Color;
import java.awt.FlowLayout;
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 Gameframe extends JFrame{
private int Guesses = 1;
private int GuessOld =0;
private int number;
private JTextField guessInputJTextField;
private JLabel prompt1JLabel;
private JLabel prompt2JLabel;
private JLabel messageJLabel;
private JLabel message2JLabel;
private JLabel random1 = new JLabel("");
private JButton newGameJButton;
private Color background;
public Gameframe()
{
super("Guessing Game");
setLayout(new FlowLayout());
background = Color.LIGHT_GRAY;
prompt1JLabel = new JLabel("\nI HAVE A NUMBER BETWEEN 1 AND 100.");
add(prompt1JLabel);
prompt1JLabel = new JLabel("\nCAN YOU GUESS MY NUMBER? ENTER YOUR GUESS:");
add(prompt2JLabel);
guessInputJTextField = new JTextField(5);
guessInputJTextField.addActionListener(new GuessHandler());
add(guessInputJTextField);
messageJLabel = new JLabel("");
add(messageJLabel);
message2JLabel = new JLabel("");
add(message2JLabel);
newGameJButton = new JButton("NEWGAME");
add(newGameJButton);
Random generator = new Random();
number = generator.nextInt(1001);
newGameJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
guessInputJTextField.setText("");
Random generator = new Random();
number = generator.nextInt(1001);
random1.setText(""+ number);
SwingUtilities.updateComponentTreeUI(random1);
messageJLabel.setText("");
guessInputJTextField.setEditable(true);
Guesses =0;
message2JLabel.setText("NUMBER OF GUESSES: "+ Guesses);
Guesses++;
}
}
);
theGame();
}
public void theGame()
{
}
class GuessHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int Guess;
Guess = Integer.parseInt(guessInputJTextField.getText());
if (Math.abs(number - Guess) < Math.abs(number - GuessOld))
{
getContentPane().setBackground(Color.RED);
}
else
{
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);
}
if (Guess == number)
{
messageJLabel.setText("CORRECT! ");
getContentPane().setBackground(Color.YELLOW);
SwingUtilities.updateComponentTreeUI(messageJLabel);
guessInputJTextField.setEditable(false);
}
message2JLabel.setText("NUMBER OF GUESSES: " + Guesses++);
}
}
}
GuessGame.java
import javax.swing.JFrame;
public class GuessGame
{
public static void main(String args[]) throws Exception
{
Gameframe guessgame = new Gameframe();
guessgame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guessgame.setSize(550, 150);
guessgame.setVisible(true);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images