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
Disposing JFrame from another class
提问by Makaroni
How can I dispose JFrame
from 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_f
isn't a variable. Can you help me?
MainWindow.main_f.dispose()
不会工作,因为main_f
不是变量。你能帮助我吗?
回答by mre
Suggestion:
建议:
Make the JFrame
instance a fieldof the MainWindow
class, 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 Disposing
class, you should have a MainWindow
instance, where you'll simply do the following to dispose of its JFrame
instance:
然后在Disposing
类中,您应该有一个MainWindow
实例,您只需在其中执行以下操作来处理其JFrame
实例:
mainWindowInstance.getMainFrame().dispose();
Recommendation:
推荐:
- Read the Learning the Java Languagetutorial. This is basic OOP.
- 阅读学习 Java 语言教程。这是基本的面向对象编程。
Edit:
编辑:
This is to address the errors that you're seeing:
这是为了解决您看到的错误:
- variable main_f might not have been initialized
- cannot find symbol "mainWindowInstance"
- 变量 main_f 可能尚未初始化
- 找不到符号“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
.
关于第一个错误,这是因为在我提供的示例中,我使用了final
修饰符。该字段必须在对象创建时初始化。因此,您必须拥有多个构造函数。要解决此问题,请移除final
修饰符,或main_f
在MainWindow
.
With regard to the second error, mainWindowInstance
is 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();
}
}