2012-03-02 8 views
5

BorderLayoutを使用する中規模の大規模なUIがあります。センターはさまざまなレイアウトのパネルを含むタブ付きのパネルです。BorderLayoutの中心にコンポーネントの望ましいサイズを維持する

この境界線レイアウトの中央にあるパネルを、ウィンドウのサイズに合わせてサイズ変更するには、ウィンドウ内のコンポーネントを入れないようにします。ストレッチするパネル。ラベル、コンボボックス、テキストフィールド、ボタン - 好みのサイズにして、それらを含むパネルを引き伸ばすことができます。私はスクロールペインにスペースを入れて、スペースがパネルにとって小さすぎる場合に備えています。

カラフルなボキャブラリを持つさまざまなポスターは、コンポーネントでsetXXXsize()メソッドを使用する危険性を警告します。それが私が今やっていることです。私はそれを避ける方法を学びたいと思います。

GridBagLayoutは私の一部のパネルでは適切ではありません。本質的には、行と列を中心にしており、すべてが行と列に収まるわけではありません。もちろん、私は人工の行と列を作成してすべてに合わせることができますが、Swingにはそれ以上のレイアウトオプションがあることを本当に望んでいます。

縦型接着剤もどちらもしません。私はHFOEの最愛のSSCEでそれを含めました:

package example; 

    import java.awt.BorderLayout; 

    import javax.swing.Box; 
    import javax.swing.BoxLayout; 
    import javax.swing.JComboBox; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 
    import javax.swing.JPanel; 

    public class BorderAndBox extends JFrame 
    { 
     public static void main(String args[]) 
     { 
      BorderAndBox bnb = new BorderAndBox(); 
      bnb.createUI(); 
      bnb.setVisible(true); 
     } 

     public void createUI() 
     { 
      JPanel borderPanel = new JPanel(new BorderLayout()); 

      JLabel northLabel = new JLabel("Nawth"); 
      borderPanel.add(northLabel, BorderLayout.NORTH); 

      String[] southComboChoices = { "one", "two", "three" }; 
      JComboBox southCombo = new JComboBox(southComboChoices); 
      borderPanel.add(southCombo, BorderLayout.SOUTH); 

      JPanel centerPanel = new JPanel(); 
      centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS)); 
      String[] firstChoices = { "first", "uno", "UN" }; 
      String[] secondChoices = { "second", "dos", "zwei" }; 
      String[] thirdChoices = { "third", "tres", "drei" }; 
      JComboBox firstCombo = new JComboBox(firstChoices); 
      JComboBox secondCombo = new JComboBox(secondChoices); 
      JComboBox thirdCombo = new JComboBox(thirdChoices); 
      centerPanel.add(firstCombo); 
      centerPanel.add(secondCombo); 
      centerPanel.add(thirdCombo); 
      centerPanel.add(Box.createVerticalGlue()); // first attempt; does NOT 
      // take up available vertical space, instead it appears to create a space 
      // that is shared equally among the (now) four components of this space. 
      borderPanel.add(centerPanel, BorderLayout.CENTER); 

      getContentPane().add(borderPanel); 
      pack(); 
     } 

    } 

ウィンドウを拡大した場合は、センター内のコンボボックスが拡大します。書かれているように、それらの下の縦の接着剤もまた拡大するが、利用可能なすべてのスペースを占めるわけではない。それは彼らのそれぞれと同じくらいのスペースが与えられているようです。

これにアプローチするにはどうすればよいでしょうか?

+0

* "愛するSSCE:" *私はそれも大好きです。 SSCCE(2 Cs)です。 ;) –

+0

小さな自己完結型の例ではありませんか? – arcy

+0

* "私は自分の好きな人のひとりです:*"この文書では、正しい(またはコンパイル可能な、特にコンピュータのソースコードに関連する)は、あなたのサンプルが受け入れられた標準とプロトコルに適合することを保証することを意味します "*最も顕著なサブポイント*"あなたの例が正しいことを確認してください。助けがほしいと思う」*コードがコンパイルされ、問題が簡単に示された。それは* SSCCEです。 :) –

答えて

10

BorderAndBox - centered

import java.awt.BorderLayout; 
import java.awt.GridBagLayout; 

import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class BorderAndBox extends JFrame 
{ 
public static void main(String args[]) 
{ 
    BorderAndBox bnb = new BorderAndBox(); 
    bnb.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    bnb.createUI(); 
    bnb.setVisible(true); 
} 

public void createUI() 
{ 
    JPanel borderPanel = new JPanel(new BorderLayout()); 

    JLabel northLabel = new JLabel("Nawth"); 
    borderPanel.add(northLabel, BorderLayout.NORTH); 

    String[] southComboChoices = { "one", "two", "three" }; 
    JComboBox southCombo = new JComboBox(southComboChoices); 
    borderPanel.add(southCombo, BorderLayout.SOUTH); 

    JPanel centerPanel = new JPanel(); 
    centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS)); 
    String[] firstChoices = { "first", "uno", "UN" }; 
    String[] secondChoices = { "second", "dos", "zwei" }; 
    String[] thirdChoices = { "third", "tres", "drei" }; 
    JComboBox firstCombo = new JComboBox(firstChoices); 
    JComboBox secondCombo = new JComboBox(secondChoices); 
    JComboBox thirdCombo = new JComboBox(thirdChoices); 
    centerPanel.add(firstCombo); 
    centerPanel.add(secondCombo); 
    centerPanel.add(thirdCombo); 
    centerPanel.add(Box.createVerticalGlue()); // first attempt; does NOT 
    // take up available vertical space, instead it appears to create a space 
    // that is shared equally among the (now) four components of this space. 
    JPanel centerPanelConstrain = new JPanel(new GridBagLayout()); 
    centerPanelConstrain.add(centerPanel); 
    borderPanel.add(centerPanelConstrain, BorderLayout.CENTER); 

    getContentPane().add(borderPanel); 
    pack(); 
} 

} 

this answer参照してください。これを解決する方法は複数あります。

+0

これはかわいいトリックです。私は、私が望むように、それが左上に表示されるように私の単一のコンポーネントのGridBagLayoutで遊ぶことができると仮定します。なぜこれがうまくいくのか詳しく調べてみませんか? – arcy

+0

* "なぜこれが機能するのか詳しく説明しますか?"*単一のコンポーネントを制約なしでGBLに追加すると、中央で終わります。他のポジションについては、わかりません。私はGBLの専門家ではありません。 –

+0

GroupPlayout?LOL、+1、私も同じロジックを投稿しようとしていましたが、私もあまりにも多いようです遅すぎる。 –

-1

私はあなたがjavax.swing.GroupLayoutJPanelを使用することをお勧めします。しかし、このレイアウトをコーディングして使用するのは容易ではありません。 Netbeans Matisse Builderのようなコードジェネレータを使用して、IDEを使用したくない場合はどこにでも貼り付けてください。

+1

GroupLayoutは、GridBagLayoutのように、すべて行と列についてです。 Thompson氏がちょうど私にGridBagLayoutを見せたのと同じ方法でGroupLayoutを使うことができました。私が望むように左上隅に置くのが簡単なのか分かります。しかし、私はGroupとGridBagのレイアウトの両方が、いくつかのコンポーネントを持つが、行/列の外観を持たないパネルの選択肢が悪いと思う。 – arcy

関連する問題