简体   繁体   English

Select 来自 JPanel 的图像

[英]Select an image from JPanel

I am working on a small project which requires me to load images into a window and then move them around at will.我正在做一个小项目,需要我将图像加载到 window 中,然后随意移动它们。

Thus far I can load images onto a JPanel simply by using a graphics object to draw them to the JPanel.到目前为止,我只需使用图形 object 将图像绘制到 JPanel 就可以将图像加载到 JPanel 上。

Now I'm faced with the challenge of figuring out how to differentiate between the various images I've loaded when I click on them so I can drag them around the screen.现在我面临的挑战是,当我点击它们时如何区分我加载的各种图像,以便我可以在屏幕上拖动它们。

Any ideas?有任何想法吗?

If you use a JLabel for each image, and make the JLabels subcomponents of the JPanel , it'll automatically draw the image, and you can add MouseListener s to each of them to be able to drag them around the screen.如果您为每个图像使用JLabel ,并使JLabels成为JPanel的子组件,它将自动绘制图像,您可以将MouseListener添加到每个图像,以便能够在屏幕上拖动它们。

Use the Component Mover to drag any component around the screen.使用组件移动器在屏幕上拖动任何组件。

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

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