2010-12-05 115 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按需要工作

關於法國sc

回答

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 2010-12-05 12:56:56

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 shoukd開始一個新行,這是第一列,不需要指定它 – francesc 2010-12-05 17:37:15

1

爲什麼不改用使用GridLayouts嵌套JPanels,如:

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實例,因爲在更復雜的形式中,您可以輕鬆地跟蹤您正在使用的實例中設置的值。另外,如果使用具有多個參數的構造函數,則不必關心默認值,因爲每次都必須指定它們。習慣它需要一定的時間,但稍後會回報。

 

    /** 
    * +----------+----------+ 
    * | 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));