java - 关闭嵌入在主 JFrame 中的特定 JFrame

标签 java swing jframe

我有一个主 JFrame,其中嵌入了其他 JFrame,单击主 JFrame 上的按钮即可打开该 JFrame。内嵌的JFrame有以下代码需要关闭:

 public void windowClosing(WindowEvent we)
{
System.exit(0);
}

但问题是一旦我关闭嵌入式 JFrame,主 JFrame 也会随之关闭。我只想关闭嵌入式 JFrame。谁能帮我解决这个问题吗?其次,是

   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   public void windowClosing(WindowEvent we)
  {
    System.exit(0);
  }

关闭框架更好吗?

最佳答案

要关闭嵌入式 JFrame,请使用 dispose() 函数。

 public void windowClosing(WindowEvent we)
{
    dispose();
}

或使用

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

dispose()

Releases all of the native screen resources used by this Window, its subcomponents, and all of its owned children. That is, the resources for these Components will be destroyed, any memory they consume will be returned to the OS, and they will be marked as undisplayable.

The Window and its subcomponents can be made displayable again by rebuilding the native resources with a subsequent call to pack or show. The states of the recreated Window and its subcomponents will be identical to the states of these objects at the point where the Window was disposed (not accounting for additional modifications between those actions).

注意:

When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate.

关于java - 关闭嵌入在主 JFrame 中的特定 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21835581/

相关文章:

java - 关闭当前窗口并弹出上一个窗口(windowListener)

Java JPanel 平铺背景图像

java - Spring 3.0.3 和 JMS 2.0 支持

java - 跳过 66 帧!应用程序可能在其主线程上做了太多工作

c# - 使用java或其他编程语言将小图标添加到任何文件/文件夹作为Windows中的图标

java - 如何调整单个 JTable 列的大小而不影响其他表列

java - 使用多线程打印奇数和偶数

Java JPanel 子类忽略添加组件的界限

java - 在不重新绘制树的情况下更新 JTree 中的 ImageIcon?

Java JButton 不会遍历容器中的 JLabel(带有图像)