pass a value obtained from a JButton to a variable

1.3k views Asked by At

so my problem is that i'm making a really simple number guessing game in java swing, i added a JButton and a JTextField, i want to click the button to get the value written in my text field and then pass it to a do while loop that's gonna make the operation, then i'm going to display it in a JLabel, But i don't know how. I have no JFrame cause i'm gonna send this class to the class that has the JFrame, so i'm adding everything to a JPanel. hope I was clear, would appreciate some help.

code:

import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GameDat extends JPanel {
JPanel panel = new JPanel();
FlowLayout layout = new FlowLayout();
Random random = new Random();
JButton enter;
JTextField display;
JLabel result;

int MIN = 1;
int MAX = 50;
int compare = random.nextInt(MAX - MIN + 1) + MIN;
int player;
int guessIt = 0;


public void iniGame(){

    enter = new JButton("Pick a number:");

    panel.setSize(300,300);
    panel.setLayout(layout);
    panel.add(enter);
    panel.add(display);
    panel.add(result);

    enter.addActionListener(new CustomActionListenerEnter());


      do {

     //here should go the value captured from the JTextField instead of a scanner
     player = input nextInt();
     guessIt++;

        if (player > compare)
            //this text should go to the jlabel instead of printing it
            System.out.println("Number is less than " + player);
        else if (player < compare)
            //this text should go to the jlabel instead of printing it
             System.out.println("Number is greater than " + player);
        else 
             //this text should go to the jlabel instead of printing it
             System.out.println(compare + " was my number, it took you " + guessIt + " guesses.");
    } while (player != compare);
}

class CustomActionListenerEnter implements ActionListener{

        public void actionPerformed(ActionEvent e){
            //I wanna pass this value to my do while.
            display.getText();


        }
    }



}
2

There are 2 answers

2
Hovercraft Full Of Eels On BEST ANSWER

My main recommendation: get rid of the while loop. This loop works with a linear console type program, but this is not how event-driven GUI's work, and that loop will tie up the Swing event thread, rendering your GUI frozen and useless. So, again, get rid of that loop, and instead change the state of a variable in this class based on user's response, and base behavior on that state. So in your ActionListener, get the text from the JTextField, convert it to an int by parsing it, check if it's a decent guess, and notify the user of the results of their guess.

Other issues:

  • You understand that your class above creates two JPanels, one the JPanel which is the instance of the class itself, and which you add nothing to, and the other one that is referenced by the panel variable that you do add components to. If you're only going to use the panel variable JPanel, then the class should probably not extend JPanel.
  • What's with the,... //here should go the value captured from the JTextField instead of a scanner and player = input nextInt();? Get rid of this non-compiling code and not helpful code and use the JTextField. It looks like you're trying to shoe-horn console code into a GUI, and this won't work.
  • Do not use System.out.println(...) or similar code for user interaction, but only for temporary debugging if that. All user interaction should be through the GUI itself, and final production code should not have printlns.
0
user3732562 On

Well it turned out like this at the end, and it works fine. Thanks.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    player = Integer.parseInt(jTextField3.getText());
    guessIt++;

        if (player > compare)
           jTextField1.setText("Number is less than " + player);

        else if (player < compare)
            jTextField1.setText("Number is greater than " + player);

        else 
          jTextField1.setText(compare + " was my number, it took you " +
          guessIt + " guesses.");


      }