java - 我们可以在其他面板中添加面板吗?如果是,那么我的代码有什么问题
问题描述
我正在尝试制作一个非常简单的时间管理器,当我编译它时显示错误。我无法在我的代码中找到导致错误的错误。
JPanel otherPanel = new JPanel();
otherPanel.add(BorderLayout.WEST, timingsPanel);
otherPanel.add(BorderLayout.CENTER, todoPanel);
otherPanel.add(BorderLayout.EAST, checkPanel);
错误是:
Error message: Exception in thread "main" java.lang.NullPointerException
at java.desktop/java.awt.Container.addImpl(Container.java:1117)
at java.desktop/java.awt.Container.add(Container.java:460)
at GUI.setUpGui(GUI.java:57) at manager.main(manager.java:6)
第 57 行附近的代码:
//setting up otherstuff
otherPanel = new JPanel();
otherPanel.add(BorderLayout.WEST, timingsPanel);
otherPanel.add(BorderLayout.CENTER, todoPanel);
otherPanel.add(BorderLayout.EAST, checkPanel);
以下代码最少,它也显示相同的错误
import javax.swing.*;
import java.awt.*;
public class timeclass
{
JFrame frame;
JPanel headerPanel;
JLabel header;
JPanel otherPanel;
JPanel timingsPanel;
JPanel todoPanel;
JPanel checkPanel;
public void setUpGui()
{
frame = new JFrame("Time Manager");
//setting up header
headerPanel = new JPanel();
header = new JLabel("Time Manager");
header.setFont(new Font("Times New Roman", Font.BOLD, 36));
headerPanel.add(header);
//setting up otherstuff
otherPanel = new JPanel();
otherPanel.add(BorderLayout.WEST, timingsPanel);
otherPanel.add(BorderLayout.CENTER, todoPanel);
otherPanel.add(BorderLayout.EAST, checkPanel);
//setting up frame
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
BoxLayout boxLayout4 = new BoxLayout(frame, BoxLayout.Y_AXIS);
frame.setLayout(boxLayout4);
frame.add(headerPanel);
frame.add(otherPanel);
frame.setSize(900, 400);
frame.setVisible(true);
}
}
解决方案
推荐阅读
- windows - Power shell Get-Counter:没有应用程序与此操作的指定文件关联
- android - 在 Android 中实现 Stories 功能
- tomcat - 我想在 Intellij Ultimate IDE 中自定义 tomcat 设置
- android - RecyclerView --SelectionTracker 将选择大小限制为 10 个项目
- cytoscape - Cytoscape 不允许我将表格上传到网络集合,唯一的选择是未分配的表格。有谁知道如何解决这个问题?
- sql-server - SQL 查询 - CONVERT datetime to date 不起作用
- javascript - 尝试修复学校项目的电子邮件验证
- powershell - 在powershell中提取两个特殊字符之间的字符串
- java - 使用 JUnit 5 测试自定义约束注解
- r - 如何制作与使用 knitr::kable 制作的表格相同的 MSWord 样式的 flextable 表格,或者如何定义中间行?