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

时间:2011-03-16 21:44:38

标签: java swing 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));



    }

}

1 个答案:

答案 0 :(得分:2)

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

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.
    }

}