所以我是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