本文共 943 字,大约阅读时间需要 3 分钟。
对我来说很好……
除了你没有处理异常(可能对诊断有用)并且没有真正在EDT中加载程序的事实,它似乎工作得很好……
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();
}
}
});
}
}
转载地址:http://dmvqa.baihongyu.com/