如何在mouseDragged事件下的DRAG_LAYER上添加JLabel
我目前正在开发一个拖放应用程序,我真的很想知道 JLayeredPane 内部发生了什么,并且我得到了一个特定的程序行为...
事情是这样的:
我在 DEFAULT_LAYER 上放置了一个棋盘。
我还有一个国际象棋棋子,我想在移动它时将其添加到 DRAG_LAYER 中。
但我有一个迷恋......
使用这一行
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
我只想在 mouseDragged 事件下
。因此,当我这样做时,我的棋子在移动鼠标时消失并隐藏在棋盘后面(?!?)
当我将上面的行更改为:
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER,0);
一切再次正常。
为什么会发生这种情况?
I am currently working on a drag n drop application and I would really like to know what's happening inside the JLayeredPane and I get a particular program behaviour...
Here's the deal:
I have a chessboard placed on the DEFAULT_LAYER.
I also have a chessPiece which I'd like to be added to the DRAG_LAYER when I move it.
But I have a fetish...
I want to use this line
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
only under the mouseDragged event.
So, when I do this, my chessPiece disappears while moving my mouse and gets hidden behind the chessboard (?!?)
When I change the above line into this:
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER,0);
everything becomes normal again.
Why is that happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用
layeredPane.moveToFront(component)
而不是依赖index
参数。请注意,
add(Component comp, Object Constraints, int Index)
是由java.awt.Container
实现的,而不是javax.swing.JLayeredPane
。I use
layeredPane.moveToFront(component)
instead of relying on theindex
parameter.Note that the
add(Component comp, Object constraints, int index)
is implemented byjava.awt.Container
, notjavax.swing.JLayeredPane
.您必须使用 GlassPane。在开始翻译标签之前,您必须在 MouseDragged 事件中使用 GlassPane.Add(label) 和 label.setLocation(x,y)。侦听器必须是 MouseMotionListener,而不是普通的 MouseListener。
You must use GlassPane. Before you start to translate the label you must use GlassPane.Add(label) and label.setLocation(x,y) inside the MouseDragged event. The listener must be MouseMotionListener and not the plain MouseListener.
问题是这两种方法有什么区别?如果你阅读 API,你就会发现差异。在第一种情况下,组件被添加到容器的“末端”。在第二种情况下,组件被添加到容器中的“指定位置”。
由于只有一个组件添加到 DragLayer 中,因此从理论上讲,使用哪种添加方法应该没有什么区别。但是,由于您在 DefaultLayer 上也有一个组件,因此可能会有所不同。唯一确定的方法是查看源代码。
然而,正如您在其他帖子中已经建议的那样。此代码不应在 mouseDragged() 事件中完成。当生成多个拖动事件时,不断地将棋子“添加”到拖动布局中是没有意义的。您所需要做的就是随着鼠标的移动改变棋子的位置。这就是为什么使用 mousePressed() 事件将棋子添加到拖动层的原因。
The question is what is the difference between the two methods? Well if you read the API you will find the difference. In the first case the component is added at the "end" of the container. In the second case the component is added at the "specified position" in the container.
Since there is only one component added to the DragLayer, theorectically it shouldn't make a difference which add method is used. However, because you also have a component on the DefaultLayer, it may may a difference. The only way to know for sure is to look at the source code.
However as has already been suggested in your other posting. This code should not be done in the mouseDragged() event. As multiple drag events are generated, it does not make sense to continually "add" the chess piece to the drag layout. All you need to do is change the location of the chess piece as the mouse is moved. That is why the mousePressed() event is used to add the chess piece to the drag layer.
我还使用了上述诊断,发生的情况如下:
int number = layeredPane.getLayer(chessPiece);
int number3 = layeredPane.getComponentCountInLayer(JLayeredPane.DRAG_LAYER);
拖动时,第一个始终返回数字 400,这是拖动层的表示法。
但
另一个一开始返回 1,但当我们拖动时,它全部变成零...
这意味着至少在一段时间内,棋子实际上进入了拖动层,然后掉了下来。
但如果你问窗格,就会发生这种情况……
这有帮助吗?
另外,您介意给我一个 jlayeredpane 源代码的链接吗?
我正在寻找 JLayeredPane.java
但到目前为止还没有运气...
提前谢谢...
I have also used the above diagnostic and here's what happens:
int number = layeredPane.getLayer(chessPiece);
int number3 = layeredPane.getComponentCountInLayer(JLayeredPane.DRAG_LAYER);
While dragging, the first one always returns the number 400 which is the notation for the drag layer.
BUT
the other one returns a 1 at first but then it all become zeros as we drag...
This means that at least for an instance of time, the chesspiece actually GOT into the drag layer and then fell off.
But that's what happens if you ask the pane...
Does this help at all?
Also, would you mind giving me a link of the jlayeredpane source code?
I'm looking for JLayeredPane.java
but had no luck till now...
Thanx in advance...