将侦听器更改为JTextField我希望消息框在用户更改文本字段中的值后立即出现。目前,我需要按回车键使消息框弹出。我的密码有什么问题吗?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);
}
}}任何帮助都将不胜感激!
3 回答
慕婉清6462132
TA贡献1804条经验 获得超2个赞
// Listen for changes in the texttextField.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); } }});
添加回答
举报
0/150
提交
取消