This is my current code that I am working with, it is compiling correctly but the problem is that instead of the whole window being colored I need just the JTextbox to be in red for too high a guess and blue for too low a guess. I have tried everything and messsed up the code more so I am here looking for some pointers on how to make my JTextbox to colors for too hot and too cold
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("unused")
public class GuessGame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
// define class variables here
private JButton tryButton;
private JButton newgameButton;
private JButton exitButton;
private JTextField guessField;
private JLabel guessLabel;
private JLabel headerLabel;
private JTextField results;
int num;
int count;
/**
* Class constructor. It is in this method where you will initialize all
* your class variables and build the GUI
*/
public GuessGame() {
//set title of frame
setTitle("Guessing Game");
//set size of frame
setSize(325, 210);
//create the random value
Random rand=new Random();
num=rand.nextInt(1000)+1;
count=0;
// initialize all class variables
tryButton = new JButton("Try the number");
exitButton = new JButton("Exit");
newgameButton = new JButton("New Game");
guessLabel = new JLabel("Enter your guess:");
headerLabel = new JLabel(
"<html>I have a number between 1 and 1000,<br>can you guess the number?</html>");
guessField = new JTextField(5);
results = new JTextField(25);
// add a default close operation to the main frame
// so that it closes when x is pressed on the top
// right corner of the GUI.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add an action listener to your buttons
// each button can have its own separate class
// since each will act differently
TryButtonHandler tbHandler = new TryButtonHandler();
tryButton.addActionListener(tbHandler);
ExitButtonHandler eHandler = new ExitButtonHandler();
exitButton.addActionListener(eHandler);
NewGameButtonHandler ngHandler = new NewGameButtonHandler();
newgameButton.addActionListener(ngHandler);
// set the mnemonic of each button
tryButton.setMnemonic('T');
exitButton.setMnemonic('E');
newgameButton.setMnemonic('N');
// set the results field properties
results.setBackground(Color.white);
results.setEditable(false);
results.setText("");
// set the layout
setLayout(new FlowLayout(FlowLayout.CENTER));
// add all components to the frame
add(headerLabel);
add(guessLabel);
add(guessField);
add(tryButton);
add(exitButton);
add(newgameButton);
add(results);
}
/**
* Inner class for the try button This class contains code that will act on
* the try button when it is pressed.
*/
class TryButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
// TODO: you need to put the code here for the try button
int guess=0;
count++;
try{
guess=Integer.parseInt(guessField.getText());
}catch (Exception e) {
// TODO: handle exception
}
if(guess==num){
results.setText("Correct! The number of guesses: "+count);
}else if(guess<num){
results.setText("Too Low!");
getContentPane().setBackground(Color.white);
}else {
results.setText("Too High!");
getContentPane().setBackground(Color.white);
}
}
}
/**
* Inner class for the exit button This class contains code that will act on
* the exit button when it is pressed.
*/
class ExitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
}
/**
* Inner class for the new game button This class contains code that will
* act on the new game button when it is pressed.
*/
class NewGameButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
// TODO: You need to put your code here for the new game button
Random rand=new Random();
num=rand.nextInt(1000)+1;
results.setText("");
guessField.setText("");
count=0;
}
}
/**
* Class' main method.
*
* @param args
*/
public static void main(String[] args) {
GuessGame g=new GuessGame();
g.setVisible(true);
}
}