【问题标题】:Images doesn't work in eclipse图像在日食中不起作用
【发布时间】:2017-04-29 08:25:29
【问题描述】:

我已经在 youtube 和这里​​进行了搜索,这可能是个愚蠢的问题,但我找不到如何让它发挥作用。

问题是当我在我的 eclipse 文件中添加图片时它们看起来像;

setLayout(new FlowLayout());

    image1 = new ImageIcon(getClass().getResource("view.jpg"));
    label1 = new JLabel(image1);
    add(label1);

    image2 = new ImageIcon(getClass().getResource("view1.jpg"));
    label1 = new JLabel(image2);
    add(label2);    // here my code

有什么想法吗?

【问题讨论】:

  • 你试过了吗:.getResource("Image/Images/view.jpg")
  • 我在你之后试过了,但它不起作用:(但是我不需要像图片一样在图像文件中看到吗?它们看起来像记事本书写
  • 我修复了它大声笑我只把图片放在文件中,它现在可以工作了。谢谢

标签: java eclipse image swing add


【解决方案1】:

我有一个设置标签图标的使用方法,你可以参考

private void loadAvartar(JLabel lbl, String path){
    try {
        BufferedImage image = ImageIO.read(new File(path));

        ImageIcon icon = new ImageIcon(image.getScaledInstance(100, 100, 50));
        lbl.setIcon(icon);
    } catch (IOException | java.lang.NullPointerException e) {
        lbl.setText("Failed");
    }

}

【讨论】:

  • 我修复了它大声笑我只把图片放在文件中,它现在可以工作了。谢谢
  • 应用程序资源在部署时将成为嵌入式资源,因此明智的做法是立即开始访问它们。 embedded-resource 必须通过 URL 而不是文件访问。请参阅info. page for embedded resource 了解如何形成 URL。
猜你喜欢
  • 2012-01-26
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
相关资源
最近更新 更多