Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, im 'trying' to make a program where the user guesses 3 random numbers.
I have built a gui for it and got code for it but i cant get it working correctly.
The code is as follows....
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;class game extends JFrame{
private Container pane;
private JButton buttonStart;
private JLabel title, prompt1, prompt2, prompt3, helpLabel, noOfGuesses;
private JTextField first, second, third;
private JPanel panel, panel1, panel2, panel3;
private ButtonGroup buttons;
private JRadioButton a, b, c;public game(){
super("guessing game");
pane=this.getContentPane();
buttonStart=new JButton("Start guessing");
title =new JLabel("welcome to the guessing game, please select the difficulty and guess each number!");
prompt1 = new JLabel("Enter your first guess:");
prompt2 = new JLabel("Enter your second guess:");
prompt3 = new JLabel("Enter your third guess:");
helpLabel = new JLabel("");
noOfGuesses = new JLabel("");
first = new JTextField("0",20);
second = new JTextField("0",20);
third = new JTextField("0",20);
panel1 = new JPanel();
panel2 = new JPanel();
a = new JRadioButton("Easy", false);
b = new JRadioButton("Medium", true);
c = new JRadioButton("Hard", false);
buttons = new ButtonGroup();
panel = new JPanel();
pane.setLayout(new BorderLayout());
panel1.setLayout(new GridLayout(5,1));
panel2.setLayout(new GridLayout(3,2));buttons.add(a);
buttons.add(b);
buttons.add(c);pane.add(title, BorderLayout.NORTH);
pane.add(buttonStart, BorderLayout.SOUTH);panel2.add(prompt1);
panel2.add(first);
panel2.add(prompt2);
panel2.add(second);
panel2.add(prompt3);
panel2.add(third);pane.add(panel2, BorderLayout.CENTER);
panel1.add(a);
panel1.add(b);
panel1.add(c);
panel1.add(helpLabel);
pane.add(panel1,BorderLayout.EAST);
GuessListener listener = new GuessListener();
Terminator quitWithX = new Terminator();first.addActionListener(listener);
buttonStart.addActionListener(listener);
this.addWindowListener(quitWithX);this.pack();
this.setSize(600,400);
this.show();
}
public static void main(String[] args) {game a = new game();
}class GuessListener implements ActionListener {
private int theHiddenNumber, thisGuess, lastDiff, thisDiff, noOfGuesses=0;
public void actionPerformed(ActionEvent e){
if(e.getSource() == buttonStart){
theHiddenNumber = (int)Math.abs(10 * Math.random());
buttonStart.setText("Another Game?");
noOfGuesses=0;
}
if(e.getSource() == first){
thisGuess = new Integer(first.getText()).intValue();
if(thisGuess > theHiddenNumber)
helpLabel.setText("Too High");
if(thisGuess lastDiff)
helpLabel.setForeground(Color.blue);lastDiff=thisDiff;
noOfGuesses++;
}
}
}
} //end of class DoMaths
class RadioHandler implements ItemListener {public void itemStateChanged(ItemEvent e){
int counter = 0;if(e.getSource()==a){
counter = 20;
}
if(e.getSource()==b){
counter = 10;
}
if(e.getSource()==c){
counter = 5;
}
}}
class Terminator extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}This compiles ok but when i actually impliment it, the program only finds the first guess and then does nothing. I know the loop isnt correct but dont kno how to fix it. Can anyone help me with this?
Im also trying to get the 3 different difficulty levels to work aswell (eg hard=5 guesses, medium=10 guesses and easy=15 guesses) I dont know how to get this to work either.
Can anyone please help with this??Its driving me mad. I would be very greatful to any replys.
Thank you

First off, computing.net trashed your code. It does not compile in J2EE 1.3.
Second, your "easy" level allows 20 guesses, not 15.
Third, there is no loop. The actionPerformed event only checks the first JTextField for a value as this is the only JTextField with an actionListener.
Fourth, and this is just me, I don't like the way you're using variables from class game in your subclasses. It's better to pass references to these objects to the subclasses and make the program modular than to use globals.

![]() |
Several windows in one ap...
|
Using VB to rename Excel ...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |