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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章