java 从另一个类处理 JFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7122349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:43:16  来源:igfitidea点击:

Disposing JFrame from another class

javaswingjframedispose

提问by Makaroni

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

我怎样才能JFrame从另一个班级处置?我的代码如下。

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_fisn't a variable. Can you help me?

MainWindow.main_f.dispose()不会工作,因为main_f不是变量。你能帮助我吗?

回答by mre

Suggestion:

建议

Make the JFrameinstance a fieldof the MainWindowclass, and provide an accessormethod for it.

使JFrame实例字段中的MainWindow类,并提供访问它的方法。

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 Disposingclass, you should have a MainWindowinstance, where you'll simply do the following to dispose of its JFrameinstance:

然后在Disposing类中,您应该有一个MainWindow实例,您只需在其中执行以下操作来处理其JFrame实例:

mainWindowInstance.getMainFrame().dispose();

Recommendation:

推荐



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"
  1. 变量 main_f 可能尚未初始化
  2. 找不到符号“mainWindowInstance”

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

关于第一个错误,这是因为在我提供的示例中,我使用了final修饰符。该字段必须在对象创建时初始化。因此,您必须拥有多个构造函数。要解决此问题,请移除final修饰符,或main_fMainWindow.

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

关于第二个错误,mainWindowInstance是我留给创造的东西。这是一个“例如”-

public class Disposing{
    private MainWindow mainWindowInstance;

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

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

回答by Jerome Cance

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

如果你想像这样访问它,你需要让 main_f 成为一个静态变量。

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()
  • 使您的 MainWindow 成为单例
  • 使 main_f 成为 MainWindow 的字段
  • 调用 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)

另一种方法是为 DisposingJFrame 提供 MainWindow 的实例(这样,您不需要将 MainFrame 声明为单例)

回答by Muhammad Umair

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

有一种最简单的方法可以做到这一点。从另一个类处理一个类的JFrame的Java代码如下:

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

public class Other extends JFrame
{
   static Main a;

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