禁用 JOptionPane 中的 close x
我想禁用 JOptionPane 左上角的关闭 x 我该怎么做?
I would like to disable the close x in the upper left corner of my JOptionPane how would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
迈克尔,
我不知道如何禁用“关闭[x]”按钮。或者,当用户单击它时您可以不执行任何操作。检查下面的代码:
这对您来说合理吗?
Michael,
I don't know how to disable the Close[x] button. Alternatively, you can do nothing when user clicks on it. Check the code below:
Is it reasonable for you?
您可以通过 JOptionPane 中声明的取消按钮覆盖退出按钮,并相应地处理取消操作:
you can overwrite your exit button by a cancel button declared in JOptionPane and handle your cancellation operation accordingly:
当用户尝试关闭对话框而不选择选项时,您始终可以再次显示该对话框。 sun 上有一个如何覆盖默认关闭行为的示例。 com。查看“停止自动对话框关闭”,它们具有以下代码:
使用该代码,您可以轻松地调整注释部分,以仅允许在用户单击可用选项之一而不是关闭按钮时关闭窗口。
You could always just show the dialog again when the user tries to close it without selecting an option. There's an example of how to override the default closing behavior at sun.com. Look under "Stopping Automatic Dialog Closing" and they have the following code:
Using that code, you could easily adapt the commented section to only allow the window to be closed when the user has clicked one of the available options and not the close button.
我不确定是否有办法在 JOptionPane 中执行此操作。
通常,当人们想要比 JOptionPane 提供的更多灵活性(它基本上是一些对话框的一堆静态工厂)时,他们会使用 JDialog 编写自己的对话框。
JDialog 提供了继承的方法
setUndecorated
,它完全消除了 X。这需要更多工作,但您可以使对话框看起来像您想要的那样。I'm not sure if there is a way to do this in
JOptionPane
.Usually when people want more flexibility than JOptionPane offers (it's basically a bunch of static factories for a few dialogs), they write their own dialogs using
JDialog
.JDialog offers the inherited method
setUndecorated
, which eliminates the X altogether. It's more work but you can make your dialog look however you want.