简体   繁体   English

将JTabbedPane添加到JTabbedPane错误

[英]Adding a JTabbedPane to a JTabbedPane error

Getting this error: 收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Project.reportpane.addComponent(reportpane.java:69) Project.reportpane.addComponent(reportpane.java:69)处的线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException

I'm unable to add the rptinvestment tabbedPane to the panel tabinvestment 我无法将rptinvestment tabbedPane添加到面板tabinvestment

public class reportpane extends JPanel { 公共类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);
}

} }

You never initialize tabinvestment. 您永远不会初始化tabinvestment。 You declare it as an instance variable, but you never initialize it in the constructor. 您将其声明为实例变量,但从未在构造函数中对其进行初始化。 At some point you need to call JTabbedPane tabinvestment = new JTabbedPane() . 在某些时候,您需要调用JTabbedPane tabinvestment = new JTabbedPane()

Your attempting to add components using absolute positioning which is been overridden by the layout manager 您尝试使用绝对定位添加组件,而布局管理器已覆盖该绝对定位

JTabbedPane uses its own layout logic and unless your really ever to create your own UI class, can't be changed. JTabbedPane使用其自己的布局逻辑,除非您真的曾经创建过自己的UI类,否则无法更改。

Equally, you've set the frames layout manager to BorderLayout, which will override any positioning values may have set (previously to otherwise) 同样,您已将框架布局管理器设置为BorderLayout,它将覆盖可能已设置的任何定位值(以前为其他设置)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM