从另一个类中处理 JFrame

发布于 2024-11-30 22:58:12 字数 464 浏览 5 评论 0原文

如何从另一个类中处理 JFrame ?我的代码如下所示。

public class MainWindow

{

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

处置类:

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ま柒月 2024-12-07 22:58:12

建议

JFrame 实例设为 MainWindow 类的字段,并提供访问器 em> 方法。

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;
    }
    .
    .
    .
}

然后在 Dispose 类中,您应该有一个 MainWindow 实例,您只需执行以下操作即可处置其 JFrame 实例:

mainWindowInstance.getMainFrame().dispose();

建议


编辑

这是为了解决您看到的错误:

  1. 变量 main_f 可能尚未初始化
  2. 找不到符号“mainWindowInstance”

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

关于第二个错误,mainWindowInstance 是我留给创建的东西。这是一个“例子”——

public class Disposing{
    private MainWindow mainWindowInstance;

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

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

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:


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();
    }
}
°如果伤别离去 2024-12-07 22:58:12

如果你想像这样访问它,你需要将 main_f 设为静态变量。

但是,这是非对象模式。相反,这样做:

  • 让你的 MainWindow 成为一个单例,
  • 让你的 main_f 成为你的 MainWindow 的一个字段
  • ,调用 MainWindow.getInstance().getFrame().dispose()

另一种方法是给 DisusingJFrame MainWindow 的实例(就像这样 ) ,您不需要将 MainFrame 声明为单例)

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)

昔日梦未散 2024-12-07 22:58:12

有一个最简单的方法可以做到这一点。从另一个类处置一个类的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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文