【问题标题】:Get Value of JTextField Changed获取 JTextField 的值已更改
【发布时间】:2014-01-13 03:33:38
【问题描述】:

我正在制作一个转换温度的小型 java Swing Applet:TempConvert.java

这是我的代码:

package swing;

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

/** Celcius to Fahrenheit Converter
 * @version 1.0
 * @author Oliver Ni
 */

public class TempConvert extends JApplet{
    JLabel result;
    JRadioButton ctof;
    JRadioButton ftoc;
    JTextField deg;
    JLabel degLab;
    JButton convert;

    public void convert() {
        if (ctof.isSelected() == true) {
            result.setText("<html><br>" + Integer.toString(Integer.parseInt(deg.getText()) * 9 / 5 + 32) + "<sup>o</sup> F</html>");
        } else if (ftoc.isSelected() == true) {
            result.setText("<html><br>" + Integer.toString((Integer.parseInt(deg.getText()) - 32) * 5 / 9) + "<sup>o</sup> C</html>");
        } else {
            result.setText("<html><br>Error.</html>");
        }
    }

    public void makeApplet() {
        setLayout(new FlowLayout());
        ctof = new JRadioButton("Celcius to Fahrenheit");
        ftoc = new JRadioButton("Fahrenheit to Celcius");
        convert = new JButton("Convert");
        result = new JLabel("");
        ButtonGroup group = new ButtonGroup();
        group.add(ctof);
        group.add(ftoc);

        deg = new JTextField(10);
        degLab = new JLabel("<html><sup>o</sup></html>");
        convert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                convert();
            }
        });
        add(ctof);
        add(ftoc);
        add(deg);
        add(degLab);
        add(convert);
        add(result);
    }

    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    makeApplet();
                }
            });
        } catch(Exception e) {
            System.out.println("Error loading because " + e);
        }
    }
}

每当JTextField deg 中的文本发生更改时,我都想调用convert() 函数。有什么办法可以做到吗?

任何帮助将不胜感激!

【问题讨论】:

  • 您已经在转换按钮中添加了一个 ActionListener -- 这不就是您需要做的吗?
  • 这是作业吗?如果是这样,您有具体要求吗?如果是这样,请发布它们,以及您的代码尝试解决当前问题。如果这是我的程序,我要做的第一件事就是让它更健壮:允许双数字输入,使用 JFormattedTextField 只允许数字输入,然后美化 GUI 布局。

标签: java swing jtextfield temperature valuechangelistener


【解决方案1】:

目前,您的转换按钮上附加了一个 ActionListener。您需要为 JTextField deg 实现相同的 ActionListener@

或者您编写代码尝试这样当您获得文本字段的事件时,您使用 postActionEvent 将事件发布到按钮

【讨论】:

    【解决方案2】:

    需要在文本字段中添加监听器。添加以下sn-p,它应该可以工作。

    deg.getDocument().addDocumentListener(new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            convert();
        }
        public void removeUpdate(DocumentEvent e) {
            convert();
        }
        public void insertUpdate(DocumentEvent e) {
            convert();
        }
    });
    

    【讨论】:

    • +1 添加空检查。当您删除和更改并且没有文本时。避免空指针
    猜你喜欢
    • 2021-07-24
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 2015-05-10
    • 2012-12-26
    • 2012-02-01
    • 2012-10-28
    相关资源
    最近更新 更多