问题描述
我将 MigLayout 库用于 Swing GUI.
I am using the MigLayout library for a Swing GUI.
我想覆盖两个 JPanel,如下所示:
I would like to overlay two JPanel, as so:
+----+-----------------------+
| 1 | 2 |
| | |
+----+ |
| |
| |
| |
| |
+----------------------------+
很像视频游戏中的小地图.
在我看来,有两种方法可以实现这一目标:
It seems to me there are two ways to achieve this:
JLayeredPane
JPanel main = new JPanel();
main.setBounds(0, 0, WIDTH, HEIGHT);
// ... fill it with Swing components ...
JPanel minimap = new JPanel();
minimap.setBounds(10, 10, 100, 100);
/// ... fill it with Swing components ...
JLayeredPane layer = new JLayeredPane();
layer.add(main, new Integer(0));
layer.add(minimap, new Integer(1));
然而,这种方法很脆弱,因为我必须指定WIDTH
和HEIGHT
.如果调整窗口大小,那么我似乎必须再次计算这些值.
This approach, however, is flimsy because I have to specify WIDTH
and HEIGHT
. If the window is resized, then it seems like I have to calculate these values again.
JLayeredPane &MigLayout
JPanel main = new JPanel();
// ... fill it with Swing components ...
JPanel minimap = new JPanel();
/// ... fill it with Swing components ...
JLayeredPane layer = new JLayeredPane();
layer.setLayoutManager(new MigLayout());
CC positioning;
positioning = // ??? something specified in percents and z-order?
layer.add(main, positioning, new Integer(0));
positioning = // ??? something specified in percents and z-order?
layer.add(minimap, positioning, new Integer(1));
是否可以使用允许使用 MigLayout 进行叠加的内容填充定位代码,同时让我指定百分比值(而不是像素分辨率 WIDTH
和 HEIGHT
在第一个例子中)?
Is it possible to fill in the positioning code with something that allows for overlays using MigLayout while letting me specify percent values (as opposed to the pixel resolution WIDTH
and HEIGHT
in the first example)?
编辑:
这是一个部分有效的解决方案:
Here is a solution that partially works:
positioning = new CC().width("100%").height("100%");
// ...
positioning = new CC().pos("10", "10");
但是,显示抖动,有时不可见.我相信这与不确定的渲染顺序有关(尽管这只是直觉).
However, the display jitters and sometimes is invisible. I believe this has to do with an indeterminate rendering order (although that's just intuition).
推荐答案
我不知道 MigLayout 在幕后如何工作,但我认为 ZOrder 很重要.
I don't know how MigLayout works behind the scenes but I would think ZOrder is important.
查看使用 ZOrder 进行布局的 重叠布局组件,并且对我对 ZOrder 的工作方式的理解有一点模糊.
Check out the Overlap Layout which uses ZOrder for laying out components and has a little blurb on my understanding of how ZOrder works.
也许文章中提到的 isOptimizedDrawingEnabled() 方法在这种情况下也很重要,
Maybe the isOptimizedDrawingEnabled() method, as mentioned in the article, is also important is this case,
这篇关于是否可以在 MigLayout 中指定深度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!