2017-03-09 54 views
0

因此,我使用佈局管理器GridLayoutManager(IntelliJ)在IntelliJ IDEA中構建Swing佈局。IntelliJ IDEA中的自定義視圖類

這樣就可以了,我可以佈置所有東西等,但所有內容都在相同的代碼文件中,並且考慮到我正在使用帶有JTabbedPane的JPanel,我希望選項卡式窗格中的每個窗格都可以在單獨的類中表示。

這怎麼可能?

回答

1

有幾個方法可以做到這一點,無論是延長JPanel或創建其中包含基於溶液

public class MyCustomPanel extends JPanel { 

    // implement your custom behavior in here 
} 

然後,JPanel

繼承另一個類,你創建你JTabbedPane您在'd有這樣的事情:

private void init() { 
    JTabbedPane jtp = new JTabbedPane(); 
    JPanel jp = new MyCustomPanel(); 
    jtp.add(jp); 
} 

雖然這可行,但從長遠來看,延長JPanel可能會引起頭痛。另一個approache,這有利於composition over inheritance可能是這個樣子:基於解決方案

public class MyCustomPanel { 
    private JPanel myPanel = new JPanel(); 

    public MyCustomPanel() { 
     // add your customizations to myPanel 
    } 

    public JPanel getPanel() { 
     return myPanel; 
    } 
} 

然後

成分在其中創建您的JTabbedPane

private void init() { 
    JTabbedPane jtp = new JTabbedPane(); 
    MyCustomPanel mp = new MyCustomPanel(); 
    jtp.add(mp.getPanel()); 
} 
相關問題