GUI图像显示错误
GUI Image Display Error
所以我是新手 Java 从 YouTube 上的视频中学习一些基础知识,我正在学习制作 GUI/Window 目前我正在尝试显示图像但我不确定代码是 wrong/old 还是图像不正确 spot/location。这是我到目前为止所写的内容。帮助将不胜感激。请谢谢。
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();
}
}
我得到的错误是:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.(Unknown Source)
at FirstGUI.(FirstGUI.java:39)
at FirstGUI.main(FirstGUI.java:50)
首先您没有将标签添加到框架中,因此即使执行它也不会显示图像图标。所以不要忘记将标签添加到框架中:
add(label1);
add(label2);
其次 我试过你的代码,它对我来说工作正常,它只打印了你在我没有将图像图标导入我正在工作的包时提到的错误在。
为此,您需要这样做:
右击你的src包->导入->常规->文件系统然后点击next
和select包含图像,单击确定,然后添加您在代码中指定的图像。
所以我是新手 Java 从 YouTube 上的视频中学习一些基础知识,我正在学习制作 GUI/Window 目前我正在尝试显示图像但我不确定代码是 wrong/old 还是图像不正确 spot/location。这是我到目前为止所写的内容。帮助将不胜感激。请谢谢。
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();
}
}
我得到的错误是:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.(Unknown Source)
at FirstGUI.(FirstGUI.java:39)
at FirstGUI.main(FirstGUI.java:50)
首先您没有将标签添加到框架中,因此即使执行它也不会显示图像图标。所以不要忘记将标签添加到框架中:
add(label1);
add(label2);
其次 我试过你的代码,它对我来说工作正常,它只打印了你在我没有将图像图标导入我正在工作的包时提到的错误在。 为此,您需要这样做:
右击你的src包->导入->常规->文件系统然后点击next
和select包含图像,单击确定,然后添加您在代码中指定的图像。