Trying to display URL image in JFrame(试图在 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.
推荐答案
对我来说很好......
Works fine for me...
除了您没有处理异常(这可能对诊断有用)并且没有真正在 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 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:试图在 JFrame 中显示 URL 图像


基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01