因此,我得到了以下代码来制作一个快速弹出框,通知我的用户该程序已完成运行。用户单击确定后,我想处理一些额外的事件,特别是我想关闭一些驱动程序和sql连接。我该怎么做?
这是使jframe的代码
javax.swing.JFrame optionFrame = new javax.swing.JFrame();
JOptionPane.showMessageDialog(optionFrame, "Tests Complete. Screenshots and Results can be found at C://Features");
optionFrame.toFront();
optionFrame.repaint();
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
最佳答案
只要在显示对话框之后但在this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
之前执行此操作
同时删除零件:
optionFrame.toFront();
optionFrame.repaint();
此行(messageDialog弹出窗口):
JOptionPane.showMessageDialog(optionFrame, "Tests Complete. Screenshots and Results can be found at C://Features");
将保持调用它的线程,直到用户单击确定或在弹出窗口上关闭为止,无需执行任何其他操作,此行之后的任何代码将在弹出窗口消失后执行。
根据您的示例,可能是:
...
JOptionPane.showMessageDialog(optionFrame, "Tests Complete. Screenshots and Results can be found at C://Features");
// do your clean-up work here (probably invoke some clean up service method)
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));