2016-10-14 42 views
1

所以我有以下代碼來創建一個快速彈出框,通知我的用戶程序已經完成運行。一旦用戶點擊確定,我想處理一些額外的事件,特別是我想關閉一些驅動程序和一個sql連接。我怎樣才能做到這一點?添加事件處理到Jframe對話框

下面的代碼,以使得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)); 

回答

0

只要做到這一點,你顯示對話框之後,但this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));

還能去除部分:

optionFrame.toFront(); 
optionFrame.repaint(); 

這條線(消息對話框彈出):

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)); 
+0

你可以給的,將如何做一個例子嗎?我需要一個單獨的方法來做到這一點?通常事件是在單獨的方法中處理的權利? – Jrawr