使用鼠标事件在 JPanel 中拖动 JLabel
我使用 MouseInputListener (MouseListener + MouseMotionListener) 在 JPanel 中拖放多个 JLabel。这是我所做工作的概述;
MouseClicked:检查是否有 单击区域内的 JLabel J面板;如果是,请选择它(将其绘制到 颜色等)。如果没有,则什么都不做。
MouseDragged:如果选择了 JLabel, 使用 setLocation 来设置 JLabel 事件的 e.getX() 和 e.getY()。如果 未选择任何内容,也不执行任何操作。
MouseReleased:如果 JLabel 是 选定后,将其绘制回原来的状态 原来的颜色。不选择任何内容(也许 无效的)。如果没有,则什么都不做。
这些都在JPanel中; JPanel 实现了 MouseInputListener。
所以问题是:当 JLabel 的初始位置为 0,0 时,假设我将其移动到 10,10。释放鼠标后,没有选择任何内容,当我单击 0,0 时,它会选择该 JLabel;但是,如果我单击 10,10,它应该选择它,因为这是它的新位置。
现在我认为这可能是因为我使用了错误的坐标;我听说JPanel中的坐标值是相对的,所以我每次都必须做减法(即最终初始坐标)才能得到正确的坐标。我做到了,但也没有成功。另一种可能性可能是 Java 存储了所有历史 X 和 Y 坐标(这样每次我单击前一个坐标时,我都会选择该对象),这纯粹是想象!
您有什么建议?
提前致谢。
I'm using the MouseInputListener (MouseListener + MouseMotionListener) to drag and drop multiple JLabels in a JPanel. Here is an outline of what I do;
MouseClicked: check if there is any
JLabel within the clicked area of
JPanel; if yes, select it (paint it to
a color,etc). If not, do nothing.MouseDragged: If a JLabel is selected,
setLocation of that JLabel using
e.getX() and e.getY() of the event. If
nothing is selected, do nothing.MouseReleased: If a JLabel is
selected, paint it back to its
original color. Select nothing (maybe
null). If not, do nothing.
These are all in JPanel; JPanel implements MouseInputListener.
So here is the problem: When the inital position of a JLabel is 0,0 say I move it to 10,10. And after the mouse release and nothing is selected, when I click on 0,0 it selects that JLabel; however it was supposed to select it if I click at 10,10 because this is its new position.
Now I think this might be because I'm using the wrong coordinates; I've heard that the coordinate values in JPanel are relative, so I have to do a subtraction (i.e. final-initial coordinates) everytime to get the correct coordinates. I did it, but it did not work either. Another possibility might be that Java is storing all the historical X and Y coordinates (so that everytime I click on a previous coord, I select that object) which is purely imagination!
What are your suggestions?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 MouseMotionListener 添加到每个标签,而不是将其添加到面板。那么你不需要确定你是否点击了标签。
有关一般实现,请参阅组件移动器。您需要对其进行自定义以支持颜色要求。
编辑:
如果将侦听器添加到面板,那么坐标将始终相对于面板,而不是相对于标签,所以我不确定问题是什么。如果您想查找是否单击了某个组件,请使用 Container.getComponentAt(Point) 方法。
如果您需要更多帮助,请发布您的 SSCCE 来说明问题。
Add the MouseMotionListener to each of the labels instead of adding it to the panel. Then you don't need to determine whether you clicked on a label or not.
See the Component Mover for a general implementation. You would need to customize it to support the coloring requirement.
Edit:
If you add the listener to the panel then the coordinates will always be relative to the panel, not the label, so I'm not sure what the problem is. If you want to find if you clicked on a component then use the Container.getComponentAt(Point) method.
If you need more help then post your SSCCE that demonstrates the problem.