📄 gridbaglayouttest.java~5~
字号:
package chapter7.layout;
import java.awt.*;
import javax.swing.JButton;
public class GridBagLayoutTest
extends Frame
{
GridBagLayout gridBagLayout1 = new GridBagLayout();
public GridBagLayoutTest()
{
try
{
jbInit();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
private void jbInit()
throws Exception
{
GridBagConstraints gbc = new GridBagConstraints(); // 使 用 类 GridBagConstriants
setLayout(new GridBagLayout()); //设 定 外观 管 理 器 为 GridBagLayout 外 观 管 理 器
gbc.fill = GridBagConstraints.BOTH; // 所 有 的 按 钮 都 会 把 分 配 的 剩 余 空 间 填 满
gbc.gridwidth = 1; // ★ 设 置 第 一 个 按钮 的 大 小
gbc.gridheight = 1; // ★
Button Button1 = new Button(" 东 ");
( (GridBagLayout) getLayout()).setConstraints(Button1, gbc);
add(Button1);
gbc.gridwidth = GridBagConstraints.REMAINDER;
// ★ 第 二 个 按 钮 填 满 整 行 空 间
Button Button2 = new Button(" 西 ");
( (GridBagLayout) getLayout()).setConstraints(Button2, gbc);
add(Button2);
gbc.gridheight = 4; // 设 置 第 三 个 按钮 的 大小
gbc.gridwidth = 1;
Button Button3 = new Button(" 南 ");
( (GridBagLayout) getLayout()).setConstraints(Button3, gbc);
add(Button3);
gbc.gridheight = 2; // 设 置 第 四 个 按钮 的 大小
gbc.gridwidth = 2; // GridBagConstraints.REMAINDER;
Button Button4 = new Button(" 北 ");
( (GridBagLayout) getLayout()).setConstraints(Button4, gbc);
add(Button4);
gbc.gridwidth = GridBagConstraints.REMAINDER;
Button Button5 = new Button(" 中 ");
( (GridBagLayout) getLayout()).setConstraints(Button5, gbc);
add(Button5);
gbc.insets = new Insets(5, 6, 7, 8); // ★ 设置
Button Button6 = new Button(" 好 酒 在张弓 ");
( (GridBagLayout) getLayout()).setConstraints(Button6, gbc);
add(Button6);
}
public static void main(String[] args)
{
GridBagLayoutTest gridbaglayouttest = new GridBagLayoutTest();
gridbaglayouttest.setSize(300, 300);
gridbaglayouttest.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -