如何在Swing java中创建JFrame模态(How to make a JFrame Modal in Swing java)

编程入门 行业动态 更新时间:2024-10-09 06:30:07
如何在Swing java中创建JFrame模态(How to make a JFrame Modal in Swing java)

我已经创建了一个使用JFrame的GUI。 我应该如何让它成为模态?

I have created one GUI in which I have used a JFrame. How should I make it Modal?

最满意答案

你最好的办法是使用JDialog而不是JFrame,如果你想使窗口模态。 查看有关Java 6中Modality API引入的详细信息。 还有一个教程 。

以下是一些示例代码,将在JDialog显示一个JPanel panel ,该Frame parentFrame对于Frame parentFrame是模态的。 除了构造函数,它遵循与打开JFrame相同的模式。

final JDialog frame = new JDialog(parentFrame, frameTitle, true); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true);

编辑:更新的Modality API链接和添加的教程链接(点击@spork的凹凸)。

Your best bet is to use a JDialog instead of a JFrame if you want to make the window modal. Check out details on the introduction of the Modality API in Java 6 for info. There is also a tutorial.

Here is some sample code which will display a JPanel panel in a JDialog which is modal to Frame parentFrame. Except for the constructor, this follows the same pattern as opening a JFrame.

final JDialog frame = new JDialog(parentFrame, frameTitle, true); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true);

Edit: updated Modality API link & added tutorial link (nod to @spork for the bump).

更多推荐

本文发布于:2023-07-09 10:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模态   如何在   Swing   java   JFrame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!