將偵聽器更改為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
提交
取消