1

我正在尝试重置我的 JTextField 以防输入 Space 。它抛出了我:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification

我的代码是:

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class MainFrame extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    /** * Launch the application. */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /** * Create the frame. */
    public MainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JDesktopPane desktopPane = new JDesktopPane();
        desktopPane.setBackground(Color.LIGHT_GRAY);
        desktopPane.setBounds(6, 6, 438, 266);
        contentPane.add(desktopPane);

        textField = new JTextField();
        textField.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
            }

            public void removeUpdate(DocumentEvent e) {
            }

            public void insertUpdate(DocumentEvent e) {
                if (textField.getText().contains(" ")) {
                    System.out
                            .println("Space was entered! clearing the  text field");
                    textField.setText("");
                }
            }
        });
        textField.setBounds(133, 112, 134, 28);
        desktopPane.add(textField);
        textField.setColumns(10);
    }
}
4

2 回答 2

1

ADocumentListener仅适用于对 a 的底层Document的通知JTextComponent。你需要一个DocumentFilter这里

于 2013-09-12T14:43:33.137 回答
0

文档监听器不应该修改文档的内容;到侦听器收到更改通知时,更改已经完成。相反,编写一个自定义文档来覆盖 insertString 或 remove 方法,或两者​​兼而有之。有关详细信息,请参阅侦听文档的更改。

(来自http://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html

我相信不是编写自定义文档,而是更容易实现 KeyListener,如下所示:

JTextField textField = new JTextField();
textField.addKeyListener( new KeyListener() {

@Override
public void keyTyped(KeyEvent arg0) {
    if(arg0.getKeyChar() == ' '){
        System.out.println("Space was entered! clearing the  text field");
        textField.setText("");
        arg0.consume();
    }

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

清除arg0.consume()文本字段后阻止空间出现的位置

于 2013-09-12T14:49:07.513 回答