JLayeredPane:深度和位置之间的功能差异是什么?
我正在为学校 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在这种情况下最好的办法是查看教程,它们通常提供非常丰富的信息:http://download.oracle.com/javase/tutorial/uiswing/components/layeredpane.html
另外,该类本身的 javadoc 包含了很好的解释JLayeredPane 有效。
由于您已经实现了您的项目,因此您知道可以通过两种不同的方式实现组件的堆叠:将每个组件放在自己的层上,或者通过为同一层上的不同组件分配不同的位置值。效果是相同的,但是您将使用两个不同的属性来实现它:
现在您可能会争辩说,既然您有“位置”值,那么您根本不需要多个层,因为您可以简单地通过它们的“位置”值沿 z 轴定位所有组件。这是事实,没有人阻止你这样做。
当您意识到有一个预定义常量可用于层的“深度”值时,就可以看出多层的基本原理:
这些只是复杂的多窗口应用程序的逻辑分组,可帮助您创建确保满足一些堆叠约束:假设您想要创建一个出现在主应用程序框架顶部的模式对话框窗口。如果使用单层,则必须自己跟踪所有可见组件的位置并将对话框的位置设置为 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:
n
components in the layer and you start counting at 0, then a position must be a value in the range between 0 andn-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:
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.