Java 试图在JFrame中显示URL图像
正在尝试在JFrame窗口中显示URL图像。如果工作正常,当程序运行时,将打开一个窗口显示图像。正在尝试URL和硬盘驱动器路径Java 试图在JFrame中显示URL图像,java,url,jframe,Java,Url,Jframe,正在尝试在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 ma
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数据,因为它是定制的,使用起来很有趣。希望有人能帮忙。干杯 对我来说很好
除了您没有处理异常(这可能对诊断有用)和没有在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();
}
}
});
}
}
嗯,即使它编译了,它也没有做任何事情。只是生成一个错误列表。“只是生成一个错误列表。”如果您指的是堆栈跟踪,请将其作为编辑发布。可能是雅虎不希望他们的财务数据被任何应用程序使用。首先尝试一个图像表单。顺便说一句-这里发布的代码对我有效。我发现了我的错误。是的,谢谢。我猜它终究还是有用的……我是个白痴。
}catch(ClassNotFoundException |实例化exception |非法访问exception |不受支持的lookandfeelexception ex){
Heh-Heh.Cool.:@AndrewThompson终于跳到了7…我喜欢;)有没有可能你能解释一下你用UIManager做什么?我对它的功能不太熟悉。UIManager
有很多重要的角色,我用它来安装系统(在我的例子中,是Windows)外观。您已经了解了一些示例Hanks!我来看看。顺便说一句,我在运行文件时遇到了一些断断续续的问题:它可以编译,但我有一个带有NoClassDefFoundError的“线程主线程中的异常”。我不太清楚如果它编译了,为什么会报告此问题。