我希望消息框在用户更改文本字段中的值后立即出现。目前,我需要按回车键来弹出消息框。我的代码有什么问题吗?

textField.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e) {

        if (Integer.parseInt(textField.getText())<=0){
            JOptionPane.showMessageDialog(null,
                    "Error: Please enter number bigger than 0", "Error Message",
                    JOptionPane.ERROR_MESSAGE);
        }       
    }
}

任何帮助都将不胜感激!


当前回答

你甚至可以使用“MouseExited”来控制。 例子:

 private void jtSoMauMouseExited(java.awt.event.MouseEvent evt) {                                    
        // TODO add your handling code here:
        try {
            if (Integer.parseInt(jtSoMau.getText()) > 1) {
                //auto update field
                SoMau = Integer.parseInt(jtSoMau.getText());
                int result = SoMau / 5;

                jtSoBlockQuan.setText(String.valueOf(result));
            }
        } catch (Exception e) {

        }

    }   

其他回答

向底层Document中添加侦听器,底层Document是自动为您创建的。

// Listen for changes in the text
textField.getDocument().addDocumentListener(new DocumentListener() {
  public void changedUpdate(DocumentEvent e) {
    warn();
  }
  public void removeUpdate(DocumentEvent e) {
    warn();
  }
  public void insertUpdate(DocumentEvent e) {
    warn();
  }

  public void warn() {
     if (Integer.parseInt(textField.getText())<=0){
       JOptionPane.showMessageDialog(null,
          "Error: Please enter number bigger than 0", "Error Message",
          JOptionPane.ERROR_MESSAGE);
     }
  }
});

这里是@Boann的答案的Kotlin移植,这是一个很好的解决方案,对我来说一直很有效。

import java.beans.*
import javax.swing.*
import javax.swing.event.*
import javax.swing.text.*

/**
 * Installs a listener to receive notification when the text of this
 * [JTextComponent] is changed. Internally, it installs a [DocumentListener] on the
 * text component's [Document], and a [PropertyChangeListener] on the text component
 * to detect if the `Document` itself is replaced.
 *
 * @param changeListener a listener to receive [ChangeEvent]s when the text is changed;
 * the source object for the events will be the text component
 */
fun JTextComponent.addChangeListener(changeListener: ChangeListener) {
    val dl: DocumentListener = object : DocumentListener {
        private var lastChange = 0
        private var lastNotifiedChange = 0
        override fun insertUpdate(e: DocumentEvent) = changedUpdate(e)
        override fun removeUpdate(e: DocumentEvent) = changedUpdate(e)
        override fun changedUpdate(e: DocumentEvent) {
            lastChange++
            SwingUtilities.invokeLater {
                if (lastNotifiedChange != lastChange) {
                    lastNotifiedChange = lastChange
                    changeListener.stateChanged(ChangeEvent(this))
                }
            }
        }
    }
    addPropertyChangeListener("document") { e: PropertyChangeEvent ->
        (e.oldValue as? Document)?.removeDocumentListener(dl)
        (e.newValue as? Document)?.addDocumentListener(dl)
        dl.changedUpdate(null)
    }
    document?.addDocumentListener(dl)
}

你可以在任何文本组件上使用它,如下所示:

myTextField.addChangeListener { event -> myEventHandler(event) }

就像他的代码一样,也是公有领域。

Be aware that when the user modify the field, the DocumentListener can, sometime, receive two events. For instance if the user selects the whole field content, then press a key, you'll receive a removeUpdate (all the content is remove) and an insertUpdate. In your case, I don't think it is a problem but, generally speaking, it is. Unfortunately, it seems there's no way to track the content of the textField without subclassing JTextField. Here is the code of a class that provide a "text" property :

package net.yapbam.gui.widget;

import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/** A JTextField with a property that maps its text.
 * <br>I've found no way to track efficiently the modifications of the text of a JTextField ... so I developed this widget.
 * <br>DocumentListeners are intended to do it, unfortunately, when a text is replace in a field, the listener receive two events:<ol>
 * <li>One when the replaced text is removed.</li>
 * <li>One when the replacing text is inserted</li>
 * </ul>
 * The first event is ... simply absolutely misleading, it corresponds to a value that the text never had.
 * <br>Anoter problem with DocumentListener is that you can't modify the text into it (it throws IllegalStateException).
 * <br><br>Another way was to use KeyListeners ... but some key events are throw a long time (probably the key auto-repeat interval)
 * after the key was released. And others events (for example a click on an OK button) may occurs before the listener is informed of the change.
 * <br><br>This widget guarantees that no "ghost" property change is thrown !
 * @author Jean-Marc Astesana
 * <BR>License : GPL v3
 */

public class CoolJTextField extends JTextField {
    private static final long serialVersionUID = 1L;

    public static final String TEXT_PROPERTY = "text";

    public CoolJTextField() {
        this(0);
    }

    public CoolJTextField(int nbColumns) {
        super("", nbColumns);
        this.setDocument(new MyDocument());
    }

    @SuppressWarnings("serial")
    private class MyDocument extends PlainDocument {
        private boolean ignoreEvents = false;

        @Override
        public void replace(int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            String oldValue = CoolJTextField.this.getText();
            this.ignoreEvents = true;
            super.replace(offset, length, text, attrs);
            this.ignoreEvents = false;
            String newValue = CoolJTextField.this.getText();
            if (!oldValue.equals(newValue)) CoolJTextField.this.firePropertyChange(TEXT_PROPERTY, oldValue, newValue);
        }

        @Override
        public void remove(int offs, int len) throws BadLocationException {
            String oldValue = CoolJTextField.this.getText();
            super.remove(offs, len);
            String newValue = CoolJTextField.this.getText();
            if (!ignoreEvents && !oldValue.equals(newValue)) CoolJTextField.this.firePropertyChange(TEXT_PROPERTY, oldValue, newValue);
        }
    }

我对WindowBuilder是全新的,事实上,几年之后才回到Java,但我实现了“一些东西”,然后我想我应该查一下,遇到了这个线程。

我正在测试这个,所以,基于对这一切的新手,我确信我一定遗漏了一些东西。

下面是我所做的,其中“runTxt”是一个文本框,“runName”是类的数据成员:

public void focusGained(FocusEvent e) {
    if (e.getSource() == runTxt) {
        System.out.println("runTxt got focus");
        runTxt.selectAll();
    }
}

public void focusLost(FocusEvent e) {
    if (e.getSource() == runTxt) {
        System.out.println("runTxt lost focus");
        if(!runTxt.getText().equals(runName))runName= runTxt.getText();
        System.out.println("runText.getText()= " + runTxt.getText() + "; runName= " + runName);
    }
}

似乎比这里到目前为止简单得多,而且似乎是有效的,但是,因为我正在写这篇文章,我很高兴听到任何被忽视的陷阱。这是一个问题,用户可以进入和离开文本框w/o作出改变?我认为你所做的一切都是不必要的任务。

我知道这涉及到一个非常老的问题,然而,它也给我带来了一些问题。正如kleopatra在上面的评论中回应的那样,我用JFormattedTextField解决了这个问题。然而,解决方案需要更多的工作,但更整洁。

在默认情况下,JFormattedTextField不会在字段中的每个文本更改后触发属性更改。JFormattedTextField的默认构造函数不创建格式化程序。

但是,要执行OP建议的操作,需要使用格式化程序,它将在字段的每次有效编辑之后调用commitEdit()方法。commitEdit()方法触发了我所看到的属性更改,如果没有格式化器,这将在焦点更改或按下enter键时默认触发。

详情见http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html#value。

创建一个默认的formatter (DefaultFormatter)对象,通过它的构造函数或setter方法传递给JFormattedTextField。默认格式化器的一个方法是setCommitsOnValidEdit(布尔提交),它设置格式化器在每次修改文本时触发commitEdit()方法。然后可以使用PropertyChangeListener和propertyChange()方法来获取。