尝试在JFrame中显示URL图像 [英] Trying to display URL image in JFrame

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

问题描述

尝试在JFrame窗口中显示URL图像.如果此方法正常运行,则在程序运行时,应打开一个窗口以显示图像.尝试尝试使用URL和硬盘驱动器路径.

Trying to display a URL-image in a JFrame window. If this works correctly, when the program runs, a window should open displaying an image. Trying to experiment with URL's and hard-drive paths.

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

 class ImageInFrame {
    public static void main(String[] args) throws IOException {
    String path = "http://chart.finance.yahoo.com/z?s=GOOG&t=6m&q=l";
    URL url = new URL(path);
    BufferedImage image = ImageIO.read(url);
    JLabel label = new JLabel(new ImageIcon(image));
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(label);
    f.pack();
    f.setLocation(200,200);
    f.setVisible(true);
  }
  }

编译正常,但无法运行.我一直在尝试使用某些YahooFinance数据,只是因为它具有自定义性,因此使用起来很有趣.希望有人能帮忙.干杯.

Compiles just fine, but fails to run. I've been experimenting with some YahooFinance data simply because it's fun to work with due to it's customization. Hope someone can help. Cheers.

推荐答案

对我来说很好...

除了您没有处理异常(这可能对诊断很有用)并且没有在EDT中真正加载程序外,它似乎还可以正常工作...

Apart from the fact your not handling the exception (which might be useful for diagnostics) and not really loading the program within the EDT, it seems to work just fine...

public class TestURLImage {

    public static void main(String[] args) {
        new TestURLImage();
    }

    public TestURLImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                try {
                    String path = "http://chart.finance.yahoo.com/z?s=GOOG&t=6m&q=l";
                    System.out.println("Get Image from " + path);
                    URL url = new URL(path);
                    BufferedImage image = ImageIO.read(url);
                    System.out.println("Load image into frame...");
                    JLabel label = new JLabel(new ImageIcon(image));
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.getContentPane().add(label);
                    f.pack();
                    f.setLocation(200, 200);
                    f.setVisible(true);
                } catch (Exception exp) {
                    exp.printStackTrace();
                }

            }
        });
    }
}

这篇关于尝试在JFrame中显示URL图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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