【发布时间】:2018-12-26 07:08:46
【问题描述】:
我正在尝试制作一个 GUI,它具有标题面板、信息部分面板 (InfoSect),以及稍后在用户将在信息部分中键入和更改值的面板上。现在我被困在试图让面板显示。我不断收到 InfoSect 面板的错误,其中我有一组 JLabels。我想我初始化错了,但我不确定如何或为什么。它似乎也影响了更简单的标题面板的显示。希望能帮助您在 GUI 中显示此面板。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI extends JFrame{
private JPanel main;
Title tle1;
InfoSect is;
public GUI() {
main = new JPanel();
tle1 = new Title();
is = new InfoSect();
main.setLayout(new BorderLayout());
main.setBackground(Color.GRAY);
add(main);
main.add(tle1, BorderLayout.NORTH);
main.add(is, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900,700);
setVisible(true);
}
public static class Title extends JPanel{
private JLabel title;
public Title() {
title = new JLabel("Change the Values");
setLayout(new FlowLayout());
add(title);
}
}
public static class InfoSect extends JPanel{
private JLabel[] info;
private int COL = 4;
public InfoSect() {
info = new JLabel[COL];
setLayout(new FlowLayout());
displayInfo();
add(info[COL]);
}
public void displayInfo() {
for(int col=0;col<COL;col++) {
Font font1 = new Font(Font.SANS_SERIF,Font.PLAIN,10);
info[col].setFont(font1);
info[col].setText("Holder");
add(info[col]);
}
}
}
}
例外是:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at build001.GUI$InfoSect.displayInfo(GUI.java:59)
at build001.GUI$InfoSect.<init>(GUI.java:52)
at build001.GUI.<init>(GUI.java:20)
【问题讨论】:
-
你得到什么错误?
-
在 build001.GUI$InfoSect.displayInfo(GUI.java:59) 在 build001.GUI$InfoSect.
(GUI .java:52) 在 build001.GUI. (GUI.java:20)
标签: java arrays swing nullpointerexception jlabel