为什么我的GUI中没有可变打印?

时间:2019-07-24 16:30:33

标签: java swing jframe

由于我是Java的新手,目前我正在开发一个非常简单的GUI。我想知道为什么我的变量不会显示在message中。

作品

...
  private void createContents() {
    JLabel numberPrompt = new JLabel("What's your random number (min will be 1)?");
    numberBox = new JTextField(15);
    maxNumberString = numberBox.getText();
    greeting = new JLabel();
    add(numberPrompt);
    add(numberBox);
    add(greeting);
    numberBox.addActionListener(new Listener());
  }

  private class Listener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String message;
      message = "Glad to meet you, " + numberBox.getText(); + "!";
      numberBox.setText("");
      greeting.setText(message);
    }
...

不起作用

...
  private void createContents() {
    JLabel numberPrompt = new JLabel("What's your random number (min will be 1)?");
    numberBox = new JTextField(15);
    maxNumberString = numberBox.getText();
    greeting = new JLabel();
    add(numberPrompt);
    add(numberBox);
    add(greeting);
    numberBox.addActionListener(new Listener());
  }

  private class Listener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String message;
      message = "Glad to meet you, " + maxNumberString + "!";
      numberBox.setText("");
      greeting.setText(message);
    }
...

为什么第一个代码可与numberBox.getText()一起使用,但是当我将其替换为maxNumberString时,即使变量的值为numberBox.getText()也不起作用吗?

完整代码

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

public class Greeting extends JFrame {
  private static final int WIDTH = 400;
  private static final int HEIGHT = 150;
  private JTextField numberBox;
  private JLabel greeting;
  private String maxNumberString;
  private Integer maxNumber;
  private String randomNumbeString;
  private Integer randomNumber;

  public Greeting() {
    setTitle("Random Number Generator");
    setSize(WIDTH, HEIGHT);
    setLayout(new FlowLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    createContents();
    setVisible(true);
  }

  private void createContents() {
    JLabel numberPrompt = new JLabel("What's your random number (min will be 1)?");
    numberBox = new JTextField(15);
    maxNumberString = numberBox.getText();
    greeting = new JLabel();
    add(numberPrompt);
    add(numberBox);
    add(greeting);
    numberBox.addActionListener(new Listener());
  }

  private class Listener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String message;
      message = "Glad to meet you, " + maxNumberString + "!";
      numberBox.setText("");
      greeting.setText(message);
    }
  }

  public static void main(String[] args) {
    new Greeting();
  }
}

1 个答案:

答案 0 :(得分:3)

numberBox = new JTextField(15);
maxNumberString = numberBox.getText();

在执行上述语句时,文本字段为空。如果事实甚至看不到框架,则用户没有机会在文本字段中输入文本。

没有自动更新带有文本的变量!

如果要自动更新变量,则需要添加逻辑以在每次生成事件时更新变量。例如,您可以在文本字段的DocumentListener中添加Document。每次从文本字段添加(或删除)文本时都会生成一个事件。然后,您可以获取文本字段的当前值。

阅读How to Write a DocumentListner的Swing教程中的部分,以获取更多信息和入门示例。