2017-03-09 16 views
0

したがって、レイアウトマネージャGridLayoutManager(IntelliJ)を使用してIntelliJ IDEAでSwingレイアウトを構築しています。IntelliJ IDEAのカスタムビュークラス

私はすべてのものをレイアウトすることができますが、すべてが同じコードファイルにあり、JTabbedPaneでJPanelを使用していると考えると、タブ付きペインの各ペインを別々のクラス。

これはどのように可能ですか?

答えて

1

あなたがこれを行うことができますカップルの方法で、JPanelを拡張したり、あなたのJTabbedPaneあなたの作成に続いてソリューション

public class MyCustomPanel extends JPanel { 

    // implement your custom behavior in here 
} 

をベースにJPanel

継承が含まれている別のクラスを作成するのいずれかがあります。

これは機能しますが、JPanelを延長すると長期的に頭痛を引き起こす可能性があります。ソリューションその後、

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()); 
} 
を作成:composition over inheritanceを好むもう一つのapproacheは、このようになります。