注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

让一切都逝去吧

排骨炖泥菜/猪肝炒苹果/鱼籽狗肉汤/狗头薏米汤

 
 
 

日志

 
 

jdk1.6的Grouplayout  

2009-09-09 13:54:18|  分类: Swing |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
        jdk1.6新增的一种布局,把组件分组,然后水平和垂直方向定义相对位置。试编写下面这个组件:

 jdk1.6的Grouplayout - ljhzzyx - 怀念外婆屋后的柚子树
代码,组件定义:
  frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JLabel label;
  label = new JLabel();
  label.setText("输入 : ");

  inputTextField = new JTextField();

  JButton submitButton;
  submitButton = new JButton();
  submitButton.setText("查找字符");

  JButton cancelButton;
  cancelButton = new JButton();
  cancelButton.setText("取消");

  JCheckBox checkBox;
  checkBox = new JCheckBox();
  checkBox.setText("New JCheckBox");

  JCheckBox checkBox_1;
  checkBox_1 = new JCheckBox();
  checkBox_1.setText("New JCheckBox");

  JCheckBox checkBox_2;
  checkBox_2 = new JCheckBox();
  checkBox_2.setText("New JCheckBox");

  JCheckBox checkBox_3;
  checkBox_3 = new JCheckBox();
  checkBox_3.setText("New JCheckBox");

使用拖拽式编程工具得到的布局代码如下:
/* 拖拽生成的代码,真是恐怖,用 NetBeans 生成的也是这样子
  final GroupLayout groupLayout = new GroupLayout((JComponent) frame.getContentPane());
  groupLayout.setHorizontalGroup(
   groupLayout.createParallelGroup(GroupLayout.LEADING)
    .add(groupLayout.createSequentialGroup()
     .addContainerGap()
     .add(label)
     .addPreferredGap(LayoutStyle.RELATED)
     .add(groupLayout.createParallelGroup(GroupLayout.LEADING)
      .add(groupLayout.createSequentialGroup()
       .add(inputTextField, GroupLayout.PREFERRED_SIZE, 342, GroupLayout.PREFERRED_SIZE)
       .addPreferredGap(LayoutStyle.RELATED, 18, Short.MAX_VALUE)
       .add(submitButton))
      .add(groupLayout.createSequentialGroup()
       .add(groupLayout.createParallelGroup(GroupLayout.LEADING)
        .add(checkBox)
        .add(checkBox_2))
       .add(61, 61, 61)
       .add(groupLayout.createParallelGroup(GroupLayout.LEADING)
        .add(checkBox_3)
        .add(groupLayout.createSequentialGroup()
         .add(checkBox_1)
         .addPreferredGap(LayoutStyle.RELATED, 63, Short.MAX_VALUE)
         .add(cancelButton)))))
     .addContainerGap())
  );
  groupLayout.setVerticalGroup(
   groupLayout.createParallelGroup(GroupLayout.LEADING)
    .add(groupLayout.createSequentialGroup()
     .addContainerGap()
     .add(groupLayout.createParallelGroup(GroupLayout.BASELINE)
      .add(submitButton)
      .add(inputTextField, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
      .add(label))
     .add(15, 15, 15)
     .add(groupLayout.createParallelGroup(GroupLayout.BASELINE)
      .add(cancelButton)
      .add(checkBox)
      .add(checkBox_1))
     .add(10, 10, 10)
     .add(groupLayout.createParallelGroup(GroupLayout.BASELINE)
      .add(checkBox_2)
      .add(checkBox_3))
     .addContainerGap(37, Short.MAX_VALUE))
  );
  frame.getContentPane().setLayout(groupLayout);
*/
这样的代码可读性太差了,手动编写的代码如下:
  final GroupLayout groupLayout = new GroupLayout((JComponent) frame.getContentPane());
  frame.getContentPane().setLayout(groupLayout);
  //自动设置组件、容器(组)之间的间隙
  groupLayout.setAutocreateGaps(true);
  groupLayout.setAutocreateContainerGaps(true);
  
  //LEADING -- 左对齐    BASELINE -- 底部对齐  CENTER -- 中心对齐
  GroupLayout.ParallelGroup hpg2a = groupLayout.createParallelGroup(GroupLayout.LEADING);
  hpg2a.add(checkBox);
  hpg2a.add(checkBox_1);
  
  GroupLayout.ParallelGroup hpg2b = groupLayout.createParallelGroup(GroupLayout.LEADING);
  hpg2b.add(checkBox_2);
  hpg2b.add(checkBox_3);
  
  GroupLayout.SequentialGroup hpg2h = groupLayout.createSequentialGroup();
  hpg2h.add(hpg2a).add(hpg2b);
  
  GroupLayout.ParallelGroup hpg2 = groupLayout.createParallelGroup(GroupLayout.LEADING);
  hpg2.add(inputTextField).add(hpg2h);
  
  GroupLayout.ParallelGroup hpg3 = groupLayout.createParallelGroup(GroupLayout.LEADING);
  hpg3.add(submitButton).add(cancelButton);
  
  //设置水平布局
  groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup().add(label).add(hpg2).add(hpg3));
  
  //设置按钮水平一样宽
  groupLayout.linkSize(new Component[]{submitButton, cancelButton});
  //groupLayout.linkSize(new Component[]{checkBox, checkBox_1, checkBox_2, checkBox_3});
  
  GroupLayout.ParallelGroup vpg1 = groupLayout.createParallelGroup(GroupLayout.LEADING);
  vpg1.add(label).add(inputTextField).add(submitButton);
  GroupLayout.ParallelGroup vpg2 = groupLayout.createParallelGroup(GroupLayout.LEADING);
  vpg2.add(checkBox).add(checkBox_2).add(cancelButton);
  GroupLayout.ParallelGroup vpg3 = groupLayout.createParallelGroup(GroupLayout.LEADING);
  vpg3.add(checkBox_1).add(checkBox_3);
  
  //设置垂直布局
  groupLayout.setVerticalGroup(groupLayout.createSequentialGroup().add(vpg1).add(vpg2).add(vpg3));
  
  String laf = UIManager.getSystemLookAndFeelClassName();
  try {
       UIManager.setLookAndFeel(laf);
  } catch (ClassNotFoundException e) {
       e.printStackTrace();
  } catch (InstantiationException e) {
       e.printStackTrace();
  } catch (IllegalAccessException e) {
       e.printStackTrace();
  } catch (UnsupportedLookAndFeelException e) {
       e.printStackTrace();
  }
  frame.pack();

  评论这张
 
阅读(2993)| 评论(2)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017