2

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

4

2 回答 2

4

这将帮助您:

SwingUtilities.windowForComponent(panel).dispose();

或者

SwingUtilities.windowForComponent(panel).setVisible(false);
于 2013-01-24T18:06:55.423 回答
2

此答案的修改版本

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 );
  }
}
于 2013-01-24T18:08:41.370 回答