请教大家,我想生成类似于计算器的界面,上面的“=”是占用两个格的大小。这里我想用网格包布局GridBagLayout.但是不会用。请问怎么设置组件占2个格子.GridBagConstraints类好像只有设置组件在整个网格中的起始位置(gridx,gridy),但没有指定结束位置啊
急!!!

解决方案 »

  1.   

    刚又看了下api,好像gridheight和gridewidth是用来设置组件占据的垂直和水平单元格个数,但是整个Frame是分成几行几列呢,这个怎么设置啊
      

  2.   

    import java.awt.*;
     import java.util.*;
     import java.applet.Applet; public class GridBagEx1 extends Applet {     protected void makebutton(String name,
                                   GridBagLayout gridbag,
                                   GridBagConstraints c) {
             Button button = new Button(name);
             gridbag.setConstraints(button, c);
             add(button);
         }     public void init() {
             GridBagLayout gridbag = new GridBagLayout();
             GridBagConstraints c = new GridBagConstraints();         setFont(new Font("SansSerif", Font.PLAIN, 14));
             setLayout(gridbag);         c.fill = GridBagConstraints.BOTH;
             c.weightx = 1.0;
             makebutton("Button1", gridbag, c);
             makebutton("Button2", gridbag, c);
             makebutton("Button3", gridbag, c);           c.gridwidth = GridBagConstraints.REMAINDER; //end row
             makebutton("Button4", gridbag, c);         c.weightx = 0.0;                  //reset to the default
             makebutton("Button5", gridbag, c); //another row           c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
             makebutton("Button6", gridbag, c);           c.gridwidth = GridBagConstraints.REMAINDER; //end row
             makebutton("Button7", gridbag, c);           c.gridwidth = 1;                //reset to the default
               c.gridheight = 2;
             c.weighty = 1.0;
             makebutton("Button8", gridbag, c);         c.weighty = 0.0;                  //reset to the default
               c.gridwidth = GridBagConstraints.REMAINDER; //end row
               c.gridheight = 1;               //reset to the default
             makebutton("Button9", gridbag, c);
             makebutton("Button10", gridbag, c);         setSize(300, 100);
         }     public static void main(String args[]) {
               Frame f = new Frame("GridBag Layout Example");
               GridBagEx1 ex1 = new GridBagEx1();           ex1.init();           f.add("Center", ex1);
               f.pack();
               f.setSize(f.getPreferredSize());
               f.show();
         }
     }
      

  3.   

    不行就setlayout(null)  然后每个按钮都可以 setBounds(int x,int y,int width,int heigth)随便放了
      

  4.   

    GridBagConstraints类的weightx,weighty表示权重,到底是什么权重啊