package ch01.setion2.ex1;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class GridBagImage extends JFrame {
JLabel JLabelName = new JLabel("姓名");
JLabel JLabelName1 = new JLabel("Name");
JTextField JTextName = new JTextField();
JLabel JLabelSex=new JLabel("性别");
JComboBox JComboBoxSex=new JComboBox();
JLabel JLabelDress = new JLabel("住址");
JTextField JTextDress = new JTextField();
JButton JButtonComfire=new JButton("确定");
JButton JButtonExit=new JButton("取消");
/**
* public GridBagConstraints(int gridx,//定义初始的位置(注意是单元位置,不是像素)
int gridy,//同上,两个都是以左上角的位置为原点。
int gridwidth,//行单元数(这里是行单元数,0表示行单元到此至,1表示行占有1个单元,2表示行占有2个单元)
int gridheight,//列单元数
double weightx,//组间宽,权重。即,默认情况下 weightx=0,显示的是组间最小大小。改变weightx的值,
//组件会在最小大小和weightx之间计算权重。若权重合适,则会填满区域空间。过大,就会在最小大小和权重之间选择
double weighty,//同上
int anchor,//指定组间的位置。(EAST,WEST,SOUTH,NORTH,CENTER)绝对值
//参考有三个标准
* 绝对值
* 相对于方向的值
* 相对于基线的值
int fill,//定义该组间的大小,有四种情况(用于调整组件的大小)
* NONE 默认
* HORIZONTAL 宽不变
* VERTICAL 高不变
* BOTH 完全充满
Insets insets,//指定该组件与外部空间的间隔大小
int ipadx,//制定布局中中组间的内部填充
int ipady)
*/
// GridBagConstraints gc=new GridBagConstraints();
// GridBagLayout grid=new GridBagLayout();
public GridBagImage() {
this.setLayout(new GridBagLayout());
// gc.fill=GridBagConstraints.HORIZONTAL;
// gc.anchor=GridBagConstraints.CENTER;
// gc.insets=new Insets(0,5,0,0);
// grid.setConstraints(JLabelName,gc);
// this.add(JLabelName);
this.add(JLabelName, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 5, 0, 0), 0, 0));
// this.add(JLabelName1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
// GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
// new Insets(0, 5, 0, 0), 0, 0));
this.add(JTextName, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 5, 0, 0), 0, 0));
JComboBoxSex.addItem("男");
JComboBoxSex.addItem("女");
this.add(JLabelSex, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 5, 0, 0), 0, 0));
this.add(JComboBoxSex, new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(0, 5, 0, 0), 0, 0));
this.add(JLabelDress, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 5, 0, 0), 0, 0));
this.add(JTextDress, new GridBagConstraints(1, 1, 0, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(5, 5, 0, 0), 0, 0));
this.add(JButtonComfire, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.EAST, GridBagConstraints.NONE,
new Insets(5, 5, 0, 0), 0, 0));
this.add(JButtonExit, new GridBagConstraints(3, 3, 1, 1, 1.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(5, 5, 0, 5), 0, 0));
setTitle("GridBagLayout 演示");
this.setVisible(true);
this.setSize(300, 140);
// this.setSize(628,400);
// this.setSize(400,600);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new GridBagImage();
}
}
GridBagLayout的使用
最新推荐文章于 2022-06-02 13:07:01 发布