简体   繁体   English

如何以不同的方法从子类关闭 JFrame?

[英]How to close a JFrame from a subclass in a different method?

I have a button in my GUI that is supposed to close its window.我的 GUI 中有一个按钮应该关闭它的窗口。 However, its event handler is located in an AbstractAction subclass that is located in a seperate method from the JFrame .但是,它的事件处理程序位于一个AbstractAction子类中,该子类位于与JFrame方法中。 Because of this I cannot see a way to tell the JFrame to close from my AbstractAction .因此,我看不到告诉JFrame从我的AbstractAction关闭的方法。

Heres the basic layout of my code:这是我的代码的基本布局:

public PointWindow()
  {
    initialize();
  }

  public void initialize()
  {
    JFrame frame = new JFrame();
    // JFrame stuff
    frame.setContentPane(createGUI());
    frame.setVisible(true);
  }

  public JPanel createGUI()
  {
    JPanel gui = new JPanel();

    // Code....

    class MakeGraphACT extends AbstractAction 
    {
      public void actionPerformed(ActionEvent e)
      {
        frame.setVisible(false);  // <--- How to get this to work?
        frame.dispose();          // <---
        new GraphWindow(pointList);
      }
    }

    //Code...

    return gui;
  }

Have I done a bad job planning my code or am I just missing something obvious?我是否在规划代码时做得不好,还是我只是遗漏了一些明显的东西?

You need to pass the frame as a final parameter to the createGUI() method.您需要将框架作为final参数传递给createGUI()方法。
You will then be able to access it from within the inner class.然后,您将能够从内部类中访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM