2010-12-05 5 views
1
/** 
* I'd like to achive following layout: 
* +----------+----------+ 
* | Button 1 |   | 
* +----------| Button 2 | 
* | Button 3 |   | 
* +----------+----------+ 
* with following code: 
*/ 

JPanel panel = new JPanel(new GridBagLayout()); 
GridBagConstraints c = new GridBagConstraints(); 
JButton button; 

button = new JButton("Button 1"); 
c.weightx = 0.5; 
c.weighty = 0.5; 
c.fill = GridBagConstraints.BOTH; 
panel.add(button, c); 

button = new JButton("Button 2"); 
c.gridwidth = GridBagConstraints.REMAINDER; 
c.gridheight = 2; 
panel.add(button, c); 

button = new JButton("Button 3"); 
c.gridwidth = 1; 
panel.add(button, c); 

/** 
* but what I achive is: 
* +----------+----------+ 
* | Button 1 | Button 2 | 
* +----------+----------+| 
* | Button 3 | 
* +----------+ 
*/ 

/** 
* However layout: 
* +----------+----------+ 
* |   | Button 2 | 
* + Button 1 +----------+ 
* |   | Button 3 | 
* +----------+----------+ 
* is easily achieved as: 
*/ 

JPanel panel = new JPanel(new GridBagLayout()); 
GridBagConstraints c = new GridBagConstraints(); 
JButton button; 

button = new JButton("Button 1"); 
c.weightx = 0.5; 
c.weighty = 0.5; 
c.fill = GridBagConstraints.BOTH; 
c.gridheight = 2; 
panel.add(button, c); 

button = new JButton("Button 2"); 
c.gridwidth = GridBagConstraints.REMAINDER; 
c.gridheight = 1; 
panel.add(button, c); 

button = new JButton("Button 3"); 
panel.add(button, c); 

手がかりはありますか?GridBagLayoutを必要に応じて動作させることはできません

について、Francesc

答えて

0

あなたの要素にgridxとgridy設定を指定していません。

JPanel panel = new JPanel(new GridBagLayout()); 
GridBagConstraints c = new GridBagConstraints(); 
JButton button; 

button = new JButton("Button 1"); 
c.weightx = 0.5; 
c.weighty = 0.5; 
c.fill = GridBagConstraints.BOTH; 
c.gridx = 0; 
c.gridy = 0; 
panel.add(button, c); 

button = new JButton("Button 2"); 
c.gridwidth = GridBagConstraints.REMAINDER; 
c.gridheight = 2; 
c.gridx = 1; 
c.gridy = 0; 
panel.add(button, c); 

button = new JButton("Button 3"); 
c.gridwidth = 1; 
c.gridx = 0; 
c.gridy = 1; 
panel.add(button, c); 

注:gridxとgridyのデフォルト値は0ですが、常に値を設定することをお勧めします。それは明確であり、デフォルト値に依存すると、コピーや貼り付けなどのときに問題が発生する可能性があります。したがって、この方法はより明確で安全です(ただし、少し冗長です)。

+0

私の場合と同じ結果、試してみましたか? – francesc

0

のGridBagLayoutが(http://www.horstmann.com/articles/Taming_the_GridBagLayout.htmlを参照)diffficulするknowlnさ: のJava SDKの標準レイアウトマネージャーの

、GridBagLayoutのは 最も有用なものですが、その複雑さ もに知られていますプログラマーの心に の恐怖を打つ。複雑さの一部は、 という煩わしさにあり、グリッドバッグの制約を設定しています。

私はこれらの記事を読んでもフォーラムで(例えばhttp://www.java-forums.org/new-java/28857-gridbaglayout-problems-questions.html)を求めるだろう

1

あなたはgridxとgridyを指定する必要があり、デフォルトは0です。

button = new JButton("Button 1"); 
    c.weightx = 0.5; 
    c.weighty = 0.5; 
    c.fill = GridBagConstraints.BOTH; 
    panel.add(button, c); 

    button = new JButton("Button 3"); 
    c.gridy = 1; 
    c.gridwidth = 1; 
    panel.add(button, c); 

    button = new JButton("Button 2"); 
    c.gridx = 1; c.gridy = 0; 
    c.gridwidth = GridBagConstraints.REMAINDER; 
    c.weighty = 1;c.gridheight = 2; 
    panel.add(button, c); 
1

最後のボタンにc.gridxを設定する必要があります。デフォルトでは、コンポーネントは互いに対して配置されます。ボタン3のgridxを0に設定した場合は、最初の列に強制します。

JPanel panel = new JPanel(new GridBagLayout()); 
GridBagConstraints c = new GridBagConstraints(); 
JButton button; 

button = new JButton("Button 1"); 
c.weightx = 1; 
c.weighty = 1; 
c.fill = GridBagConstraints.BOTH; 
panel.add(button, c); 

button = new JButton("Button 2"); 
c.gridwidth = GridBagConstraints.REMAINDER; 
c.gridheight = GridBagConstraints.REMAINDER;   
panel.add(button, c); 

button = new JButton("Button 3"); 
c.gridx = 0; 
c.gridwidth = 1; 
c.gridheight = 1; 
panel.add(button, c); 
+0

コードは機能しますが、ボタン2にはc.gridwidth = GridBagConstraints.REMAINDERがあります。ボタン3は新しい行を開始します、それはそれを指定する必要なしに最初の列です – francesc

1

なぜ代わりのようなGridLayoutsを使用し、ネストされたのJPanel、使用しない:

import java.awt.GridLayout; 
import javax.swing.*; 

public class GridLayoutEg { 
    public static void main(String[] args) { 
     JPanel leftPanel = new JPanel(new GridLayout(0, 1)); 
     leftPanel.add(new JButton("Button 1")); 
     leftPanel.add(new JButton("Button 3")); 

     JPanel mainPanel = new JPanel(new GridLayout(1, 0)); 
     mainPanel.add(leftPanel); 
     mainPanel.add(new JButton("Button 2")); 

     JFrame frame = new JFrame("Foo"); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

するか、そこに私たちが知っている必要があります話によりますか?

0

はい、他は正しいです。 GridXとGridYを手動で設定する必要があります。

また、すべてのコンポーネントに新しいGridBagConstraintsインスタンスを使用することをお奨めします。これは、より複雑なフォームでは、使用している1つのインスタンスで実際に設定されている値を簡単に追跡できます。さらに、毎回すべてを指定する必要があるため、多くのパラメータを持つコンストラクタを使用する場合は、デフォルト値を気にする必要はありません。それに慣れるまでに時間がかかりますが、後で返金されます。

 

    /** 
    * +----------+----------+ 
    * | Button 1 |   | 
    * +----------| Button 2 | 
    * | Button 3 |   | 
    * +----------+----------+ 
    */ 

    JPanel panel = new JPanel(new GridBagLayout()); 

    JButton b1 = new JButton("Button 1"); 
    panel.add(b1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
     GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 
      0), 0, 0)); 

    JButton b2 = new JButton("Button 2"); 
    panel.add(b2, new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0, 
     GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 
      0), 0, 0)); 

    JButton b3 = new JButton("Button 3"); 
    panel.add(b3, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
     GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 
      0), 0, 0)); 
 
関連する問題