关闭一个 JFrame 而不关闭另一个? [英] Close one JFrame without closing another?

查看:43
本文介绍了关闭一个 JFrame 而不关闭另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时显示两个(或更多)JFrame.
当我关闭其中一个(使用默认关闭按钮)时,其他帧应该仍然可见.

I want to display two (or more) JFrames at the same time.
When I close one of them (use the default close button), the other frames should still be visible.

我该怎么做?

推荐答案

如果您希望您的应用程序在 JFrame 关闭时终止,请使用

If you do not want your application to terminate when a JFrame is closed, use

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

代替

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

来自文档:

  • DO_NOTHING_ON_CLOSE(在WindowConstants中定义):什么都不做;要求程序在已注册的 WindowListener 对象的 windowClosing 方法中处理操作.
  • HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任何注册的 WindowListener 对象后自动隐藏框架.
  • DISPOSE_ON_CLOSE(在 WindowConstants 中定义):在调用任何注册的 WindowListener 对象后自动隐藏和处理框架.
  • EXIT_ON_CLOSE(在 JFrame 中定义):使用系统退出方法退出应用程序.仅在应用程序中使用它.
  • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
  • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
  • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
  • EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

这是我在澄清问题之前的回答,可能仍然有用:

This was my answer before the question was clarified, might still be useful:

如果您想再次显示同一帧,可以在 JFrame 上使用 setVisible(false).
否则调用 dispose()代码>到删除所有原生屏幕资源.

You can use setVisible(false) on your JFrame if you want to display the same frame again.
Otherwise call dispose() to remove all of the native screen resources.

这篇关于关闭一个 JFrame 而不关闭另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆