将 JPanel 置于 java 中其他对象的前面 (SWING)
我想在应用程序处理时发出加载消息,因此我在 JTree
上使用了 JPanel
。但是,当用户单击JPanel
时,JTree
将被选中,并且JPanel
将转到后面。隐藏 JPanel
后,它再也不会显示。我不知道为什么,但它似乎永远不会出现在JTree
前面。
我需要一种方法将 JPanel
放在所有内容的前面。我该怎么做?
编辑:另外我必须提到我不需要JDialog
。我想在任何元素顶部使用 JPanel
来显示加载消息,直到进程完成。
I want to make a loading message when an app processes, so I used a JPanel
over a JTree
. But when the user clicks on the JPanel
, the JTree
will be selected and the JPanel
will go to the back. After hiding the JPanel
, it never shows again. I don't know why, but it seems it never go in front of the JTree
.
I need a way to bring the JPanel
in front of everything. How can I do this?
EDIT: Also I must mention that I don't want a JDialog
. I want to use the JPanel
on top of any element to show a loading message until a process finishes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
所以这里至少有两个解决方案。要么遵循@Geoff 和@sthupahsmaht 的建议。顺便说一句,也可以使用 JOptionPane,它会自动为您创建一个对话框。
另一种选择是使用框架中的 GlassPane。
或者,另一种选择是按照 @jzd 的建议使用 JLayeredPane。
编辑:
示例展示如何使用 GlassPane 捕获用户选择。
尝试以下步骤:
1.左键单击开始时可见的玻璃窗格。查看输出。
2.右键单击它。这隐藏了玻璃板。
3.左键单击内容窗格。查看输出。
4.右键单击它。转到第 1 点。
享受。
编辑2:
如果您想获得对话框的效果,可以通过将此代码适当地合并到我的示例中来实现。
So here you have at least two solutions. Either go with what @Geoff and @sthupahsmaht are suggesting. BTW also possible is to use JOptionPane which automatically creates a dialog for you.
The other option would be to use a GlassPane from a frame.
Or yet another option is to use JLayeredPane as @jzd suggests.
EDIT:
Example showing how to use GlassPane to capture user selections.
Try following steps:
1.Left clicking on the glass pane visible at start. See the output.
2.Right click it. This hides the glass pane.
3.Left clicking on the content pane. See the output.
4.Right click it. Go to point 1.
Enjoy.
EDIT2:
If you want to have an effect of a dialog, you can achieve it by incorporating appropriately this code into my example.
您需要使用
JLayeredPane
用于在彼此前面移动组件。这是一个教程:如何使用分层窗格
You need a to use a
JLayeredPane
for moving components in front of each other.Here is a tutorial: How to use Layered Panes
禁用玻璃窗格可能会帮助您。
Disabled Glass Pane might help you out.
目前还不清楚您的代码是如何组织的。然而,听起来您可能想要的是模式对话框。以下是包含许多参考资源的类似讨论的链接。
如何在 Swing java 中制作 JFrame 模态
It's not really clear how your code is organized. However, it sounds like what you might want is a modal dialog. Here's a link to a similar discussion with a number of referenced resources.
How to make a JFrame Modal in Swing java
使用
JXLayer
或 JIDE可叠加
。Use
JXLayer
or JIDEOverlayable
.此时对象:
at this point the object:
对于那些使用
JDialog
没有问题的人来说,这是在遇到问题时让它显示出来的可靠方法。只要确保在对话框是模态的、配置、设置焦点等时正确控制它即可。For those who have no problem using a
JDialog
, this is a sure way to get it to show up if you're having issues. Just make sure to control it properly if the dialog is modal, when disposing, setting focus etc.