关闭JPanel的父元素

时间:2013-01-24 18:04:55

标签: java swing jframe jpanel swingutilities

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

2 个答案:

答案 0 :(得分:4)

这将对您有所帮助:

SwingUtilities.windowForComponent(panel).dispose();

SwingUtilities.windowForComponent(panel).setVisible(false);

答案 1 :(得分:2)

this answer的修改版

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