don't forget to use:
getContentPane() instead of just adding things to the frame itself. The JFrame is not actually meant to hold items itself other than panels, the default is the content pane. This code piece should precede all add lines and the setLayout line, and your background color changes as well.
also variables should not start with capital letters. The only exception is a static variable which is made of ALL capital letters.
this line does not handle your string having spaces... which i ran into many times, so make it (with a trim):
number = Integer.parseInt((random1.getText()).trim());Your algorithm is correct, but the if statements should have { and } since you are using an else... the only time {} should ever be left out is when there is a 1 line if statement and only 1 line.
Remove the distance method and make your actionHandler:
CODE
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);
}
}
}
please work on your tabbing, it is very inconsistent and hard to read.
If you need any further help, i have finished the program as you described in full.