2017-05-10 3 views
2

私は複雑なTabbedPaneを使ってたくさんの要素を持つプログラムを書いています。クラスの1つでByte-limitに達したので、クラスをInitialisation/Button Listenersと実際のGridBagLayoutに分割することにしました。しかし、今私はそれを動作させるのに問題があります。私のメインクラスは次のようになります:Javaで別のクラスのレイアウトを設定するには?

public class Main{ 
JFrame mainFrame = new JFrame(""); 
JTabbedPane tabpane = new JTabbedPane(); 
JPanel panelTab1 = new Tab1(); 
JScrollPane scrollTab2 = new JScrollPane(new Tab2()); 
JScrollPane scrollTab3 = new JScrollPane(new Tab3()); 
JPanel panelTab4 = new Tab4(); 
JMenuBar bar = new MenuBar(); 


public Main(){ 
    tabpane.add("Tab1", panelTab1); 
    tabpane.add("Tab2", scrollTab2); 
    tabpane.add("Tab3", scrollTab3); 
    tabpane.add("Tab4", panelTab4); 
    mainFrame.getContentPane().add(tabpane); 
    mainFrame.setSize(1920,1080); 
    mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    mainFrame.setVisible(true);  
    mainFrame.validate(); 
    mainFrame.setJMenuBar(bar); 

} 



public static void main(String[] args) 
{ 

    SwingUtilities.invokeLater(new Runnable(){ 
     public void run(){ 
      new Main(); 
     } 
    }); 


}} 

タブ3は私が分割した2つのクラスです。

public class Tab3 extends JPanel { 
JPanel Annex = new JPanel(); 
//A Bunch of Labels and Buttons 
. 
. 
. 
public Tab3(){ 
//ActionListeners for the Buttons 
this.setLayout(new BorderLayout()); 
this.add(Annex,BorderLayout.WEST); 
this.add(Bsp,BorderLayout.EAST); 
}} 

すべてのGridBagLayoutは以下のクラスである:

public class Tab3Layout extends Tab3{ 
    public Tab3Layout(){ 
     Annex.setLayout(new GridBagLayout()); 
     GridBagConstraints co1 = new GridBagConstraints(); 
     co1.gridx = 0; 
     co1.gridy = 0; 
     co1.anchor = GridBagConstraints.FIRST_LINE_START; 
     co1.weighty = 1.0; 
     Annex.add(Annex1, co1); 
     //and so on... 
     }} 

今、私の質問は、私はこの作業を取得んか、でしょうか?今コンパイルすると、Tab3は空です。すべてが1つのクラスに含まれていれば、それは私が望むように正確に動作しますが、コードはあまりにも長すぎます。私はTab3クラスで行が欠けているように私には思えるが、何時間もかかっていて検索しても、これを解決する方法は知られていない。私が試したすべてが、より多くのエラーを生成しました。

答えて

2

問題は継承にあります。 Tab3LayoutTab3クラスはそのAnnexを挿入している間、それはnew JPanel()経由で初期化され、あなたはそれで他に何もしないされ、修正およびにコンポーネントを追加している、(Tab3から継承)JPanelAnnex独自のを持っています。 Tab3Layoutクラスは、に初期化し、追加するパネルには一度も触れません。Annex

public class Tab3 extends JPanel { 
JPanel Annex = new JPanel(); // <----- this, an empty JPanel 
//A Bunch of Labels and Buttons 

public Tab3(){ 
//ActionListeners for the Buttons 
this.setLayout(new BorderLayout()); 
this.add(Annex,BorderLayout.WEST); // <----- is inserted here 
this.add(Bsp,BorderLayout.EAST); 
}} 

あなたは両方のクラスがちょうど同じ名前のことが起こる異なるオブジェクト上で作業を行っていることを示しますnew JPanel()を、削除する場合は、エラーになります。 Tab3に表示するすべてのコンポーネント/パネルをTab3Layoutに渡す必要があります。変更するには、後でgettersで取得できます。別の方法は、AnnexPanelクラスを作成することです。それ自体はJPanelクラスを拡張します。独自のレイアウト、コンポーネントなどを設定し、直接追加することができます。あなたのクラスのサイズが大きくなる場合は、Single Responsibility Principleに従うのが一番です。コード行が200行以上ある場合は、それをリファクタリングすることを検討する必要があります。あなたのクラスが3000行以上のコードのように成長することを許すならば、あなたは悪い時を過すでしょう。開発の初期段階で新しいクラス/メソッドを作成することができます。 KISS principleを参照してください。

まあ私は別の方法でそれらを分割します。

My Tab3Layoutクラス。附属パネルはinjected via constructorですが、setterを設定できますが、注入前にcreateAnnex()に電話しないでください。とにかく、コンパイラはあなたに覚えています。

public final class Tab3Layout { 

    private JPanel Annex; 
    JPanel Annex1; //just to avoid compiler error 
    private GridBagLayout gridBagLayout; 
    private GridBagConstraints co1; 

    public Tab3Layout(JPanel Annex) { 
     this.Annex = Annex; 
     this.gridBagLayout = new GridBagLayout(); 
     this.co1 = new GridBagConstraints(); 
    } 

    public void createAnnex() { 
     this.Annex.setLayout(gridBagLayout); 

     this.co1.gridx = 0; 
     this.co1.gridy = 0; 
     this.co1.anchor = GridBagConstraints.FIRST_LINE_START; 
     this.co1.weighty = 1.0; 

     this.Annex.add(Annex1, co1); 
    } 

    public JPanel getAnnex() { 
     return this.Annex; 
    } 
} 

そしてTAB3クラス:私のGridBagManagerレポで

public class Tab3 extends JPanel { 

    JPanel Annex; 
    Tab3Layout tab3Layout; 

    public Tab3() { 
     super(); 
     this.Annex = new JPanel(); 

     this.tab3Layout = new Tab3Layout(Annex); 
     this.tab3Layout.createAnnex(); 

     this.setLayout(new BorderLayout()); 
     this.add(tab3Layout.getAnnex(), BorderLayout.WEST); 
     this.add(new JPanel(), BorderLayout.EAST); 
    } 
} 

見て、それはあなたに別のクラスでレイアウトを設定する方法のアイデアを与えることができます。

JPanelでTab3Layoutを拡張することもできます。また、anchorfillをGridBagConstraintsに設定することを忘れないでください。

およびJava naming convention。あなたは、私は上記の私のレポで見るよう

JPanel Annex = new JPanel(); 
GridBagConstraints co1 = new GridBagConstraints(); 

JPanel annexPanel = new JPanel(); 
GridBagConstraints gridBagConstraints = new GridBagConstraints(); 

に個人的に私は、 GridBagConstraintsオブジェクトに対して gbcを使用しています。

編集: 私はあなたの実装をいくつかの小さなクラスに分割するべきだと思います。それはプロフェッショナルになるだろう。コードをリファクタリングしてください。まず、すべてのパネルにJPanel(私がAnnexPanelクラスで提案したような)クラスを拡張することができます。

EDIT2:次のようにcreateAnnex()があなたの定型的なコードを削減、JPanelのタイプを返すことができ、あなたの呼び出しは次のようになります。代わりに

Tab3Layout.createAnnex(); 
this.add(Tab3Layout.getAnnex(), co1); 

this.add(Tab3Layout.createAnnex(), co1); 

は多分クラスを想定し(コンストラクタにcreateAnnex()を取りますJPanel)がシングルトンの場合は拡張されます。

これが役立つ場合は、回答に同意してください。

+0

@Lotec何があなたの心を変えましたか? – bigby

関連する問題