0
我有一個圖形用戶界面設計,有許多字段,用戶輸入。我正在使用try-catch
來處理用戶錯誤輸入的異常。當用戶在字段中輸入一個字符串時,我很成功(我的id
字段),但我很沮喪,試圖在用戶在文本/字符串字段中輸入整數時使用異常來處理。 這裏是我的代碼,讓你知道我爲我成功的例外做了些什麼。謝謝。Java GUI Jframe異常處理
try {
String taskID = txtId.getText();
int id = Integer.parseInt(taskID);
String taskName = txtName.getText();
String taskDesc = txtDesc.getText();
boolean isCompleted = chkComp.isSelected();
int taskPriority = comboPriority.getSelectedIndex();
Task newTask = new Task(id, taskName, taskDesc, isCompleted, taskPriority);
tasks.add(newTask);
taskMod.addElement(newTask);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Error: You must enter an integer");
}
你好,謝謝你的回覆,但什麼時候運異常會那是?像這是一個「NumberFormatException」?對不起,我是新來學習這個。 – user3597639
嗯,我在這裏有點困惑。你的目標是讓程序再次提示嗎? – BitNinja
是的,我的目標是提示用戶「你必須在名稱中輸入一個字符串而不是整數」..很抱歉,如果我沒有太大的意義。我的目標是讓它提示出現任何錯誤 – user3597639