Java Jtextfield和keylistener

Java Jtextfield和keylistener,java,swing,jtextfield,keylistener,Java,Swing,Jtextfield,Keylistener,我有一个jtextfield(jt),我希望一旦用户在其中键入“e”,就会在jtextfield中自动写入单词“example” 我使用代码: KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent e) { jt.setText("Example"); } } 但是当按下e时,这会给出“example”!有什么想法吗?非常感谢不要在文本组件上使用KeyList

我有一个jtextfield(jt),我希望一旦用户在其中键入
“e”
,就会在jtextfield中自动写入单词
“example”

我使用代码:

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

但是当按下e时,这会给出
“example”
!有什么想法吗?非常感谢

不要在文本组件上使用
KeyListener
,存在大量问题(未通知、变异异常、用户将内容粘贴到字段时未通知),相反,您应该使用

例如

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); 
        }

    }

}

不要在文本组件上使用
KeyListener
,存在大量问题(未通知、变异异常、用户将内容粘贴到字段时未通知),相反,您应该使用

例如

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); 
        }

    }

}

您可以移动
jt.setText(“示例”)
进入
键盘监听器的
public void keyereleased(KeyEvent e)
中,您可以移动
jt.setText(“示例”)
进入
KeyListener的
public void keyReleased(KeyEvent e)

按键时,字段中设置了单词“Example”,但由于您没有使用添加到文本字段中的事件“e”,请使用e.consume()为了避免它。@Arvind不能保证您的侦听器会被调用first@MadProgrammer我同意vid u,但OP已询问d原因n我正在分享我对wat可能发生的看法。当你按下键时,字段中设置了“示例”一词,但由于你没有使用添加到文本字段中的事件“e”,请使用e.consume()为了避免它。@Arvind不能保证您的侦听器会被调用first@MadProgrammer我同意vid u,但OP询问了d原因n,我正在分享我的观点,关于wat会发生的情况。@user3265784,不要使用KeyListener!!!更好的解决方案是使用程序员演示的DocumentFilter。这是一个较新的API,专门为Swing设计。KeyListener在旧的AWT应用程序中使用,因为没有其他选择。@user3265784,不要使用KeyListener!!!更好的解决方案是使用程序员演示的DocumentFilter。这是一个较新的API,专门为Swing设计。KeyListener用于旧的AWT应用程序,因为没有其他选择。