【发布时间】:2015-07-21 19:49:10
【问题描述】:
我目前正在从 url 读取图像(谷歌街景)并将其添加到 JPanel。首先,我使用 url "https://maps.googleapis.com/maps/api/streetview?size=600x300&location=46.414382,10.013988" 并尝试从中读取图像,然后将此图像添加到我将显示的 jpanel 中。我没有收到任何编译或运行时错误,但是 JPanel 永远不会弹出,所以我无法判断我的图像是否在上面。这里有什么想法吗?
编辑:澄清一下,我想弹出一个包含从 URL 读取的图像的新窗口,而不是将图像添加到现有面板中
URL url = new URL("https://maps.googleapis.com/maps/api/streetview?size=600x300&location=46.414382,10.013988");
BufferedImage streetView = ImageIO.read(url);
JLabel label = new JLabel(new ImageIcon(streetView));
JPanel panel = new JPanel();
panel.add(label);
panel.setLocation(0,0);
panel.setVisible(true);
【问题讨论】:
-
@cbender 我使用从缓冲图像创建的图像图标制作标签,然后将标签添加到面板...有其他方法吗?
-
我有一个愚蠢的问题:您是否将此面板添加到任何可见的框架中?
-
我支持@Pshemo,因为问题可能出在您的 JPanel 面板变量上。我建议您简化并删除面板 JPanel 变量,因为它除了混淆之外没有其他用途。而是将图像添加到已添加到 GUI 的 JLabel。不,不要像有人在答案中建议的那样显示另一个 JFrame。
-
也许我没有我应该说的那么清楚。我想弹出一个新窗口,它将在 URL 中显示此图像,而不是将其添加到现有框架中
-
你还提到,
" however the JPanel never pops up..."——你为什么会期望它呢?编辑,您声明:"I want to pop up a new window which will display this image in the URL, not add it to an existing frame"- 然后将其显示在 JOptionPane 中。
标签: java jpanel bufferedimage imageicon