Jtextfield和keylistener [英] Jtextfield and keylistener

查看:139
本文介绍了Jtextfield和keylistener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个jtextfield(jt)我想要用户输入e,例如单词示例将自动写入jtextfield。

I have a jtextfield (jt) where I want as soon as the user types "e" for example in it, the word "Example" to be written automatically in the jtextfield.

我使用代码:

KeyListener keyListener = new KeyListener() {
    public void keyPressed(KeyEvent e) {
        jt.setText("Example");
    }
} 

但这会给出示例按下e时!有任何想法吗?非常感谢

But this gives "Examplee" when e is pressed! Any ideas? Thanks a lot

推荐答案

不要在文本组件上使用 KeyListener ,有一系列问题(未通知,变异异常,当用户将某些内容粘贴到字段中时未通知),而是应该使用 DocumentFilter

Don't use KeyListener on text components, there are a rafter of issues (not been notified, mutation exceptions, not been notified when the user pastes something into the field), instead, you should be using a DocumentFilter

例如......

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class TextFieldExample {

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

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

                JTextField field = new JTextField(20);
                ((AbstractDocument)field.getDocument()).setDocumentFilter(new ExampleExpandingDocumentFilter());

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

    public class ExampleExpandingDocumentFilter extends DocumentFilter {

        @Override
        public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
            System.out.println("I" + text);
            super.insertString(fb, offset, text, attr);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if ("e".equalsIgnoreCase(text)) {
                text = "example";
            }
            super.replace(fb, offset, length, text, attrs); 
        }

    }

}

这篇关于Jtextfield和keylistener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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