gpt4 book ai didi

java - 将 JTabbedPane 添加到 JTabbedPane 错误

转载 作者:行者123 更新时间:2023-12-01 18:52:51 28 4
gpt4 key购买 nike

收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Project.reportpane.addComponent(reportpane.java:69)

我无法将 rptinvestment tabbedPane 添加到面板 tabinvestment

公共(public)类reportpane扩展了JPanel{

JTabbedPane rpt;
JPanel tabcashflow;
JPanel tabinvestment;
JPanel tabexchangerates;
JTabbedPane rptinvestment;
JPanel tabofficial;
JPanel tabdem;
JPanel tabcommodities;

public reportpane() {

rpt = new JTabbedPane();
tabcashflow = new JPanel();
tabinvestment = new JPanel();
tabexchangerates = new JPanel();

tabofficial = new JPanel();
tabdem = new JPanel();
tabcommodities = new JPanel();


rpt.add("Cashflow", tabcashflow);
rpt.add("Investement", tabinvestment);
rpt.add("Exchange rates", tabexchangerates);
rpt.setBackground(new Color(255, 255, 255));
rpt.setFont(new Font("sansserif", Font.PLAIN, 14));
rpt.setTabPlacement(javax.swing.JTabbedPane.TOP);
rpt.setBorder(BorderFactory.createEmptyBorder());
rpt.setSize(750, 500);


rptinvestment = new JTabbedPane();
rptinvestment.add("Official", tabofficial);
rptinvestment.add("DEM", tabdem);
rptinvestment.add("Commodities", tabcommodities);
rptinvestment.setBackground(new Color(255, 255, 255));
rptinvestment.setFont(new Font("sansserif", Font.PLAIN, 14));
rptinvestment.setTabPlacement(JTabbedPane.TOP);
rptinvestment.setBorder(BorderFactory.createEmptyBorder());
rptinvestment.setSize(750, 500);

addComponent(tabinvestment, rptinvestment, 0, 0, 675, 570);
addComponent(this, rpt, 10, 10, 675, 570);



this.setLayout(new BorderLayout());
this.setBackground(Color.WHITE);
this.setBorder(null);
this.revalidate();
this.repaint();

}

public void addComponent(Container container, Component c, int x, int y, int width, int height) {
c.setBounds(x, y, width, height);
container.add(c);
}

}

最佳答案

您永远不会初始化 tabinvestment。您将其声明为实例变量,但从未在构造函数中初始化它。有时您需要调用 JTabbedPane tabinvestment = new JTabbedPane()

关于java - 将 JTabbedPane 添加到 JTabbedPane 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15196309/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com