不能让GridBagLayout按需要工作

问题描述:

/** 
* 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

您需要在约束上设置gridx和gridy。查看本教程:http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

您尚未为元素指定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

的GridBagLayout是knowln是diffficul(见http://www.horstmann.com/articles/Taming_the_GridBagLayout.html):

的 的Java SDK的标准布局管理器时,GridBagLayout的是 最有用的,但它的复杂性 也已经知道在 中打击恐惧程序员的心。部分 的复杂性在于设置网格包约束的单调乏味。

我会读这些文章,也请在论坛上(例如http://www.java-forums.org/new-java/28857-gridbaglayout-problems-questions.html

你需要指定的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); 

您必须为最后一个按钮设置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

为什么不改用使用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); 
    } 
} 

还是有更多的,我们需要知道这个故事?

是的,其他人都是对的。您必须手动设置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));