1

--EDIT--
我有一个JTextField,并且我希望每次有人更改(键入或删除)JTextField 中的字符时调用一个方法(现在只是打印语句)。它背后的目的是让该方法立即检查键入的内容是否满足某些条件。感谢您的帮助,我设法写了:

public class MyDocumentListener implements DocumentListener {

    public void insertUpdate(DocumentEvent e) {
        updateLog(e, "inserted into");
    }
    public void removeUpdate(DocumentEvent e) {
        updateLog(e, "removed from");
    }
    public void changedUpdate(DocumentEvent e) {
        //Plain text components do not fire these events
    }

    public void updateLog(DocumentEvent e, String action) {
        System.out.println("should call the method here");
    }
}

JTextField 代码:

    JTextField passwordField = new JTextField();
    passwordField.getDocument().addDocumentListener(new MyDocumentListener());
    passwordField.getDocument().putProperty("name", "Text Field");

我现在遇到的问题是我需要使用

String textFieldPassword = passwordField.getText();

但它返回NullPointerException。我假设这是因为我添加了DocumentListenerand 应该DocumentEvent现在正在运行。但我真的不知道该怎么做。

4

2 回答 2

2

您需要该字段的操作侦听器:

public class YourClass extends JPanel implements ActionListener {

    public void addComponents() {
        ...
        passwordField.addActionListener(this);
        ...
    }

    /**
      will be fired when the password field changes
    */
    public void actionPerformed(ActionEvent evt) {
        String text = passwordField.getText();
        System.out.println("key pressed");
    }
}

如果这不符合您的口味,那么您可以尝试DocumentListener

于 2012-02-19T23:34:16.277 回答
1

这就是我最终得到的结果(当 2 个密码匹配时启用按钮):

public class ChangePasswordUI implements DocumentListener, ActionListener {
    private JFrame frame;
    private JPasswordField newPassword1 = new JPasswordField(20);
    private JPasswordField newPassword2 = new JPasswordField(20);
    private JButton OKbutton;

    protected ChangePasswordUI() {
        OKbutton.addActionListener(this);
        newPassword1.addActionListener(this);
        newPassword2.addActionListener(this);
        newPassword1.getDocument().addDocumentListener(this);
        newPassword2.getDocument().addDocumentListener(this);

        frame = new JFrame();
        frame.add(newPassword1);
        frame.add(newPassword2);
        frame.pack();
        updateOKbutton();
        frame.setVisible(true);
    }

    private void updateOKbutton() {
        if(Arrays.equals(newPassword1.getPassword(),newPassword2.getPassword()) == false) {
            OKbutton.setEnabled(false);
        } else {
            OKbutton.setEnabled(true);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == cancelButton) {
            frame.dispose();
        } else if (e.getSource() == OKbutton) {
            frame.dispose();
        } else if (e.getSource() == newPassword1) {
            updateOKbutton();
        } else if (e.getSource() == newPassword2) {
            updateOKbutton();
        }
    }

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

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

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

笔记:

  • 示例代码是我正在使用的“设置新密码”对话框的精简版,而且这些JFrame东西可能完全是狡猾的(我正在使用一个凌乱GridBagLayout()的东西,没有人想看到它)
  • .getText()方法现在已弃用(您想使用.getDocument())。
  • anactionListener对于密码字段几乎没有用(但可能需要其他东西,所以为什么不包括它呢!)
于 2019-07-24T02:21:36.460 回答