简体   繁体   English

如何在Swing Java中设置图像的Z顺序

[英]How do I set Z-order for image in Swing Java

I have 3 image so I want, if I drag any image that should appear above all the other images. 我有3张图片,所以我想拖动任何应显示在所有其他图片上方的图片。 Which technique can use? 可以使用哪种技术? please any idea???? 请任何想法吗??? By the way I am using "ImageIcon". 顺便说一句,我正在使用“ ImageIcon”。

Thanks in advance. 提前致谢。

If what you want to do is click on an image, lift it above the others and then drag it, consider placing them in ImageIcons and these in JLabels, and displaying them in a JLayeredPane, or a JPanel that is held in a JLayeredPane. 如果要执行的操作是单击图像,将其提升到其他图像之上,然后将其拖动,请考虑将它们放置在ImageIcons中,并将它们放置在JLabel中,然后将它们显示在JLayeredPane或JLayeredPane中保存的JPanel中。 You can then lift the clicked label onto the JLayeredPane.DRAG_LAYER while dragging, and then drop it down into the DEFAULT_LAYER (or on a JPanel that's on the DEFAULT_LAYER) when done. 然后,您可以在拖动时将单击的标签提升到JLayeredPane.DRAG_LAYER上,然后将其拖放到DEFAULT_LAYER(或DEFAULT_LAYER上的JPanel上)中。 For an example of this, please see my code in a related question: dragging a jlabel around the screen 有关此示例,请在相关问题中查看我的代码: 在屏幕周围拖动jlabel

If I'm totally off base, sorry, but please correct my incorrect assumptions. 如果我完全不在基地,对不起,但是请更正我的错误假设。

By the way I am using "ImageIcon". 顺便说一句,我正在使用“ ImageIcon”。

I assume this means you are adding in image to a JLabel and are then adding the JLabel to a JPanel. 我认为这意味着您要将图像添加到JLabel,然后再将JLabel添加到JPanel。

so I want, if I drag any image that should appear above all the other images 所以我想,如果我拖动应出现在所有其他图像上方的任何图像

You need to add a MouseListener to the JLabel. 您需要将MouseListener添加到JLabel。 When you click on the label then you can reset its Z-Order to 0. This will cause the label to be painted last and so it will be on top of all other labels on the panel. 当您单击标签时,可以将其Z顺序重置为0。这将导致该标签最后被绘制,因此它将位于面板上所有其他标签的顶部。

So the basic code in the MouseListener would be: 因此,MouseListener中的基本代码为:

panel.setComponentZOrder(label, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM