0

I am trying to turn a terminal based guessing game into a GUI based one. My problem is that there are always issues when I put all the code into the main method.

I figured I would just write the main method last, its only job to make the GUI visible, but I can't make a static reference to the non-static JFrame variable frame. I'm new to Java and programming in general, and I'm just at a loss at this point.

Code

public class GuiGuess extends JPanel implements ActionListener{
    JTextField guessField = new JTextField("Your Guess");
    JTextField minField = new JTextField ("Minimum Boundary");
    JTextField maxField = new JTextField ("Maximum Boundary");
    JTextArea outputArea;
    JButton checkButton;
    JFrame frame = new JFrame("Guessing Game");

    public GuiGuess(){
        JFrame frame = new JFrame("Guessing Game");

        JPanel textHolder = new JPanel();
        JPanel buttonHolder = new JPanel();

        textHolder.add(minField);
        textHolder.add(maxField);
        textHolder.add(guessField);
        textHolder.add(outputArea);

        buttonHolder.add(checkButton);

        frame.getContentPane().add(textHolder, "North");
        frame.getContentPane().add(buttonHolder, "South");
        frame.setVisible(true);

        checkButton.addActionListener(this);
    }


    int numGuesses = 0;
    int counter = 0;
    int guess = Integer.parseInt(guessField.getText());
        public void actionPerformed(ActionEvent e){
            if (counter < 1){
            int min = Integer.parseInt(minField.getText());
            int max = Integer.parseInt(maxField.getText());
            int numToGuess = min + (int)(Math.random()*max);
            }
            guess = Integer.parseInt(guessField.getText());
            numGuesses++;
            counter++;
            if (guess < numToGuess){
                outputArea.setText("Your guess is too low");
            }
            else if (guess > numToGuess){
                outputArea.setText("Your guess is too high");
            }
                else if (guess == numToGuess){
                    outputArea.setText("You Win! It took you "+numGuesses +"Tries!");
                }

                }

public static void main(String Args[]){
    frame.setVisible(true);

}

}
Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
Matt
  • 1
  • 1
  • Possible duplicate of [non-static variable cannot be referenced from a static context](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – resueman Mar 01 '16 at 19:55
  • As written, that code won't compile. – Zymus Mar 01 '16 at 20:01
  • 2
    Generally speaking, it's not a good idea to anticipate the use of your panel, it's not its (core) responsibility to create the frame. Instead, you could create an instance of a JFrame from your main method and add and instance of the panel to it – MadProgrammer Mar 01 '16 at 20:34

2 Answers2

1

Bad option: make frame static.

Better: create a method in GuiGuess:

public void show() {
    frame.setVisible(true);
}

and in your main:

 GuiGuess gGuess = new GuiGuess();
 gGuess.show();

Because frame is a member variable of class GuiGuess, and therefore changing it's state should go through GuiGuess.

Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
0

Frame is a member of the class GuiGuess.... so you need to use an instace

do in the static main the following:

public static void main(String Args[]){
  GuiGuess  myGui = new GuiGuess( );        
  myGui.setVisible(true);
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97