Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中处理和关闭窗口_Java_User Interface_Awt - Fatal编程技术网

在Java中处理和关闭窗口

在Java中处理和关闭窗口,java,user-interface,awt,Java,User Interface,Awt,好吧,这可能是一个愚蠢的问题,但我对Java还不熟悉,在养成任何坏习惯之前,我试图用正确的方式教自己 无论如何,昨晚我正在写一个程序,它由一个自定义类扩展框架和一个自定义类扩展画布组成。main()方法在canvas类中,我在那里创建了frame类的一个实例。问题是,当程序检测到窗口关闭事件时,我无法处理帧,因为我似乎无法从主方法之外访问它。如果我试图在main()之外定义它,那么就不能在main()内使用它。因此,我最终跳过了dispose(),只使用了System.exit(0)。这样行吗?

好吧,这可能是一个愚蠢的问题,但我对Java还不熟悉,在养成任何坏习惯之前,我试图用正确的方式教自己

无论如何,昨晚我正在写一个程序,它由一个自定义类扩展框架和一个自定义类扩展画布组成。main()方法在canvas类中,我在那里创建了frame类的一个实例。问题是,当程序检测到窗口关闭事件时,我无法处理帧,因为我似乎无法从主方法之外访问它。如果我试图在main()之外定义它,那么就不能在main()内使用它。因此,我最终跳过了dispose(),只使用了System.exit(0)。这样行吗?它基本上是在做同样的事情吗?或者这是一个我需要解决的问题,如果是,你知道怎么解决吗

非常感谢你的阅读


科迪不是一个愚蠢的问题。但是,由于垃圾收集器的原因,这并不是一个大问题,有时您需要在窗口关闭时执行一些清理。因此,有一些建议:

应该从框架本身处理窗口关闭事件。例如:

    this.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
                  //code here to perform as the window is about to close.
         }
        });

我建议您为主方法创建一个单独的类来调用框架等。

您可以从事件的
source
属性获取框架的引用:

class MyWindowListener extends WindowAdapter {

    public void windowClosing(WindowEvent e){
         Frame frame = (Frame) e.getSource();
         frame.dispose();
    }

}
或者,由于这是构造函数中声明的匿名类(可能),因此您也可以访问封闭实例,因此您也可以将其编写为:

class MyFrameClass extends Frame {
    public MyFrameClass() {
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                 MyFrameClass.this.dispose();
            }
        });
    }
}
或者您可以使它更简单(因为您的WindowListener没有自己的名为“dispose”的方法):

}

//在父窗口中 @凌驾

 public void windowClosing(WindowEvent arg0) {

        // TODO Auto-generated method stub
        this.frame.disable();
    }

exit(0)终止正在运行的JVM,所以要小心。可能不仅仅是您的应用程序在jvm中运行。谢谢您的解释。这更有意义。我从未想过将getSource的输出转换为一个框架并使用它。感谢您的回复。我想我会尝试使用一个单独的类作为我的主要方法。那样看起来干净多了。
This is used to  close Jframe  with an event handler.


current Jframe 

public class LoginForm  extends JFrame
{

   LoginForm()
   {
      //Some code for Jframe and its components.
      if(Condition)
        disposewindow();
   }




private void disposewindow()
      {
          WindowEvent closingEvent = new WindowEvent(LoginForm.this,
                                                           WindowEvent.WINDOW_CLOSING);
          Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closingEvent);

      }



//you can  can use for alternate of dispose()event  and it post some event handler **Closing event** ,

// if we can use  this closing event to open new window with conditions.

//It means closing child window with closing event, get this flag in main window to make main window as Disable or Enable state
 public void windowClosing(WindowEvent arg0) {

        // TODO Auto-generated method stub
        this.frame.disable();
    }