如何在Java应用程序中显示图像 [英] How to display an image in a Java application

查看:136
本文介绍了如何在Java应用程序中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Java应用程序中显示图像。我找到了一个代码,用于从网络服务器下载图像并在jframe中显示。

I want to display an image in my Java application. I found a code to download the image from the webserver and show it in the jframe.

我想使用标签来显示图像或其他内容。它不应该是JFrame。

I want to use a label to show the image or something else. It shouldn't be JFrame.

这是我的代码:

Image image = null;
try {
    URL url = new URL(
        "http://www.personal.psu.edu/acr117/blogs/audrey/images/image-2.jpg");
    image = ImageIO.read(url);
} catch (IOException e) {
}

// Use a label to display the image
JFrame frame = new JFrame();

JLabel lblimage = new JLabel(new ImageIcon(image));
frame.getContentPane().add(lblimage, BorderLayout.CENTER);
frame.setSize(300, 400);
frame.setVisible(true);

有人可以帮助我吗?

推荐答案

使用您提供的代码,您已经将图像放在 JLabel 中,名为 lblimage 。您现在可以将其添加到面板或任何其他容器对象,如下所示:

Using the code you provided, you already have the image in a JLabel called lblimage. You can now add that to a panel or any other container object, like so:

JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(lblimage);
// add more components here
frame.add(mainPanel);
frame.setVisible(true);

您可以对待该标签( lblimage )就像你使用任何普通组件一样。

You can treat that label (lblimage) just like you would any normal component.

这篇关于如何在Java应用程序中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆