所以我是Java新手,从YouTube上的视频中学习一些基础知识,我正在学习制作GUI /窗口,此刻我正在尝试显示iamges,但我是不知道代码是错误的/旧的或图像不在正确的位置/位置。这是我迄今写的。帮助将不胜感激。谢谢,麻烦您了。GUI图像显示错误
import java.awt.*;
import javax.swing.*;
public class FirstGUI extends JFrame {
private static Object out;
private JLabel label;
private JButton button;
private JTextField textfield;
private ImageIcon image1;
private JLabel label1;
private ImageIcon image2;
private JLabel label2;
public FirstGUI() {
setLayout (new FlowLayout());
label = new JLabel("Hi, I'm a label!");
add(label);
textfield = new JTextField(15);
add(textfield);
button = new JButton("Click me!");
add(button);
button = new JButton("No, CLICK ME!!");
add(button);
label = new JLabel("This is the end of the program?");
add(label);
image1 = new ImageIcon(getClass().getResource("Apiary.png"));
label1 = new JLabel(image1);
image2 = new ImageIcon(getClass().getResource("bee.png"));
label2 = new JLabel(image2);
}
public static void main(String[] args) {
FirstGUI gui = new FirstGUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//* gui.setSize(400, 400);
gui.setVisible(true);
gui.setTitle("Hello World");
gui.pack();
}
}
我的错误得到什么:
异常线程 “main” 显示java.lang.NullPointerException
在javax.swing.ImageIcon中(来源不明)
。在FirstGUI。(FirstGUI.java:39)
在FirstGUI.main(FirstGUI.java:50)
注意:这在图像显示代码被添加之前工作。 – MattRivas