JLabel图片显示问题
JLabel picture display issue
我目前正在尝试在 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);
}
}
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")));
}
}
当然不行。阅读 Class.getResource()
的 javadoc。它使用 ClassLoader 从 classpath 加载资源,使用 slash分隔路径。
将您的图像与 PictureTester
class 放在同一个包中,然后使用
PictureTester.class.getResource("pug-image3.jpg");
或者将它放在源文件夹的某个随机包中(如 com.foo.bar
),然后使用
加载它
PictureTester.class.getResource("/com/foo/bar/pug-image3.jpg");
这段代码解决了它,它帮助我思考在新项目中创建它:
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();
}
}
Icon i=new ImageIcon("src//pics//scope.jpg");
JLabel l1=new JLabel(i);
像这样简单地使用JLabel
或ImageIcon
我目前正在尝试在 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);
}
}
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")));
}
}
当然不行。阅读 Class.getResource()
的 javadoc。它使用 ClassLoader 从 classpath 加载资源,使用 slash分隔路径。
将您的图像与 PictureTester
class 放在同一个包中,然后使用
PictureTester.class.getResource("pug-image3.jpg");
或者将它放在源文件夹的某个随机包中(如 com.foo.bar
),然后使用
PictureTester.class.getResource("/com/foo/bar/pug-image3.jpg");
这段代码解决了它,它帮助我思考在新项目中创建它:
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();
}
}
Icon i=new ImageIcon("src//pics//scope.jpg");
JLabel l1=new JLabel(i);
像这样简单地使用JLabel
或ImageIcon