让我的 Java 程序通过拖放来显示图像
我试图让我的程序在拖动图像后显示图像,但我不太清楚如何做到这一点。
示例:您的桌面上有一张图像 photo.jpg 。您可以将其拖到 java JFrame 中,然后将显示该图像的内容(照片本身)。
从那里我希望能够发挥照片的大小能力,但第一步,我不知道如何让它在 JFrame 中显示。任何帮助将不胜感激。
I am trying to get my program to display an image after it is dragged over, but I dont really have a good idea of how to do this.
Example: There is an image on your desktop, photo.jpg . You would take that, drag it over into a java JFrame, and the contents of that image (the photo iteself) would display.
From there I would like to be able to play with the size ability of the photo, but first step first, I dont know how to approach getting it to display in the JFrame. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦你有了图像(我不认为你问的是拖放功能,如果这是你问的部分,抱歉。),你可以将图像推入 ImageIcon 并将其显示在JLabel 如此处所述
或者您可以使用
paintComponent
中的Graphics#drawImage
方法用于绘制图像
。类似的东西(作为非常基本示例):
我给出了一个更详细的示例这里。
Once you have the image (I didn't think you were asking about the drag-and-drop feature, if that was the part you were asking about, sorry.), you can either push the image into an ImageIcon and display it in a JLabel as described here
Or you can mess around with using
Graphics#drawImage
methods inpaintComponent
to drawImages
.Something along the lines of (as a very basic example):
I give a more detailed example here.