简体   繁体   中英

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

The code I have is

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));



    }

}

You can just add them like you normally would.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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