【发布时间】:2016-06-15 01:45:10
【问题描述】:
我使用 java.awt.Frame 制作了一个 GUI 文件 I/O 应用程序。有两个标记为“ENTER”和“DONE”的按钮。输入按钮使程序将文本字段中的数据存储到文件中,而完成按钮使程序退出。点击“ENTER”按钮的事件由action()方法处理,而“DONE”按钮的事件由handleEvent()方法处理。该程序运行并完美地完成了这项工作,但问题是每当我单击一个按钮时,终端窗口就会出现在 GUI 框架后面,显示一个长的运行时异常消息。我将整个异常消息中的几行中的一行标识为指向 handleEvent() 方法中的一行(line:78)。
See the full exception message here.(谷歌文档)
以下是 handleEvent() 和 action() 方法的定义。 请预测运行时异常的可能原因并帮助我解决它。谢谢。
64 public boolean action(Event event, Object o){
65 if(event.target instanceof Button){
66 if(event.arg.equals("ENTER")){
67 try{
68 addRecord(); //calls the function to write data to the file
69 }
70 catch(Exception e){}
71 }
72 }
73 return super.action(event,o);
74 }
...
...
76 public boolean handleEvent(Event e){
77 if(e.target instanceof Button){
78 if(e.arg.equals("DONE"))
79 System.exit(0); //exits the program
80 }
81 return super.handleEvent(e);
82 }
...
...
84 public static void main(String args[]){
85 new ClassName().prepareGUI(); //prepareGUI() setups the Frame
86 }
【问题讨论】:
标签: java events button runtime-error