滨海做网站哪家好,怎么做网站的教程,php做用户登录网站,湖北城市建设职业技术学院教务网站GridBagLayout以表格形式布置容器内的组件#xff0c; 将每个组件放置在每个单元格内#xff0c;而一个单元格可以跨越多个单元格合并成一个单元格#xff0c;即多个单元格可以组合成一个单元格#xff0c;从而实现组件的自由布局。每一个单元格都有各自的属性#xff0c;…GridBagLayout以表格形式布置容器内的组件 将每个组件放置在每个单元格内而一个单元格可以跨越多个单元格合并成一个单元格即多个单元格可以组合成一个单元格从而实现组件的自由布局。每一个单元格都有各自的属性而这些属性由GridBagConstrainsts类的成员变量来定义且GridBagConstriaints中的所有成员变量都是public的。构造函数 GirdBagLayout()建立一个新的GridBagLayout管理器。 GridBagConstraints()建立一个新的GridBagConstraints对象。 GridBagConstraints(int gridx,int gridy,int gridwidth,int gridheight,double weightx,double weighty,int anchor,int fill, Insets insets,int ipadx,int ipady) 建立一个新的GridBagConstraints对象并指定其参数的值。下面提供一个用来设置GridBagConstraints对象各参数的帮助类
import java.awt.GridBagConstraints;
import java.awt.Insets;
/**1. 2. author han3. 功能用来设置GridBagConstraints对象的一些参数4. */
public class GBC extends GridBagConstraints {/*** 初始化左上角位置 gridx,gridy —— 设置组件的位置* gridx0,gridy0时放在0行0列。* * param gridx* param gridy*/public GBC(int gridx, int gridy) {this.gridx gridx;this.gridy gridy;}/*** 初始化左上角位置和所占行数和列数* * gridwidth,gridheight —— 用来设置组件所占的单位长度与高度默认值皆为1。* 可以使用GridBagConstraints.REMAINDER常量* 代表此组件为此行或此列的最后一个 组件而且会占据所有剩余的空间。* * param gridx* param gridy* param gridwidth* param gridheight*/public GBC(int gridx, int gridy, int gridwidth, int gridheight) {this.gridx gridx;this.gridy gridy;this.gridwidth gridwidth;this.gridheight gridheight;}/*** 对齐方式 anchor设置组件在单元格中的对齐方式。* 由以下常量来定义* * GridBagConstraints.CENTER* * GridBagConstraints.EAST* * GridBagConstraints.WEST* * GridBagConstraints.SOUTH* * GridBagConstraints.NORTH* * GridBagConstraints.SOUTHEAST* * GrisBagConstraints.SOUTHWEST* * GridBagConstraints.NORTHEAST* * GridBagConstraints.NORTHWEST* * param anchor* return*/public GBC setAnchor(int anchor) {this.anchor anchor;return this;}/*** 是否拉伸及拉伸方向* * fill当某个组件未能填满单元格时可由此属性设置横向、* 纵向或双向填满。由以下常量来定义* * GridBagConstraints.NONE* * GridBagConstraints.HORIZONTAL* * GridBagConstraints.VERTICAL* * GridBagConstraints.BOTH* * param fill* return*/public GBC setFill(int fill) {this.fill fill;return this;}/*** x和y方向上的增量* * weightx,weighty——用来设置窗口变大时各组件跟着变大的比例。 * 当数字越大表示组件能得到更多的空间默认值皆为00-1。* * param weightx* param weighty* return*/public GBC setWeight(double weightx, double weighty) {this.weightx weightx;this.weighty weighty;return this;}/*** 外部填充* * param distance* return*/public GBC setInsets(int distance) {this.insets new Insets(distance, distance, distance, distance);return this;}/*** 外填充* * insets —— 设置组件之间彼此的间距。* 它有四个参数分别是上左下右默认为(0,0,0,0)。* * param top* param left* param bottom* param right* return*/public GBC setInsets(int top, int left, int bottom, int right) {this.insets new Insets(top, left, bottom, right);return this;}/*** 内填充* * ipadx,ipady —— 设置组件间距默认值为0。* * param ipadx* param ipady* return*/public GBC setIpad(int ipadx, int ipady) {this.ipadx ipadx;this.ipady ipady;return this;}
}
GridBagLayout布局的一个案例—-使三个带颜色的面板一直在Frame窗口的中央显示无论窗口放大还是缩小
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
/*** * author han* * 使用GridBagLayout布局的一个案例* * 功能使三个带颜色的面板一直在Frame窗口的中央显示无论窗口放大还是缩小* */
public class GridBagLayoutTest extends JFrame {private static final long serialVersionUID 1391949900949468015L;private JPanel contentPane;public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {GridBagLayoutTest frame new GridBagLayoutTest();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}public GridBagLayoutTest() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 511, 500);setTitle(GridBagLayout布局案例);contentPane new JPanel();setContentPane(contentPane);GridBagLayout gbl_contentPane new GridBagLayout();contentPane.setLayout(gbl_contentPane);JPanel jPanel1 new JPanel();jPanel1.setSize(300, 100);jPanel1.setBackground(Color.blue);JPanel jPanel2 new JPanel();jPanel2.setSize(300, 300);jPanel2.setBackground(Color.red);JPanel jPanel3 new JPanel();jPanel3.setSize(300, 100);jPanel3.setBackground(Color.YELLOW);//如果加入的是空的面板时需要把内部填充的大小与面板size的大小设为一样。contentPane.add(jPanel1, new GBC(0, 1).setInsets(20, 0, 20, 0).setWeight(1, 0).setIpad(300, 100));contentPane.add(jPanel2, new GBC(0, 2).setInsets(20, 0, 20, 0).setWeight(1, 0).setIpad(300, 100));contentPane.add(jPanel3, new GBC(0, 3).setInsets(20, 0, 20, 0).setWeight(1, 0).setIpad(300, 100));}}