java gbc_Java中GBC方法

展开全部

GBC类继承于GridBagConstraints这样做的目的是简化每次对GridBagConstraints对象的直接操作带来的62616964757a686964616fe4b893e5b19e31333337613132繁琐,而GBC的各个set方法返回的都是一个GBC对象因此可以接连调用set方法。GBC类的代码如下:

public class GBC extends GridBagConstraints

{

//初始化左上角位置

public GBC(int gridx, int gridy)

{

this.gridx = gridx;

this.gridy = gridy;

}

//初始化左上角位置和所占行数和列数

public GBC(int gridx, int gridy, int gridwidth, int gridheight)

{

this.gridx = gridx;

this.gridy = gridy;

this.gridwidth = gridwidth;

this.gridheight = gridheight;

}

//对齐方式

public GBC setAnchor(int anchor)

{

this.anchor = anchor;

return this;

}

//是否拉伸及拉伸方向

public GBC setFill(int fill)

{

this.fill = fill;

return this;

}

//x和y方向上的增量

public GBC setWeight(double weightx, double weighty)

{

this.weightx = weightx;

this.weighty = weighty;

return this;

}

//外部填充

public GBC setInsets(int distance)

{

this.insets = new Insets(distance, distance, distance, distance);

return this;

}

//外填充

public GBC setInsets(int top, int left, int bottom, int right)

{

this.insets = new Insets(top, left, bottom, right);

return this;

}

//内填充

public GBC setIpad(int ipadx, int ipady)

{

this.ipadx = ipadx;

this.ipady = ipady;

return this;

}

}

运行效果:

cfb39a92f17ee22d4a9fc8d22b28027e.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GridBagLayout是Java的一个布局管理器,它可以让我们灵活地布置组件,同时也可以控制组件的大小和位置。下面是GridBagLayout的一些常见应用: 1. 创建一个GridBagLayout对象 ```java GridBagLayout gbl = new GridBagLayout(); ``` 2. 将GridBagLayout设置为容器的布局管理器 ```java JPanel panel = new JPanel(); panel.setLayout(gbl); ``` 3. 创建一个GridBagConstraints对象,用于控制组件的位置和大小 ```java GridBagConstraints gbc = new GridBagConstraints(); ``` 4. 设置组件的位置和大小 ```java gbc.gridx = 0; //组件在网格的横向位置 gbc.gridy = 0; //组件在网格的纵向位置 gbc.gridwidth = 1; //组件跨越的列数 gbc.gridheight = 1; //组件跨越的行数 gbc.weightx = 1; //组件在横向上的权重,用于控制横向的拉伸 gbc.weighty = 1; //组件在纵向上的权重,用于控制纵向的拉伸 gbc.fill = GridBagConstraints.BOTH; //组件的拉伸方式,可以是NONE、BOTH、HORIZONTAL、VERTICAL ``` 5. 将组件添加到容器 ```java JButton button = new JButton("Button"); gbl.setConstraints(button, gbc); panel.add(button); ``` 6. 重复步骤4和步骤5,添加其他的组件 ```java JLabel label = new JLabel("Label"); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0; gbc.weighty = 0; gbc.fill = GridBagConstraints.NONE; gbl.setConstraints(label, gbc); panel.add(label); ``` 最后,我们就可以得到一个使用GridBagLayout布局管理器的Java应用程序,该程序可以根据我们的要求布置组件,并控制它们的大小和位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值