[英]Disposing JFrame from another class
How can I dispose JFrame
from another class?如何从另一个 class 处理
JFrame
? 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:处理 class:
public static void DisposingJFrame (){
.
.
.
MainWindow.main_f.dispose();
}
MainWindow.main_f.dispose()
won't work because main_f
isn't a variable. MainWindow.main_f.dispose()
不起作用,因为main_f
不是变量。 Can you help me?你能帮助我吗?
Suggestion :建议:
Make the JFrame
instance a field of the MainWindow
class, and provide an accessor method for it.将
JFrame
实例设为MainWindow
class 的字段,并为其提供访问器方法。
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
class 中,您应该有一个MainWindow
实例,您只需在其中执行以下操作即可处理其JFrame
实例:
mainWindowInstance.getMainFrame().dispose();
Recommendation :建议:
Edit :编辑:
This is to address the errors that you're seeing:这是为了解决您看到的错误:
With regard to the first error, this is because in the example I provided, I used the final
modifier.关于第一个错误,这是因为在我提供的示例中,我使用了
final
修饰符。 This field must be initialized upon object creation.此字段必须在 object 创建时初始化。 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
修饰符,或在MainWindow
的每个构造函数中初始化main_f
字段。
With regard to the second error, mainWindowInstance
is something that I left for you to create.关于第二个错误,
mainWindowInstance
是我留给您创建的。 Here's a "for instance" -这是一个“例如” -
public class Disposing{
private MainWindow mainWindowInstance;
public Disposing(){
mainWindowInstance = new MainWindow();
.
.
.
}
public void diposeMainFrame(){
mainWindowInstance.getMainFrame().dispose();
}
}
There is a simplest way of doing this.有一种最简单的方法可以做到这一点。 The Java code for disposing the JFrame of a class from another class is as follows:
用于从另一个 ZA2F2ED4F8EBC2CBB64C21A29DZ 处理 class 的 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();
}
}
you need to make main_f to be a static variable if you want to access it like this.如果你想像这样访问它,你需要让 main_f 成为一个 static 变量。
BUT, this is non object pattern.但是,这是非 object 模式。 Instead of that, do this:
取而代之的是,这样做:
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 声明为单例)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.