2011-03-16 38 views
0

我的代碼是如何在已經擴展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

那麼'add'方法呢? – 2011-03-16 21:48:40

+2

不擴展'JPanel'可能會使代碼更容易。同時刪除標籤的字段(在正常情況下,您應該能夠在面板上拍打它們並忘記它們)。 – 2011-03-16 21:52:30

回答

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

} 
+0

感謝您的所有快速回復;他們真的幫助:) – user831788 2011-03-17 13:04:38