日期:2014-05-20  浏览次数:20709 次

swing 布局问题,有兴趣请进,望指教
我用 GridBagLayout 布局,想实现以下效果:

但实际上,我得出的结果却是这样:


如下代码,谢谢:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
  
@SuppressWarnings("serial")
public class GridBagLayoutExample extends JPanel {
private JButton top    = new JButton() ;
private JButton left   = new JButton() ;
private JButton right1 = new JButton() ;
private JButton right2 = new JButton() ;
public GridBagLayoutExample() {
GridBagLayout gridBag = new GridBagLayout() ;
this.setLayout(gridBag);
GridBagConstraints gridBagC = new GridBagConstraints() ;

gridBagC.fill = GridBagConstraints.HORIZONTAL ;
gridBagC.gridx = 0 ;
gridBagC.gridy = 0 ;
gridBagC.gridwidth = 2 ;
gridBag.setConstraints(top, gridBagC);
this.add(top) ;

gridBagC.gridx = 0 ;
gridBagC.gridy = 1 ;
gridBagC.gridheight = 2 ;
gridBagC.fill = GridBagConstraints.VERTICAL ;
gridBag.setConstraints(left, gridBagC);
this.add(left) ;

gridBagC.gridx = 1 ;
gridBagC.gridy = 1 ;
gridBag.setConstraints(right1, gridBagC);
this.add(right1);

gridBagC.gridx = 1 ;
gridBagC.gridy = 2 ;
gridBag.setConstraints(right2, gridBagC);
this.add(right2);
}
public static void main(String[] args) {
JFrame f = new JFrame("GridBagLayoutExample");
JPanel p = new GridBagLayoutExample();
f.getContentPane().add(p , BorderLayout.CENTER);
f.setSize(230, 230);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}

}

我不懂我错在哪,有兴趣,望指教 !
------解决方案--------------------
你一直在用一个GridBagConstraints对象-gridBagC
所以你的top、left、right1、right2都是一个对象

修改 每次都新new一个

    public GridBagLayoutExample() {
        GridBagLayout gridBag = new GridBagLayout() ;
        this.setLayout(gridBag);
        GridBagConstraints gridBagC = new GridBagConstraints() ;
         
        gridBagC.fill = GridBagConstraints.HORIZONTAL ;
        gridBagC.gridx = 0 ;
        gridBagC.gridy = 0 ;
        gridBagC.gridwidth = 2 ;
        gridBag.setConstraints(top, gridBagC);
        this.add(top) ;
         
        gridBagC = new GridBagConstraints() ;
        gridBagC.gridx = 0 ;
        gridBagC.gridy = 1 ;
        gridBagC.gridheight = 2 ;
        gridBagC.fill = GridBagConstraints.VERTICAL ;
        gridBag.setConstraints(left, gridBagC);
        this.add(left) ;
 
        gridBagC = new GridBagConstraints() ;
        gridBagC.gridx = 1 ;
        gridBagC.gridy = 1 ;
        gridBag.setConstraints(right1, gridBagC);
        this.add(right1);
 
        gridBagC = new GridBagConstraints() ;
        gridBagC.gridx = 1 ;
        gridBagC.gridy = 2 ;
        gridBag.setConstraints(right2, gridBagC);
        this.add(right2);
    }

------解决方案--------------------
主要原因是因为你之前更改了height width 但是下一个button的时候 没有把width和height改回来.
例如你在top的时候
gridBagC.gridwidth = 2 ;
但是你在left的时候没有把width改回1
gridBagC.gridwidth = 1;
下面是left的时候改的height
gridBagC.gridheight = 2 ;
在right1的时候要把height改回1
gridBagC.gridheight = 1 ;

这样就行了.
不过为了防止这种事情发生
一般都是分别对每个button新建一个GridBagConstraints的