那你就用setBounds(x,y,w,h)JPanel不能用GridBagLayout? 还真没试过,应该能用吧

解决方案 »

  1.   

    我也碰到,不过,它的大小好像会根据显示在button上的字体而自动掉正大小的
      

  2.   

    好像要把Layout设置成null才可以随便调整,但是JButton的大小就被固定了
      

  3.   

    用setBounds(x,y,w,h)
    w 是宽度
    h 是高度
    x 是左坐标
    y 是上坐标
      

  4.   

    JPanel显然可以用GridBagLayout呀,但是需要GridBagConstraints类的实例
    作为约束对象
    GridBagLayout gridbag=new GridBagLayout();
    GridBagConstraints constraints=new GridBagConstraints();
    JPanel pane=new JPanel();
    pane.setLayout(gridbag);
    //再构造buildContraints方法设置gridx等参数JButton b=new JButton("Button");
    buildContraints(contraints,0,0,1,1,100,100)/*(0,0)是左上角,(1,1)是右下角*/
    gridbag.setConstraints(b,contraints);
    pane.add(b);按钮的大小可以通过参数设定
      

  5.   

    使用setBounds(x,y,width,height)
    使用width何height设置长宽