2009-05-03 15 views

回答

3

只是延長WindowAdapter,而不是實施WindowListener

你會發現這個概念在所有的Swing聽衆。 (MouseListener/MouseAdapter,KeyListener/KeyAdapter,...)有一個Listener接口和一個Adapter類,它用空方法實現這個接口。

因此,如果您只想對特定事件做出反應,請使用適配器並覆蓋所需的方法。

例子:

setWindowListener(new WindowAdapter() { 
    public void windowClosed(WindowEvent e) { 
     //Cleanup code 
    } 
}); 
+0

這似乎是工作精細。非常感謝。 – Aveen 2009-05-03 09:43:20

1

這裏是你需要的是如何從內FrameView添加ExitListener

private class Closer extends WindowAdapter 
{ 
public void windowClosing(WindowEvent e) 
{ 
int exit = JOptionPane.showConfirmDialog(this, "Are you 
sure?"); 
if (exit == JOptionPane.YES_OPTION) { 
System.exit(0);} 
} 
} 
0

例子:

YourApp.getApplication().addExitListener(new ExitListener() { 

     @Override 
     public boolean canExit(EventObject arg0) { 
      doStuff(); 

      // the return value is used by the application to actually exit or 
      // not. Returning false would prevent the application from exiting. 
      return true; 
     } 
}