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

New Topic/Question
Reply




MultiQuote




|