954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

GUI Guessing Game help, not displaying

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
}
}
fin4424
Newbie Poster
3 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

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);

toferdagofer
Light Poster
32 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

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

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

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

fin4424
Newbie Poster
3 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

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

toferdagofer
Light Poster
32 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
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??

fin4424
Newbie Poster
3 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: