在文本更改时摆动 JTextField [英] Swing JTextField on text change

查看:23
本文介绍了在文本更改时摆动 JTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Swing 表单.当用户更改 TextField 中的文本时,我想从其他字段获取输入,进行一些计算,然后显示结果.我该怎么做?

I am working on a Swing form. When the user changes text in a TextField, I want to get input from other fields, do some calculations and then display the results. How can I do that?

这是我目前所拥有的:

jTextField3.addKeyListener(

    new KeyAdapter() {
        public void keyTyped(KeyEvent e){
           char c = e.getKeyChar();
           if('0'<=c && c<='9') {
                String a = jTextField6.getText().toString();
                String l = jTextField7.getText().toString();
                int m = Integer.parseInt(a);
                int n = Integer.parseInt(l);
                jTextField13.setText("" + m*n);
          }
       }
    });

推荐答案

如果您想监视一个或多个文本字段的更改,您应该使用 DocumentListener,这也将为您提供当用户将文本粘贴到字段中或以编程方式更改字段时的通知(通过调用 setText)

If you want to monitor for changes to one or more text fields, you should be using a DocumentListener, this will also provide you notification's of when the user pastes text into the field or the field is changed programatically (via a call to setText)

例如...

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Text {

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

    public Text() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            JTextField field1 = new JTextField(10);
            JTextField field2 = new JTextField(10);
            JTextField field3 = new JTextField(10);

            DocumentListener dl = new DocumentListener() {

                @Override
                public void insertUpdate(DocumentEvent e) {
                    updateFieldState();
                }

                @Override
                public void removeUpdate(DocumentEvent e) {
                    updateFieldState();
                }

                @Override
                public void changedUpdate(DocumentEvent e) {
                    updateFieldState();
                }

                protected void updateFieldState() {
                    String text = field1.getText() + " " + field2.getText();
                    field3.setText(text);
                }
            };

            field1.getDocument().addDocumentListener(dl);
            field2.getDocument().addDocumentListener(dl);
            field3.setEditable(false);

            add(field1);
            add(field2);
            add(field3);
        }

    }

}

现在,您似乎正在尝试限制可以输入到字段中的字符.您可以使用 JSpinnerJFormattedTextField,但这些仅提供后期验证.

Now, it appear that you are trying to limit the character that can be typed into the field. You could use a JSpinner or JFormattedTextField, but these only provide post validation.

对于实时验证,您应该使用 DocumentFilter,这将允许您在将其应用于基础 Document 之前拦截输入到字段中的内容.

For real time validation, you should be using a DocumentFilter, which will allow you to intercept what is entered into the field before it's applied to the underlying Document.

参见实施DocumentFilterDocumentFilter 示例了解更多详情

See Implementing a DocumentFilter and DocumentFilter examples for more details

这篇关于在文本更改时摆动 JTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆