【问题标题】:Jtextfield and keylistenerJtextfield 和 keylistener
【发布时间】:2014-04-09 06:53:33
【问题描述】:

我有一个 jtextfield (jt),只要用户在其中键入 "e",例如,单词 "Example" 就会自动写入 jtextfield。

我使用代码:

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

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

【问题讨论】:

  • 当您按下键时,在字段中设置了“示例”一词,但由于您没有使用添加到文本字段中的事件“e”,请使用 e.consume() 来避免它。
  • @Arvind 不能保证您的听众会被首先调用
  • @MadProgrammer 我同意你的看法,但 OP 要求我分享我的观点,因为我认为 wat 会发生。

标签: java swing jtextfield keylistener


【解决方案1】:

不要在文本组件上使用KeyListener,有很多问题(未通知,突变异常,当用户将某些内容粘贴到字段中时未通知),相反,您应该使用@987654321 @

例如...

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

    }

}

【讨论】:

    【解决方案2】:

    您可以将您的jt.setText("Example"); 移动到您的KeyListenerpublic void keyReleased(KeyEvent e)

    【讨论】:

    • @user3265784,不要使用 KeyListener!!!更好的解决方案是使用 MadProgrammer 演示的 DocumentFilter。这是一个较新的 API,专为 Swing 设计。 KeyListeners 用于旧的 AWT 应用程序,因为没有其他选择。
    猜你喜欢
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 2015-02-21
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    相关资源
    最近更新 更多