Hi so basically im pretty sure i have all my code right and its compiling but for some reason when i run it the frame will load and then quit on me without saying anything i dont know why here is my code
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 my numb");
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( 220, 150 );
gField = new JTextField(4); //setting it to four because the guessed number should$
gField.addActionListener(
public void actionPerformed( ActionEvent event)
{
counter++;
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);
}
public static void main(String[] args)
{
GuessGame myGuessGame = new GuessGame(); //instantiate a GUI object
}
}