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

📄 relativedemo.java

📁 这是孙卫琴在"Java与面向对象编程"一书的源代码。
💻 JAVA
字号:
import java.awt.*;
import java.util.*;
public class RelativeDemo extends Frame {
  public RelativeDemo(String title) {
    super(title);

    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(gridbag);
    c.fill = GridBagConstraints.BOTH;
    c.weightx =1;
    addComponent("Button1", gridbag, c);
    addComponent("Button2", gridbag, c);
    addComponent("Button3", gridbag, c);
    c.gridwidth = GridBagConstraints.REMAINDER; //最后一个组件
    addComponent("Button4", gridbag, c);
    c.weightx = 0.0; //恢复为默认值
    addComponent("Button5", gridbag, c); 
    c.gridwidth = GridBagConstraints.RELATIVE; //倒数第二个组件
    addComponent("Button6", gridbag, c);
    c.gridwidth = GridBagConstraints.REMAINDER; //最后一个组件
    addComponent("Button7", gridbag, c);

    c.gridwidth = 1; //恢复为默认值
    c.gridheight = 2;
    c.weighty = 1.0;
    addComponent("Button8", gridbag, c);
    c.weighty = 0.0; //恢复为默认值
    c.gridwidth = GridBagConstraints.REMAINDER; //最后一个组件
    c.gridheight = 1; //恢复为默认值
    addComponent("Button9", gridbag, c);
    addComponent("Button10", gridbag, c);

    pack();
    setVisible(true);
  }
  protected void addComponent(String name,
                            GridBagLayout gridbag,
                            GridBagConstraints c) {
    Button button = new Button(name);
    gridbag.setConstraints(button, c);
    add(button);
  }
  public static void main(String args[]) {
     new RelativeDemo("Hello");
  }
}


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

⌨️ 快捷键说明

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