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
}
}

i think that your problem may be that you need to put in a setVisible(true) call.

i think that it should be placed right after you set the size for ur gui

Just type setVisible(true);

there isn't anything about visibilty for JFrame, and probably Code folds are wrong, then is this code nonRunable, and must cried ...., something else

i think that your problem may be that you need to put in a setVisible(true) call.

i think that it should be placed right after you set the size for ur gui

Just type setVisible(true);

thanks that worked but i need to adjust the frame to make it visible still??

um shouldn't. since you specified the size already it should be made to that size. if you looking for a smaller window put pack() right after your visiblity statement and comment out your size declaration

um shouldn't. since you specified the size already it should be made to that size. if you looking for a smaller window put pack() right after your visiblity statement and comment out your size declaration

got it to work perfectly but it doesnt run the program again after my first guess now :/ is it because i didnt set my action listen as a boolean??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.