java 尝试在 JFrame 中显示 URL 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13448368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:46:30  来源:igfitidea点击:

Trying to display URL image in JFrame

javaurljframe

提问by A.G.

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.

试图在 JFrame 窗口中显示 URL 图像。如果这工作正常,当程序运行时,应该会打开一个显示图像的窗口。尝试尝试使用 URL 和硬盘驱动器路径。

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);
  }
  }

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.

编译正常,但无法运行。我一直在试验一些 YahooFinance 数据,只是因为它的定制很有趣。希望有人能帮忙。干杯。

回答by MadProgrammer

Works fine for me...

对我来说很好用...

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...

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

enter image description here

在此处输入图片说明

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();
                }

            }
        });
    }
}