java布局管理器GridBagLayout

       java结构办理器GridBagLayout怎么利用,下面讲解结构办理器应用实例。我写的经验对你进修java有帮忙的话,给我投票或者保藏!

0java结构办理器-borderlayout

0安装NetBeans IDE 8.1

0iReport安装java打印

工具/原料

  • netbeans
  • java

方式/步骤

  1. 1

    新建一个java项目。

    0java结构办理器-borderlayout

  2. 2

    新建一个类,取名jframe2。

  3. 3

    打开jframe2.java这个文件,把代码点窜为:

    public class jframe2 extends javax.swing.JFrame{

        public jframe2(){

            buju();

        }

        private void buju(){

       

        }

        public static void main(String[] arg){

        }

    }

  4. 4

    界说用到的变量。

  5. 5

    在buju这个方式中初始化变量:new 类。

  6. 6

    关头代码:

    this.getContentPane().setLayout(new java.awt.GridBagLayout());

    窗口结构设为GridBagLayout

    gridBagConstraints.gridx = 0;结构0列

    gridBagConstraints.gridy = 0;结构0行

    gridBagConstraints.gridwidth=1;     几多列

    gridBagConstraints.gridheight=1;    几多行

    gridBagConstraints.weightx=1;   分派程度空间

    gridBagConstraints.weighty=1;    分派垂直空间

    gridBagConstraints.fill=java.awt.GridBagConstraints.HORIZONTAL填充,程度,垂直,

    gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);外侧空间

  7. 7

    buju方式中完整代码:

     java.awt.GridBagConstraints gridBagConstraints;

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            getContentPane().setLayout(new java.awt.GridBagLayout());

            jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

            java.awt.GridBagLayout jPanel1Layout = new java.awt.GridBagLayout();

            jPanel1Layout.columnWidths = new int[] {0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0};

            jPanel1Layout.rowHeights = new int[] {0, 5, 0, 5, 0, 5, 0};

            jPanel1.setLayout(jPanel1Layout);

            jLabel1.setText("姓名");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 0;

            gridBagConstraints.gridy = 0;

            

            

            

            jPanel1.add(jLabel1, gridBagConstraints);

            jTextField1.setText("abc");

            jTextField1.setPreferredSize(new java.awt.Dimension(6, 21));

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 2;

            gridBagConstraints.gridy = 0;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            gridBagConstraints.weightx = 1.0;

            jPanel1.add(jTextField1, gridBagConstraints);

            jLabel2.setText("性别");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 4;

            gridBagConstraints.gridy = 0;

            jPanel1.add(jLabel2, gridBagConstraints);

            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "男", "女" }));

            jComboBox1.setMinimumSize(new java.awt.Dimension(6, 21));

            jComboBox1.setPreferredSize(new java.awt.Dimension(6, 21));

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 6;

            gridBagConstraints.gridy = 0;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            gridBagConstraints.weightx = 1.0;

            jPanel1.add(jComboBox1, gridBagConstraints);

            jLabel3.setText("出生日期");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 8;

            gridBagConstraints.gridy = 0;

            jPanel1.add(jLabel3, gridBagConstraints);

            jTextField2.setText("2000-01-01");

            jTextField2.setPreferredSize(new java.awt.Dimension(6, 21));

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 10;

            gridBagConstraints.gridy = 0;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            gridBagConstraints.weightx = 1.0;

            jPanel1.add(jTextField2, gridBagConstraints);

            jLabel4.setText("国籍");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 12;

            gridBagConstraints.gridy = 0;

            jPanel1.add(jLabel4, gridBagConstraints);

            jTextField3.setText("中国");

            jTextField3.setPreferredSize(new java.awt.Dimension(6, 21));

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 14;

            gridBagConstraints.gridy = 0;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            gridBagConstraints.weightx = 1.0;

            jPanel1.add(jTextField3, gridBagConstraints);

            jLabel5.setText("证件名称");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 0;

            gridBagConstraints.gridy = 2;

            jPanel1.add(jLabel5, gridBagConstraints);

            jTextField4.setText("身份证");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 2;

            gridBagConstraints.gridy = 2;

            gridBagConstraints.gridwidth = 5;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            jPanel1.add(jTextField4, gridBagConstraints);

            jLabel6.setText("号码");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 8;

            gridBagConstraints.gridy = 2;

            jPanel1.add(jLabel6, gridBagConstraints);

            jTextField5.setText("123456789");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 10;

            gridBagConstraints.gridy = 2;

            gridBagConstraints.gridwidth = 5;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            jPanel1.add(jTextField5, gridBagConstraints);

            jLabel7.setText("代号");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 0;

            gridBagConstraints.gridy = 4;

            jPanel1.add(jLabel7, gridBagConstraints);

            jTextField6.setText("A1");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 2;

            gridBagConstraints.gridy = 4;

            gridBagConstraints.gridwidth = 5;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            jPanel1.add(jTextField6, gridBagConstraints);

            jLabel8.setText("档案编号");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 8;

            gridBagConstraints.gridy = 4;

            jPanel1.add(jLabel8, gridBagConstraints);

            jTextField7.setText("123456789");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 10;

            gridBagConstraints.gridy = 4;

            gridBagConstraints.gridwidth = 5;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            jPanel1.add(jTextField7, gridBagConstraints);

            jLabel9.setText("邮寄地址");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 0;

            gridBagConstraints.gridy = 6;

            jPanel1.add(jLabel9, gridBagConstraints);

            jTextField8.setText("ABC-123");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 2;

            gridBagConstraints.gridy = 6;

            gridBagConstraints.gridwidth = 5;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            jPanel1.add(jTextField8, gridBagConstraints);

            jLabel10.setText("联系德律风");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 8;

            gridBagConstraints.gridy = 6;

            jPanel1.add(jLabel10, gridBagConstraints);

            jTextField9.setText("852963");

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 10;

            gridBagConstraints.gridy = 6;

            gridBagConstraints.gridwidth = 5;

            gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

            jPanel1.add(jTextField9, gridBagConstraints);

            jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

            jPanel2.setOpaque(false);

            jPanel2.setPreferredSize(new java.awt.Dimension(80, 100));

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.gridx = 16;

            gridBagConstraints.gridy = 0;

            gridBagConstraints.gridheight = 7;

            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;

            jPanel1.add(jPanel2, gridBagConstraints);

            gridBagConstraints = new java.awt.GridBagConstraints();

            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;

            gridBagConstraints.weightx = 1.0;

            gridBagConstraints.weighty = 1.0;

            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);

            getContentPane().add(jPanel1, gridBagConstraints);

            setSize(new java.awt.Dimension(541, 334));

            setLocationRelativeTo(null);

  8. 8

    添加main方式中代码:

    java.awt.EventQueue.invokeLater(new Runnable(){

                @Override

                public void run(){

                    new jframe2().setVisible(true);

                }

            });

  9. 9

    运行这个文件jframe2.

  • 发表于 2018-11-08 00:00
  • 阅读 ( 209 )
  • 分类:其他类型

相关问题

0 条评论

请先 登录 后评论