【问题标题】:Is it possible to specify depth in MigLayout?是否可以在 MigLayout 中指定深度?
【发布时间】:2013-02-08 18:54:15
【问题描述】:

我正在将 MigLayout 库用于 Swing GUI。

我想叠加两个JPanel,如下:

+----+-----------------------+
| 1  |         2             |
|    |                       |
+----+                       |
|                            |
|                            |
|                            |
|                            |
+----------------------------+

很像电子游戏中的minimap


在我看来有两种方法可以实现这一点:

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));

但是,这种方法很脆弱,因为我必须指定 WIDTHHEIGHT。如果调整了窗口大小,那么似乎我必须再次计算这些值。

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 进行叠加的内容,同时让我指定百分比值(与第一个示例中的像素分辨率 WIDTHHEIGHT 相对)?

编辑

这是一个部分有效的解决方案:

positioning = new CC().width("100%").height("100%");
// ...
positioning = new CC().pos("10", "10");

但是,显示抖动,有时是不可见的。我相信这与不确定的渲染顺序有关(尽管这只是直觉)。

【问题讨论】:

  • 我也试过setComponentZOrder,但是好像没有任何效果。

标签: java swing user-interface miglayout


【解决方案1】:

我也试过setComponentZOrder,但是好像没有任何效果

我不知道 MigLayout 在幕后是如何工作的,但我认为 ZOrder 很重要。

查看Overlap Layout,它使用 ZOrder 来布置组件,并且对我对 ZOrder 工作原理的理解有一些了解。

也许文章中提到的 isOptimizedDrawingEnabled() 方法在这种情况下也很重要,

【讨论】:

  • Robert D. Camick,你解决了我的问题!我必须重写 isOptmizedDrawingEnabled 以返回 false,并首先添加小地图。
猜你喜欢
  • 2013-08-01
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 2021-08-10
  • 2021-12-28
  • 2012-11-27
  • 2018-12-04
  • 2023-03-24
相关资源
最近更新 更多