2011-03-16 3 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'메소드는 어떻게됩니까? –

+2

'JPanel'을 확장하지 않으면 코드를 더 쉽게 만들 수 있습니다. 또한 레이블 필드를 제거합니다 (정상적인 상황에서는 패널에서 해당 필드를 두드리고 잊어 버릴 수 있어야합니다). –

답변

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

관련 문제