繁体   English   中英

如何在已经扩展JPanel的类中嵌套面板?

[英]How do you nest panels in a class that already extends JPanel?

我的代码是

public class IncomeStatementPanel extends JPanel
{
    private JLabel costOfGoodSoldIncState = new JLabel("Cost of goods sold", SwingConstants.RIGHT);
    private JLabel ebitIncState = new JLabel("EBIT", SwingConstants.RIGHT);
    private JLabel deprecIncState = new JLabel("Depreciation", SwingConstants.RIGHT);
    ...    

//I want to add more panels to this, but don't know the code to create them.

    public IncomeStatementPanel()
    {
        //Set grid layout for the panel
        setLayout(new GridLayout(14,2,0,0));



    }

}

你可以像往常一样添加它们。

public class IncomeStatementPanel extends JPanel
{
    private JLabel costOfGoodSoldIncState = new JLabel("Cost of goods sold", SwingConstants.RIGHT);
    private JLabel ebitIncState = new JLabel("EBIT", SwingConstants.RIGHT);
    private JLabel deprecIncState = new JLabel("Depreciation", SwingConstants.RIGHT);
    private JPanel myPanel = new JPanel(); // Nothing special here
    ...    

    public IncomeStatementPanel()
    {
        //Set grid layout for the panel
        setLayout(new GridLayout(14,2,0,0));
        this.add(myPanel); // Or here. The "this." part is optional by the way.
    }

}

暂无
暂无

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

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