2 Replies - 356 Views - Last Post: 26 February 2012 - 09:42 PM Rate Topic: -----

#1 neato0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 29-August 11

Game: click the start button and nothing happens

Posted 26 February 2012 - 08:47 PM

The program saves with not errors and complies but when the start button is clicked nothing happens. Just wondering what the gurus thought. also line 44 i get a warning saying prompt isnt being used but it is. Wondering about that also.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GuessTheNumberGame extends JFrame implements ActionListener
{
	//variables, objects first
	final int FRAME_WIDTH = 400;
	final int FRAME_HEIGHT = 200;
	int num;
	int userAnswer;
	
	
	
	// constructor
	public  GuessTheNumberGame()
	{
		super("Guess the Number Game");
		setSize (FRAME_WIDTH, FRAME_HEIGHT);
		setLayout( new FlowLayout (FlowLayout.CENTER));
		
		
		
		setBackground(Color.MAGENTA);
		//component classes are local
		JButton button = new JButton("Start Game");
		button.setToolTipText("Click to continue");
		add(button);
		button.addActionListener(this);
		
		JTextField userGuess = new JTextField();
		add(userGuess);
		userGuess.addActionListener(this);
		
		JLabel jbel = new JLabel();
		add(jbel);
		
		num = (1 + (int)(Math.random() * 1000)); System.out.println("attempt: " + num);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	public void actionPerformed(ActionEvent e)
	{
		// declare string variable
		String prompt = ("");
		
		if(userAnswer <= 0 || userAnswer > 1000)
		{
			prompt = ("I have a number 1 and 1000. Can you guess it?");
			//set background color
			setBackground(Color.MAGENTA);
		}
		else if(userAnswer > num)
		{
			
			prompt = (userAnswer + " To HOt.");
			setBackground(Color.RED);
		}	
		
		else if(userAnswer < num)
		{
			
			prompt = (userAnswer + "To Low. But you getting cooler.");
			setBackground(Color.BLUE);
			
		}
		else if(userAnswer == num)
		{
			prompt = (userAnswer + " is the right number!!!");
			setBackground(Color.MAGENTA);
			
		}
		
		
		
		
		
		
	}
	public static void main(String[] args)
	{
		GuessTheNumberGame frame = new GuessTheNumberGame();
		frame.setVisible(true);
	}
	

}


This post has been edited by neato0z: 26 February 2012 - 08:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Game: click the start button and nothing happens

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Game: click the start button and nothing happens

Posted 26 February 2012 - 08:52 PM

you check for
if(userAnswer <= 0 || userAnswer > 1000)
in your actionPerformed
Where is userAnser assigned a value ?
Was This Post Helpful? 1
  • +
  • -

#3 neato0z  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 29-August 11

Re: Game: click the start button and nothing happens

Posted 26 February 2012 - 09:42 PM

View Postpbl, on 26 February 2012 - 08:52 PM, said:

you check for
if(userAnswer <= 0 || userAnswer > 1000)
in your actionPerformed
Where is userAnser assigned a value ?


youre right.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1