是否可以用 X(红色 x 向右和向上)关闭 Java 中的 JFrame 并且不关闭整个程序?

Is it possible to close a JFrame in Java with X ( red x right and up ) and don't close the whole program?

是否可以在 Java 中用 X(红色 x 向右和向上)关闭一个 JFrame 并且不关闭整个程序(只关闭这个 Jframe)?

非常感谢。

你可以设置f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 并且 JFrame 将在按 x 时处理,如果它符合您的条件。

有关更多信息,您可以查看 JFrame

documentation

您可以使用 JFrame 中的以下方法 class:

setDefaultCloseOperation(int)

根据文档:

设置当用户在此帧上发起"close"时默认发生的操作

可能的值:

JFrame.EXIT_ON_CLOSE — Exit the application.
JFrame.HIDE_ON_CLOSE — Hide the frame, but keep the application running.
JFrame.DISPOSE_ON_CLOSE — Dispose of the frame object, but keep the application running.
JFrame.DO_NOTHING_ON_CLOSE — Ignore the click.