如何在JWindow的setVisible之后停止执行代码直到它被关闭?

时间:2014-08-01 08:39:11

标签: java swing jwindow

例如,我的代码如下:

public static void main(String[] args) {
    JWindow w = new JWindow();
    w.setSize(100, 100);
    w.setVisible(true);
    // some code need to be executed after window is closed, Eg.
    System.out.println("some code need to be executed after window is closed");
}

我希望代码在w.setVisible(true);停止,直到窗口关闭/ setVisible(false),然后执行System.out.println("window closed");,如何实现?

2 个答案:

答案 0 :(得分:0)

你可以参考这个link

您可以覆盖windowClosing()方法

public void windowClosing(WindowEvent e) {
  System.out.println("window closed");
}

或windowClosed()

public void windowClosed(WindowEvent e) {
  System.out.println("window closed");
}

根据你正在做的事情,在setVisible(true)开始构造函数中的屏幕截图之后,然后屏幕截图应该告诉你该过程已经完成,这样你就可以继续执行其他的代码了。没有必要暂停代码执行。

答案 1 :(得分:-1)

您可以使用窗口关闭事件或窗口关闭事件

 public void windowClosing(WindowEvent e) 
 {
        System.out.println("window closed");
 }

OR

  public void windowClosed(WindowEvent e) 
  {
         System.out.println("window closed");
  }