JLayeredPane:深度和位置之间的功能差异是什么?

发布于 2024-10-04 07:48:59 字数 726 浏览 3 评论 0原文

我正在为学校 Java 项目开发四子棋游戏。我已经了解了 JLayeredPane 的“如何”,并且它按预期工作,但我不太明白某个概念背后的“原因”。

我的理解是:

JLayeredPane是一个容器,类似于JPanel,它允许您指定每个组件的深度和位置。 Depth 是一个 Integer,0 是底层,n-1 是顶层,n 是组件的数量。 Position 是一个 int (是的,一个使用 Integer 包装类,一个只是一个原语!),它指定组件在层中的位置,0 是最顶层,-1 是最底层,正整数为之间,数字越小,位置越高。因此,单层中的四个组件可以从最顶层到最底层排序到位置 0、1、2、-1。

我的问题是,什么需要同时拥有这两个概念?

例如,我创建了三个带有图像的 JLabel:一个 frontBoard、一个 backBoard 和一个piece。该棋子有时位于前板前面,有时位于前板和后板之间。让我们看一下第二个例子。

我可以通过以下任一方式获得相同的效果:

1)我可以将backBoard设置为第0层,位置0;块到第 1 层,位置 0;将 frontBoard 设置为第 2 层,位置 0

 or 

2) 我可以将 backBoard 设置为第 0 层,位置 -1;块到第 0 层,位置 1;和frontBoard到第0层,第0位置

我已经测试了这两种方法,并且我找不到这两种方法之间的功能差异。

谁能为我解开这个谜团?

I am working on a Connect Four game for a school Java project. I've got the 'how' of the JLayeredPane, and it is working as expected, but I'm not quite getting the 'why' behind a certain concept.

Here is my understanding:

JLayeredPane is a container, similar to JPanel, that allows you to specify depth and position for each component. Depth is an Integer, with 0 being the bottom layer and n-1 being the top layer, with n being the number of components. Position is an int (yes, one uses the Integer wrapper class and one is just a primitive!) that specifies a component's postion within the layer, with 0 being the topmost layer, -1 being the bottom-most layer, and positive ints in between, the lower the number the higher the position. So, four components in a single layer could be ordered into position 0, 1, 2, -1 from topmost to bottom-most.

My question is, what's the need to have both concepts?

For instance, I have created three JLabels with images: a frontBoard, a backBoard, and a piece. The piece sometimes goes in front of the frontBoard and sometimes goes between the frontBoard and the backBoard. Let's examine the second example.

I can get the same effect through either of these means:

1) I can set the backBoard to layer 0, position 0; the piece to layer 1, position 0; and the frontBoard to layer 2, position 0

 or 

2) I can set the backBoard to layer 0, position -1; the piece to layer 0, position 1; and the frontBoard to layer 0, position 0

I have tested both of these ways, and I can't find a functional difference between the two methods.

Can anyone shed any light on this mystery for me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2024-10-11 07:48:59

首先,在这种情况下最好的办法是查看教程,它们通常提供非常丰富的信息:http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

另外,该类本身的 javadoc 包含了很好的解释JLayeredPane 有效。

由于您已经实现了您的项目,因此您知道可以通过两种不同的方式实现组件的堆叠:将每个组件放在自己的层上,或者通过为同一层上的不同组件分配不同的位置值。效果是相同的,但是您将使用两个不同的属性来实现它:

  • “深度”图层的属性:它们被枚举,以便具有深度的图层0 是最远最低的。深度较高的层覆盖深度较低的层。
  • 由于层可以包含多个组件,并且组件总是可以重叠,因此必须有一种方法来定义层内组件的 z 顺序:这是通过使用 枚举组件来实现的。 em>“位置”值。如果层中有 n 个组件并且从 0 开始计数,则位置必须是 0 到 n-1 之间的值。

现在您可能会争辩说,既然您有“位置”值,那么您根本不需要多个层,因为您可以简单地通过它们的“位置”值沿 z 轴定位所有组件。这是事实,没有人阻止你这样做。

当您意识到有一个预定义常量可用于层的“深度”值时,就可以看出多层的基本原理:

  • DEFAULT_LAYER
  • PALETTE_LAYER
  • MODAL_LAYER
  • POPUP_LAYER
  • DRAG_LAYER

这些只是复杂的多窗口应用程序的逻辑分组,可帮助您创建确保满足一些堆叠约束:假设您想要创建一个出现在主应用程序框架顶部的模式对话框窗口。如果使用单层,则必须自己跟踪所有可见组件的位置并将对话框的位置设置为 n。现在添加拖放动画、弹出菜单等,这个任务变得相当复杂。

通过使用预定义的层,可以降低复杂性。如果您想显示模式对话框窗口,则不必关心主应用程序窗口的组件,只需将其放置在 MODAL_LAYER 上即可:您可以确定它显示在所有其他组件之上。

幸运的是,Swing 已经为您完成了所有这些工作(通过在内部使用 JLayeredPane 或其子类),因此您只需在 JDialog 上调用 setVisible(boolean) 或 setModal(boolean) 即可,它就会按照您期望的方式显示。

First off, the best thing to do in such a case is have a look at the tutorial, they are usually very informative: http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

Also, the javadoc of the class itself contains quite a good explanation of the way JLayeredPane works.

Since you already implemented your project, you know that you can achieve stacking of components in two different ways: putting each component on its own layer, or by assigning different components that are on the same layer different position values. The effect is the same, but you would use two different properties to achieve it:

  • "depth" is a property of the layer: they are enumerated so that the layer with depth 0 is furthest the lowest of all. Layers with higher depths cover layers with lower depths.
  • Since layers can contain more than one component and since components can always overlap, there must be a way to define the z-order of the components within the layer: that is achieved by enumerating the components with a "position" value. If there are n components in the layer and you start counting at 0, then a position must be a value in the range between 0 and n-1.

Now you could argue that since you have the "position" value, you don't need multiple layers at all, since you can position all components along the z-axis simply through their "position" value. That is true and nobody keeps you from doing so.

The rationale for multiple layers can be seen when you realize that there a predefined constants to be used for the "depth" value of the layers:

  • DEFAULT_LAYER
  • PALETTE_LAYER
  • MODAL_LAYER
  • POPUP_LAYER
  • DRAG_LAYER

These are are just logical groupings for complex multi-window applications that help you make sure that some stacking constraints are met: imagine you want to create a modal dialog window that appears on top of your main application frame. If you use a single layer, you must keep track of the positions of all visible components yourself and set the position of the dialog to n. Now add in drag and drop animations, popup menus, etc. and this task becomes quite complex.

By using predefined layers this complexity is reduced. If you want to display a modal dialog window, you don't care about the components of the main application window, you just place it on the MODAL_LAYER and you're done: you can be certain that it's displayed on top of all other components.

Luckily, Swing does all this stuff for you already (by using JLayeredPane or subclasses thereof internally), so you can just call setVisible(boolean) or setModal(boolean) on a JDialog and it will turn out the way you expect it to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文