【问题标题】:JLabel picture display issueJLabel 图片显示问题
【发布时间】:2015-09-30 22:25:38
【问题描述】:

我目前正在尝试在JFrame 中简单地显示图像。我试过使用下面的代码,但它似乎不起作用。我不知道哪里出错了 - 有什么想法吗?

这是控制台中显示的错误信息:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at Day1.PictureTester.<init>(PictureTester.java:22)
    at Day1.PictureTester.main(PictureTester.java:14)

代码如下:

import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class PictureTester extends JFrame{

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        new PictureTester().setVisible(true);
    }

    public PictureTester(){
        super();
        setSize(600, 600);
        setLayout(new GridLayout());
        java.net.URL imgUrl = PictureTester.class.getResource("C:\\Users\\Harry\\Desktop\\LearnJava\\pug-image3.jpg");
        ImageIcon image = new ImageIcon(imgUrl);    
        JLabel display = new JLabel(image);

        add (display);
    }
}

【问题讨论】:

标签: java image swing jlabel embedded-resource


【解决方案1】:

这当然行不通。阅读Class.getResource() 的javadoc。它使用 ClassLoaderclasspath 加载资源,使用 slash 分隔路径。

将您的图像与PictureTester 类放在同一个包中,然后使用

PictureTester.class.getResource("pug-image3.jpg");

或者将它放在源文件夹的某个随机包中(如com.foo.bar),然后使用加载它

PictureTester.class.getResource("/com/foo/bar/pug-image3.jpg");

【讨论】:

  • 您好 JB Nizet,感谢您的意见。我已经相应地调整了代码,但我仍然面临与上述相同的错误消息。有什么想法吗?
  • 那么,新代码是什么,你把文件放在哪里了?您如何构建项目?您使用的是哪个 IDE?是 gradle 还是 Maven 项目?
  • IDE:Eclipse Gradle/Maven:不知道。归档放置:在包内的新源文件夹中。新代码:package Day1; import java.awt.GridLayout; import javax.swing.JFrame; public class PictureTester extends JFrame{ private static final long serialVersionUID = 1L; public static void main(String[] args) { new PictureTester().setVisible(true); } public PictureTester(){ setSize(600, 600); setLayout(new GridLayout()); PictureTester.class.getResource("pug-image3.jpg"); } }
【解决方案2】:

这段代码解决了它,它帮助我思考在一个新项目中创建它:

package NewAppIdea;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


    public class PictureSplash extends JFrame {

        private static final long serialVersionUID = 1L;
        JLabel l1;

        PictureSplash(){
        setTitle("Pic");
        setSize(400,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(new BorderLayout());
        setContentPane(new JLabel(new ImageIcon("C:\\Users\\Harry\\Desktop\\LearnJava\\pug-image3.jpg")));
        setLayout(new FlowLayout());
        setSize(399,399);
        setSize(400,400);
        }

        public static void main(String args[]) {
            new PictureSplash();
        }   
    }

【讨论】:

    【解决方案3】:
    You can do it like the following example, by using just an image and a label. also you can remove all the extra code if you intend to use this image only once.
    
    import java.awt.GridLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class PictureTester extends JFrame{
    
        private static final long serialVersionUID = 1L;
    
        public static void main(String[] args) {
            new PictureTester().setVisible(true);
        }
    
        public PictureTester(){
            super();
            setSize(600, 600);
            setLayout(new GridLayout());
            add(new JLabel(new ImageIcon("Path/To/Your/Image.png")));
        }
    }
    

    【讨论】:

    • @moffeltje 你去吧。
    【解决方案4】:
            Icon i=new ImageIcon("src//pics//scope.jpg");
            JLabel l1=new JLabel(i);
    

    像这样简单使用JLabelImageIcon

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      相关资源
      最近更新 更多