GridBagLayout的使用

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();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值