zoukankan      html  css  js  c++  java
  • GridBagLayout占多行效果注意

     如果想要出现按钮2占两行的效果必须按键3、按钮4同时存在且同时可见

    • 如果缺少按钮4,则按钮2不会占两行;
    • 如果缺少按钮3、4,则按钮2也不会占两行。

      

     1 package com.wst.bj;
     2 
     3 import java.awt.GridBagConstraints;
     4 import java.awt.GridBagLayout;
     5 
     6 import javax.swing.JButton;
     7 import javax.swing.JFrame;
     8 
     9 class GridBagText {
    10      
    11     JFrame jframe = new JFrame("搜索");
    12 
    13     JButton jButton = new JButton("按钮1");
    14     JButton jButton2 = new JButton("按钮2");
    15     JButton jButton3 = new JButton("按钮3");
    16     JButton jButton4 = new JButton("按钮4");
    17 
    18     public GridBagText() {
    19 
    20         init();
    21     }
    22     
    23     private void init()
    24     {
    25         FrameUtil.initFram(jframe, 500, 700);
    26         
    27         GridBagLayout gridBagLayout = new GridBagLayout();
    28         jframe.setLayout(gridBagLayout);
    29         GridBagConstraints constraints = new  GridBagConstraints();
    30         
    31         constraints.fill = GridBagConstraints.BOTH;
    32         constraints.weightx = 1.0;
    33         constraints.weighty = 1.0;
    34         
    35         
    36         constraints.gridheight = 1;
    37         constraints.gridwidth = GridBagConstraints.REMAINDER;
    38         gridBagLayout.setConstraints(jButton, constraints);
    39         jframe.add(jButton);
    40         
    41         constraints.gridheight = 2;
    42         constraints.gridwidth = 1;
    43         gridBagLayout.setConstraints(jButton2, constraints);
    44         jframe.add(jButton2);
    45 
    46         constraints.gridheight = 1;
    47         constraints.gridwidth = GridBagConstraints.REMAINDER;
    48         gridBagLayout.setConstraints(jButton3, constraints);
    49         jframe.add(jButton3);
    50         gridBagLayout.setConstraints(jButton4, constraints);
    51         jframe.add(jButton4);
    52         
    53 
    54         jframe.setSize(jframe.getPreferredSize());
    55         jframe.setVisible(true);
    56 //        jframe.pack();
    57     }
    58 }

      

     1 package com.wst.bj;
     2 
     3 import java.awt.GridBagConstraints;
     4 import java.awt.GridBagLayout;
     5 
     6 import javax.swing.JButton;
     7 import javax.swing.JFrame;
     8 
     9 class GridBagText {
    10      
    11     JFrame jframe = new JFrame("搜索");
    12 
    13     JButton jButton = new JButton("按钮1");
    14     JButton jButton2 = new JButton("按钮2");
    15     JButton jButton3 = new JButton("按钮3");
    16 //    JButton jButton4 = new JButton("按钮4");
    17 
    18     public GridBagText() {
    19 
    20         init();
    21     }
    22     
    23     private void init()
    24     {
    25         FrameUtil.initFram(jframe, 500, 700);
    26         
    27         GridBagLayout gridBagLayout = new GridBagLayout();
    28         jframe.setLayout(gridBagLayout);
    29         GridBagConstraints constraints = new  GridBagConstraints();
    30         
    31         constraints.fill = GridBagConstraints.BOTH;
    32         constraints.weightx = 1.0;
    33         constraints.weighty = 1.0;
    34         
    35         
    36         constraints.gridheight = 1;
    37         constraints.gridwidth = GridBagConstraints.REMAINDER;
    38         gridBagLayout.setConstraints(jButton, constraints);
    39         jframe.add(jButton);
    40         
    41         constraints.gridheight = 2;
    42         constraints.gridwidth = 1;
    43         gridBagLayout.setConstraints(jButton2, constraints);
    44         jframe.add(jButton2);
    45 
    46         constraints.gridheight = 1;
    47         constraints.gridwidth = GridBagConstraints.REMAINDER;
    48         gridBagLayout.setConstraints(jButton3, constraints);
    49         jframe.add(jButton3);
    50 //        gridBagLayout.setConstraints(jButton4, constraints);
    51 //        jframe.add(jButton4);
    52         
    53 
    54         jframe.setSize(jframe.getPreferredSize());
    55         jframe.setVisible(true);
    56 //        jframe.pack();
    57     }
    58 }

     1 package com.wst.bj;
     2 
     3 import java.awt.GridBagConstraints;
     4 import java.awt.GridBagLayout;
     5 
     6 import javax.swing.JButton;
     7 import javax.swing.JFrame;
     8 
     9 class GridBagText {
    10      
    11     JFrame jframe = new JFrame("搜索");
    12 
    13     JButton jButton = new JButton("按钮1");
    14     JButton jButton2 = new JButton("按钮2");
    15 //    JButton jButton3 = new JButton("按钮3");
    16 //    JButton jButton4 = new JButton("按钮4");
    17 
    18     public GridBagText() {
    19 
    20         init();
    21     }
    22     
    23     private void init()
    24     {
    25         FrameUtil.initFram(jframe, 500, 700);
    26         
    27         GridBagLayout gridBagLayout = new GridBagLayout();
    28         jframe.setLayout(gridBagLayout);
    29         GridBagConstraints constraints = new  GridBagConstraints();
    30         
    31         constraints.fill = GridBagConstraints.BOTH;
    32         constraints.weightx = 1.0;
    33         constraints.weighty = 1.0;
    34         
    35         
    36         constraints.gridheight = 1;
    37         constraints.gridwidth = GridBagConstraints.REMAINDER;
    38         gridBagLayout.setConstraints(jButton, constraints);
    39         jframe.add(jButton);
    40         
    41         constraints.gridheight = 2;
    42         constraints.gridwidth = 1;
    43         gridBagLayout.setConstraints(jButton2, constraints);
    44         jframe.add(jButton2);
    45 
    46 //        constraints.gridheight = 2;
    47 //        constraints.gridwidth = GridBagConstraints.REMAINDER;
    48 //        gridBagLayout.setConstraints(jButton3, constraints);
    49 //        jframe.add(jButton3);
    50 //        gridBagLayout.setConstraints(jButton4, constraints);
    51 //        jframe.add(jButton4);
    52         
    53 
    54         jframe.setSize(jframe.getPreferredSize());
    55         jframe.setVisible(true);
    56 //        jframe.pack();
    57     }
    58 }
  • 相关阅读:
    【支付宝支付】手机网页内 支付宝支付实现过程
    【微信支付】微信端的手机网页支付 开发流程
    【Linux】Linux下 环境变量/etc/profile、/etc/bashrc、~/.bashrc的区别【转】
    SLF4J 和 Logback 在 Maven 项目中的使用方法
    打造Spring Cloud构建微服务架构的最全资料
    WebApi安全性 使用TOKEN+签名验证
    <meta http-equiv = "X-UA-Compatible" content = "IE=edge,chrome=1" />的意义
    request.getParameter(“参数名”) 中文乱码解决方法
    Mysql的timestamp类型,自动记录数据的更新时间
    根据身份证号,取得行政区划的Javascript实现
  • 原文地址:https://www.cnblogs.com/LiuYanYGZ/p/6158349.html
Copyright © 2011-2022 走看看