将从JButton获得的值传递给变量

时间:2015-06-11 22:03:19

标签: java swing awt

所以我的问题是我在java swing中制作了一个非常简单的猜数游戏,我添加了一个JButton和一个JTextField,我想点击按钮获取写在我的文本字段中的值,然后将其传递给做一个while循环,它将进行操作,然后我将在JLabel中显示它,但我不知道如何。我没有JFrame,因为我要将这个类发送到具有JFrame的类,所以我将所有内容添加到JPanel。 希望我很清楚,会感激一些帮助。

代码:

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 个答案:

答案 0 :(得分:4)

我的主要建议:摆脱while循环。此循环适用于线性控制台类型程序,但这不是事件驱动的GUI的工作方式,并且该循环将占用Swing事件线程,从而使GUI冻结且无用。因此,再次,摆脱该循环,而是根据用户的响应以及该状态的基本行为来更改此类中变量的状态。因此,在您的ActionListener中,从JTextField获取文本,通过解析它将其转换为int,检查它是否是一个不错的猜测,并通知用户他们的猜测结果。

其他问题:

  • 您了解上面的类创建两个 JPanel,一个是JPanel,它是类本身的实例,并且您不添加任何内容,另一个是{{{ 1}} 执行的变量添加组件。如果你只是要使用面板变量JPanel,那么该类可能不应该扩展JPanel。
  • 与... panel//here should go the value captured from the JTextField instead of a scanner有什么关系?摆脱这个非编译代码,没有有用的代码,并使用JTextField。看起来你正试图将控制台代码插入到GUI中,这样就无法工作。
  • 请勿使用player = input nextInt();或类似代码进行用户交互,但仅限于临时调试。所有用户交互都应该通过GUI本身,最终的生产代码不应该有printlns。

答案 1 :(得分:0)

最后结果是这样,它运作正常。感谢。

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.");


      }