【问题标题】:JAVA JPanel not displaying imageJAVA JPanel 不显示图像
【发布时间】: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


【解决方案1】:

我想弹出一个新窗口,该窗口将在 URL 中显示此图像,而不是将其添加到现有框架中

我问你为什么希望它显示为一个窗口,你说,

我希望它会这样,因为我已经实例化了它并在它上面调用了 setVisible。

了解 JPanel 是一个容器组件,它包含其他组件,但没有显示完整 GUI 的机制。为此,您需要将其放入某种类型的顶级窗口中,例如 JFrame、JDialog、JApplet 或 JOptionPane,或放入显示在顶级窗口中的另一个容器中。

然后创建一个对话窗口并在其中显示图像。最简单的是 JOptionPane:

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

// code not needed:
// panel.setLocation(0,0);
// panel.setVisible(true);

// mainGuiComponent is a reference to a component on the main GUI
// or null if there is no main GUI.
JOptionPane.showMessageDialog(mainGuiComponent, label);

请注意,您可以只将 ImageIcon 传递给 JDialog,这也足够了

JOptionPane.showMessageDialog(mainGuiComponent, myImageIcon);

【讨论】:

  • 非常感谢。也非常翔实。作为后续问题,如何/为什么将 null 作为父组件传递仍然允许我的 JOptionPane 显示?
  • @peggy:一个JOptionPane实际上是一个JDialog或变相的“子”窗口,第一个参数是Swing用来给对话框分配一个父窗口,一个对话框会锁定的窗口并在 JDialog 关闭之前阻止用户交互。如果你传入null,Swing 会创建(我相信)一个不显示的 JFrame 作为对话框的父窗口。请注意,我不是 100% 确定后者,并且需要仔细检查它的准确性。我确实知道 null 有效,但如果您从另一个 Swing 窗口显示 JOptionPane,则不应使用它。
【解决方案2】:

JPanel 本身不会弹出和显示任何内容。您需要将其添加到父窗口,例如 JFrame 或 JDialog。

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

JFrame frame = new JFrame();
frame.add(panel);
frame.pack();
frame.setVisible(true);

这应该让你开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2017-01-10
    • 2013-01-05
    相关资源
    最近更新 更多