如图,我想使用GridBagLayout实现图上的布局,求高人指点。

解决方案 »

  1.   

    表示图片看不到,GridBagLayout在Java核心技术卷一中有详细介绍,在Java核心技术卷一的P360页,还有一个封装好的GBC类来简化这个布局的使用,楼主可以看看
      

  2.   

    我写了一个,用的就是GBCGBC.javaimport java.awt.*;/**
     * This class simplifies the use of the GridBagConstraints class.
     * @version 1.01 2004-05-06
     * @author Cay Horstmann
     */
    public class GBC extends GridBagConstraints
    {
       /**
        * Constructs a GBC with a given gridx and gridy position and all other grid
        * bag constraint values set to the default.
        * @param gridx the gridx position
        * @param gridy the gridy position
        */
       public GBC(int gridx, int gridy)
       {
          this.gridx = gridx;
          this.gridy = gridy;
       }   /**
        * Constructs a GBC with given gridx, gridy, gridwidth, gridheight and all
        * other grid bag constraint values set to the default.
        * @param gridx the gridx position
        * @param gridy the gridy position
        * @param gridwidth the cell span in x-direction
        * @param gridheight the cell span in y-direction
        */
       public GBC(int gridx, int gridy, int gridwidth, int gridheight)
       {
          this.gridx = gridx;
          this.gridy = gridy;
          this.gridwidth = gridwidth;
          this.gridheight = gridheight;
       }   /**
        * Sets the anchor. 
        * @param anchor the anchor value
        * @return this object for further modification
        */
       public GBC setAnchor(int anchor)
       {
          this.anchor = anchor;
          return this;
       }   /**
        * Sets the fill direction. 
        * @param fill the fill direction
        * @return this object for further modification
        */
       public GBC setFill(int fill)
       {
          this.fill = fill;
          return this;
       }   /**
        * Sets the cell weights.
        * @param weightx the cell weight in x-direction
        * @param weighty the cell weight in y-direction
        * @return this object for further modification
        */
       public GBC setWeight(double weightx, double weighty)
       {
          this.weightx = weightx;
          this.weighty = weighty;
          return this;
       }   /**
        * Sets the insets of this cell. 
        * @param distance the spacing to use in all directions
        * @return this object for further modification
        */
       public GBC setInsets(int distance)
       {
          this.insets = new Insets(distance, distance, distance, distance);
          return this;
       }   /**
        * Sets the insets of this cell.
        * @param top the spacing to use on top
        * @param left the spacing to use to the left
        * @param bottom the spacing to use on the bottom
        * @param right the spacing to use to the right
        * @return this object for further modification
        */
       public GBC setInsets(int top, int left, int bottom, int right)
       {
          this.insets = new Insets(top, left, bottom, right);
          return this;
       }   /**
        * Sets the internal padding
        * @param ipadx the internal padding in x-direction
        * @param ipady the internal padding in y-direction
        * @return this object for further modification
        */
       public GBC setIpad(int ipadx, int ipady)
       {
          this.ipadx = ipadx;
          this.ipady = ipady;
          return this;
       }
    }
    TestFrame.javaimport java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class TestFrame extends JFrame
    {
    private JTree tree = new JTree();
    private JTextArea taText = new JTextArea();
    private JTextField tfText = new JTextField();
    private JButton btnAdd = new JButton("添加");
    private JButton btnDel = new JButton("删除");
    private JButton btnChange = new JButton("修改");
    private JButton btnClearAll = new JButton("清除全部");
    private JLabel lbl = new JLabel("这里有个JLabel");

    public TestFrame()
    {
    this.setLayout(new GridBagLayout());

    add(new JScrollPane(tree), new GBC(0, 0, 1, 2).setFill(GBC.BOTH).setWeight(1, 1));
    add(taText, new GBC(1, 0).setFill(GBC.BOTH).setWeight(1, 1));

    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(tfText, new GBC(0, 0, 4, 1).setFill(GBC.HORIZONTAL));
    panel.add(btnAdd, new GBC(0, 1).setAnchor(GBC.CENTER));
    panel.add(btnDel, new GBC(1, 1).setAnchor(GBC.CENTER));
    panel.add(btnChange, new GBC(2, 1).setAnchor(GBC.CENTER));
    panel.add(btnClearAll, new GBC(3, 1).setAnchor(GBC.CENTER));
    panel.add(lbl, new GBC(0, 2, 4, 1).setFill(GBC.HORIZONTAL).setWeight(1, 1));
    add(panel, new GBC(1, 1));
    }

    public static void main(String[] args)
    {
    JFrame frame = new TestFrame();
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
      

  3.   

    谢谢6楼的代码,但是我对
    gridx
    gridy
    gridwidth
    gridheight
    fill
    anchor
    weightx
    weighty
    ipadx
    ipady
    的作用还是不理解,API上面的解释太抽象,我都看不懂。您能帮我解释一下吗?或者有相关方面的资料推荐一下,谢谢你。
      

  4.   


    GridBagLayout把一个界面分为m行n列的网格gridx表示组件位于第几列,gridy表示组件位于第几行
    gridwidth表示组件占几列,gridheight表示组件占几行
    fill指定当组件小于单元格大小时的填充方式
    anchor表示当组件小于单元格大小时的对齐方式
    weighx表示当界面有收缩放大时,组件的宽度收缩放大的程度,weighty表示组件高度收缩放大的程度
    ipadx表示组件最小的宽度,ipady表示组件最小的高度如果还是有疑问,可以从网上下《Java核心技术》卷一的电子书,看看里面关于GridBagLayout的详细介绍,我用的GBC也是从它那里拷贝过来的
      

  5.   

    我看了Java核心技术卷,对这些用法明白了不少,谢谢你。