让我的 Java 程序通过拖放来显示图像

发布于 2024-09-30 00:40:25 字数 177 浏览 3 评论 0原文

我试图让我的程序在拖动图像后显示图像,但我不太清楚如何做到这一点。

示例:您的桌面上有一张图像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2024-10-07 00:40:25

一旦你有了图像(我不认为你问的是拖放功能,如果这是你问的部分,抱歉。),你可以将图像推入 ImageIcon 并将其显示在JLabel 如此处所述

或者您可以使用paintComponent 中的 Graphics#drawImage 方法用于绘制图像
类似的东西(作为非常基本示例):

public void paintComponent(Graphics page)
{
    super.paintComponent(page);
    page.drawImage(img, 0, 0, null);
}

我给出了一个更详细的示例这里

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 in paintComponent to draw Images.
Something along the lines of (as a very basic example):

public void paintComponent(Graphics page)
{
    super.paintComponent(page);
    page.drawImage(img, 0, 0, null);
}

I give a more detailed example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文