⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gridbaglayoutdemo.java

📁 这是孙卫琴在"Java与面向对象编程"一书的源代码。
💻 JAVA
字号:
import java.awt.*;
public class GridBagLayoutDemo extends Frame {
  private GridBagLayout gridbag = new GridBagLayout();
  private GridBagConstraints c = new GridBagConstraints();
  private Button[] buttons=new Button[7];
  public GridBagLayoutDemo(String title) {
    super(title);
    setLayout(gridbag);

    for(int i=0;i<buttons.length;i++)
      buttons[i]=new Button("button"+i);

    c.fill=GridBagConstraints.BOTH;
    addComponent(buttons[0],0,0,1,3);

    c.fill=GridBagConstraints.HORIZONTAL;
    addComponent(buttons[1], 0,1,2,1);

    addComponent(buttons[2], 2,1,2,1);

    c.weightx=1000;  //设定水平方向的重量
    c.weighty=1;  //设定垂直方向的重量
    c.fill=GridBagConstraints.BOTH;
    addComponent(buttons[3], 1,1,1,1);

    c.weightx=0;
    c.weighty=0;
    addComponent(buttons[4], 1,2,1,1);

    addComponent(buttons[5], 3,0,2,1);
    addComponent(buttons[6], 3,2,1,1);
    pack();
    setVisible(true);
  }
  protected void addComponent(Component component,
                            int row,int column,
                            int width,int height) {
    c.gridx=column;
    c.gridy=row;
    c.gridwidth=width;
    c.gridheight=height;
    gridbag.setConstraints(component, c);
    add(component);
  }
public static void main(String args[]) {
     new GridBagLayoutDemo ("Hello");
  }
}


/****************************************************
 * 作者:孙卫琴                                     *
 * 来源:<<Java面向对象编程>>                       *
 * 技术支持网址:www.javathinker.org                *
 ***************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -