Java 关闭JPanel的父元素

Java 关闭JPanel的父元素,java,swing,jframe,jpanel,swingutilities,Java,Swing,Jframe,Jpanel,Swingutilities,是否可以关闭组件的父元素。。可能是来自JPanel的JFrame/JInternalFrame?如果是,怎么做 这将帮助您: SwingUtilities.windowForComponent(panel).dispose(); 或 修订版 SwingUtilities.windowForComponent(panel).setVisible(false); public class CustomPanel extends JPanel { private JFrame parent;

是否可以关闭组件的父元素。。可能是来自JPanel的
JFrame
/
JInternalFrame
?如果是,怎么做

这将帮助您:

SwingUtilities.windowForComponent(panel).dispose();

修订版

SwingUtilities.windowForComponent(panel).setVisible(false);
public class CustomPanel extends JPanel {

  private JFrame parent; //Need to pass this reference somehow, constructor or otherwise

  public void closeJFrame() {
    WindowEvent winEvent = new WindowEvent(parent, WindowEvent.WINDOW_CLOSING);
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winEvent );
  }
}