开发者

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

开发者 https://www.devze.com 2023-02-18 10:07 出处:网络
The code I have is public class IncomeStatementPanel extends JPanel { private JLabel costOfGoodSoldIncStat开发者_C百科e = new JLabel(\"Cost of goods sold\", SwingConstants.RIGHT);

The code I have is

public class IncomeStatementPanel extends JPanel
{
    private JLabel costOfGoodSoldIncStat开发者_C百科e = 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.
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号