Disposing JFrame from another class

ⅰ亾dé卋堺 提交于 2019-12-08 03:09:53

问题


How can I dispose JFrame from another class? My code is listed below.

public class MainWindow

{

JFrame main_f = new JFrame("xx");
main_f.getContentPane().setLayout(new BorderLayout());
main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
.
.
.
}

Disposing class:

public static void DisposingJFrame (){
.
.
.
MainWindow.main_f.dispose();
}

MainWindow.main_f.dispose() won't work because main_f isn't a variable. Can you help me?


回答1:


Suggestion:

Make the JFrame instance a field of the MainWindow class, and provide an accessor method for it.

public final class MainWindow{
    private final JFrame main_f;

    public MainWindow(){
        main_f = new JFrame("xx");
        main_f.getContentPane().setLayout(new BorderLayout());
        main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        .
        .
        .
    }

    public final JFrame getMainFrame(){
        return main_f;
    }
    .
    .
    .
}

And then in the Disposing class, you should have a MainWindow instance, where you'll simply do the following to dispose of its JFrame instance:

mainWindowInstance.getMainFrame().dispose();

Recommendation:

  • Read the Learning the Java Language tutorial. This is basic OOP.

Edit:

This is to address the errors that you're seeing:

  1. variable main_f might not have been initialized
  2. cannot find symbol "mainWindowInstance"

With regard to the first error, this is because in the example I provided, I used the final modifier. This field must be initialized upon object creation. Therefore, you must have more than one constructor. To resolve this, either remove the final modifier, or initialize the main_f field in every constructor of MainWindow.

With regard to the second error, mainWindowInstance is something that I left for you to create. Here's a "for instance" -

public class Disposing{
    private MainWindow mainWindowInstance;

    public Disposing(){
        mainWindowInstance = new MainWindow();
        .
        .
        .
    }

    public void diposeMainFrame(){
        mainWindowInstance.getMainFrame().dispose();
    }
}



回答2:


you need to make main_f to be a static variable if you want to access it like this.

BUT, this is non object pattern. Instead of that, do this :

  • make your MainWindow to be a singleton
  • make your main_f a field of your MainWindow
  • call MainWindow.getInstance().getFrame().dispose()

Another way is to give to DisposingJFrame the instance of MainWindow (like that, you don't need to declare MainFrame as a singleton)




回答3:


There is a simplest way of doing this. The Java code for disposing the JFrame of a class from another class is as follows:

public class Main extends JFrame
{
  Main()
  {
    Main x=new Main();
    Other.a=x;
  }
}

public class Other extends JFrame
{
   static Main a;

   Other()
   {  
      a.dispose();
    }
}


来源:https://stackoverflow.com/questions/7122349/disposing-jframe-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!