我有一个主 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/