swt/jface控件的隐藏与显示

swt/jface控件的隐藏与显示部分源代码:

 

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

/**
 * Test.java
 *
 * 2007-12-27 下午03:34:16
 *
 * Copyright 2007 Sigmasoft, Inc. All rights reserved.
 * Sigmasoft PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Class instruction
 * 
 * @author fangyao
 * 
 */

public class Test {
 public static void main(String[] args) {
  exclude();
 }

 // exclude
 private static void exclude() {
  Display display = Display.getDefault();
  Shell shell = new Shell(display);
  final GridLayout gridLayout = new GridLayout();
  shell.setLayout(gridLayout);

  final Group reportGroup = new Group(shell, SWT.SHADOW_IN);
  final Button specifyFile = new Button(shell, SWT.PUSH | SWT.LEFT);
  // specifyFile.setVisible(false);
  specifyFile.setText("指定文件");
  GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
  // gd.exclude = true;
  specifyFile.setLayoutData(gd);

  specifyFile.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    // 必须同时设置这两个属性才能实现隐藏,显示也一样
    // 得到GridData
    GridData gd = (GridData) reportGroup.getLayoutData();
    gd.exclude = !gd.exclude;
    // 得到visible
    boolean visible = reportGroup.getVisible();
    visible = !visible;
    reportGroup.setVisible(visible);
    reportGroup.getParent().layout();

   }

  });

  reportGroup.setText("请选择");
  GridLayout layout = new GridLayout(2, false);
  reportGroup.setLayout(layout);
  gd = new GridData(GridData.FILL_HORIZONTAL);
  // gd.exclude = true;
  reportGroup.setLayoutData(gd);
  // reportGroup.setVisible(false);
  reportGroup.getParent().layout();

  shell.setSize(300, 200);

  shell.open();
  // shell.layout();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }

  display.dispose();
 }
}

如果,要一启动就隐藏某个控件,还可以把注释掉的两行去掉gd.exclude = true;和reportGroup.setVisible(false);
shell.layout()是对界面进行刷新动作。

注意:
1.在对控件应用属性时,要使用补充类XXXData,必须先对父容器控件设置为

XXXLayout();通过obj.setLayout()方法设置。否则,将看不到控件。
2.就像注释说得那样,必须同时设置gd.exclude=true和

reportGroup.setVisible(false);才能时控件真正隐藏。显示的时候也一样需

要把设置gd.exclude=false;和reportGroup.setVisible(true);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值