PDA

View Full Version : GUI Style Guessing Game Problems


mrdoody55
08-05-2007, 05:53 PM
Hello All. After a little while of trying to figure this one out myself, I have given up on what should be a simple fix. I know it is probably a mess, but any help you guys could give would be much appreciated.

I get the error:
GuiGuessingGameApp.java:7: cannot find symbol
symbol : constructor GuiGuessingGameFrame()
location: class GuiGuessingGameFrame
GuiGuessingGameFrame frame = new GuiGuessingGameFrame();



And here is my code:
public class GuiGuessingGameTest
{
public static void main(String[] args)
{
GuiGuessingGameApp game = new GuiGuessingGameApp();
game.play();
}
}

import java.util.Random;

public class GuiGuessingGameApp
{
Random rnd = new Random();
int secretNumber = 0;
GuiGuessingGameFrame frame = new GuiGuessingGameFrame();

public GuiGuessingGameApp()
{
secretNumber = 1 + rnd.nextInt(100);
}

public void play()
{
if (frame != null)
frame.show();
}
}

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class GuiGuessingGameFrame
{
JFrame myFrame = new JFrame();
JTextField myField = new JTextField("",2);
JLabel myLabel = new JLabel("Enter A Guess");
JButton myButton = new JButton ("Guess!");

int mySecretNumber = -1;

public GuiGuessingGameFrame(int mySecretNumber)
{
myFrame = new JFrame("Guessing Game!");
myFrame.setSize(200,95);
myFrame.setVisible(true);
myFrame.addWindowListener(new WinHandler());

Container contain = myFrame.getContentPane();
contain.setLayout(new FlowLayout());

contain.add(myField);
contain.add(myButton);
contain.add(myLabel);

myButton.addActionListener(new ButtonListener());
}

public void show()
{
myFrame.show();
}

class ButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent evt)
{
int guess = 0;
try
{
guess = Integer.parseInt(myField.getText());
}
catch (NumberFormatException e)
{
guess = 0;
}
System.out.println("The Number In The Text Box Is " + guess);
}
}
}
class WinHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e) {System.exit(0);}
}

Aradon
08-05-2007, 06:08 PM
When you create a new Frame in your code you call it with a constructor that has no arguements:



GuiGuessingGameFrame frame = new GuiGuessingGameFrame();


But in your GuiGuessingGameFrame class, your constructor is listed as this:


public GuiGuessingGameFrame(int mySecretNumber)
{



You need to either include an integer argument for the constructor when you call it, or create a default constructor of some sort in your GuiGuessingGameFrame class.

mrdoody55
08-05-2007, 06:16 PM
You are a savior. :) Thank you very much.

The professor ended the class halfway through the program, and he said to finish it the way we wanted to. I must have still had some of his coding in there.

Once again, thank you much for the quick reply.

Gox
08-05-2007, 09:03 PM
Just in case you look this thread over again even though your problem has been solved, I thought I'd mention the following.

Your Constructor is "public GuiGuessingGameFrame(int mySecretNumber)", but you never use the variable mySecretNumber in any of your code. While Aradon gave you two ways to solve your issue, you should really go with the one that eliminates the constructor parameter, public GuiGuessingGameFrame(). Otherwise you're just introducing bad coding style, and you've got plenty of time to learn (and hopefully not use) bad habits.

Aradon
08-06-2007, 02:51 AM
Just in case you look this thread over again even though your problem has been solved, I thought I'd mention the following.

Your Constructor is "public GuiGuessingGameFrame(int mySecretNumber)", but you never use the variable mySecretNumber in any of your code. While Aradon gave you two ways to solve your issue, you should really go with the one that eliminates the constructor parameter, public GuiGuessingGameFrame(). Otherwise you're just introducing bad coding style, and you've got plenty of time to learn (and hopefully not use) bad habits.

In this case I would actually suggest adding a default constructor instead. In that case you have the option of overloading the constructor if needed, and also allowing for the int constructor as well.

So either way you do it, with an int or otherwise, you have a complete constructor.

But it's probably down to preference at that point.