Java Swing - 如何在另一个面板之上显示一个面板?
我希望有一个内部(非窗口)对话框来请求成员输入。 我希望将对话框集中放置在现有 JPanel 上。
我看过 layeredpanes ,这些似乎无法使用,因为所有窗格中只有一个布局管理器(或没有布局管理器)。 我想我可以尝试覆盖 JLayeredPane 并提供自定义布局,但这似乎很极端。
玻璃窗格似乎也不合适。
如何才能做到这一点? Swing 中没有可用的 z 索引概念吗?
编辑
分层窗格不合适的原因是每层缺少布局管理器。 面板大小可调整,面板 A 应保持在 100% 区域,面板 B 应保持居中。
I wish to have an internal (non window) dialog to ask for member input. I would like the dialog to be placed centrally on an existing JPanel.
I have looked at layeredpanes and these seem unusable due to only having a single layout manager (or no layout manager) across all the panes. I guess I could try to override JLayeredPane and provide a custom layout but this seems extreme.
Glass panes don't seem to be appropriate either.
How can this be done? Is there no usable concept of z-indexes in Swing?
EDIT
The reason Layered Panes weren't appropriate was due to the lack of a layout manager per layer. The panel is resizeable, Panel A should stay at 100% of area and Panel B should stay centralized.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为 LayeredPane 是你最好的选择。 不过,您将需要第三个面板来包含 A 和 B。这第三个面板将是 LayeredPane,然后面板 A 和 B 仍然可以有一个很好的 LayoutManager。 您所要做的就是将 B 放在 A 的中心,Swing 路径中有很多关于如何做到这一点的示例。 不使用 LayoutManager 进行定位的教程。
您可以使用 setBounds 将面板定位在分层窗格内,并设置它们的大小。
编辑以反映对原始帖子的更改
您需要添加组件侦听器来检测父容器何时调整大小,然后动态更改面板 A 和 B 的边界。
I think LayeredPane is your best bet here. You would need a third panel though to contain A and B. This third panel would be the layeredPane and then panel A and B could still have a nice LayoutManagers. All you would have to do is center B over A and there is quite a lot of examples in the Swing trail on how to do this. Tutorial for positioning without a LayoutManager.
You use setBounds to position the panels inside the layered pane and also to set their sizes.
Edit to reflect changes to original post
You will need to add component listeners that detect when the parent container is being resized and then dynamically change the bounds of panel A and B.
您可以像这样添加一个未修饰的 JDialog:
You can add an undecorated JDialog like this:
我只是想补充一点,Swing 中有一个 Z 顺序的概念,请参阅
[java.awt.Component#setComponentZOrder][1]
影响组件在其父组件数组中的位置,从而确定绘制顺序。
请注意,您应该覆盖 javax. swing.JComponent#isOptimizedDrawingEnabled 在父容器中返回 false 以使重叠组件正确重绘,否则它们的重绘将互相破坏。 (JComponent 假定没有重叠子项,除非 isOptimizedDrawingEnabled 返回 false)
[1]: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html#setComponentZOrder(java.awt.Component,整数)
I just thought that I'd add that there is a notion of Z-order in Swing, see
[java.awt.Component#setComponentZOrder][1]
which affects the positions of a component in its parents component array, which determines the painting order.
Note that you should override javax.swing.JComponent#isOptimizedDrawingEnabled to return false in the parent container to get your overlapping components to repaint correctly, otherwise their repaints will clobber each other. (JComponents assume no overlapping children unless isOptimizedDrawingEnabled returns false)
[1]: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Container.html#setComponentZOrder(java.awt.Component, int)
在现有 JPanel 上使用 1 x 1 GridLayout,然后将您的面板添加到该 JPanel。 1 x 1 的 GridLayout 的唯一问题是您无法在 JPanel 上放置其他项目。 在这种情况下,您必须找出合适的布局。 您使用的每个面板都可以使用自己的布局,因此这不会成为问题。
我对这个问题的理解正确吗?
Use a 1 by 1 GridLayout on the existing JPanel, then add your Panel to that JPanel. The only problem with a GridLayout that's 1 by 1 is that you won't be able to place other items on the JPanel. In this case, you will have to figure out a layout that is suitable. Each panel that you use can use their own layout so that wouldn't be a problem.
Am I understanding this question correctly?
JOptionPane.showInternalInputDialog 可能会满足您的需求。 如果没有,那么了解它缺少什么将会很有帮助。
JOptionPane.showInternalInputDialog probably does what you want. If not, it would be helpful to understand what it is missing.
这里可能还有另一个布局管理器:覆盖布局
这是一个简单的教程: OverlayLayout:用于相互叠置的组件的布局管理。
覆盖布局允许您将面板放置在另一个面板的顶部,并在每个面板内设置布局。 您还可以单独设置每个面板的大小和位置。
(我几年前回答这个问题的原因是因为我偶然发现了同样的问题,而且我在 StackOverflow 上看到很少提及 Overlay Layout,虽然它似乎有很好的文档记录。希望这可以帮助某人其他人继续寻找它。)
There's another layout manager that may be of interest here: Overlay Layout
Here's a simple tutorial: OverlayLayout: for layout management of components that lie on top of one another.
Overlay Layout allows you to position panels on top of one another, and set a layout inside each panel. You are also able to set size and position of each panel separately.
(The reason I'm answering this question from years ago is because I have stumbled upon the same problem, and I have seen very few mentions of the Overlay Layout on StackOverflow, while it seems to be pretty well documented. Hopefully this may help someone else who keeps searching for it.)