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);
本文介绍了一个使用Eclipse SWT/JFace实现控件显示与隐藏的示例程序。通过按钮点击事件来切换一个组(Group)的可见性和排除状态,实现了控件的隐藏与重新显示,并详细解释了实现过程中的关键步骤。
2801

被折叠的 条评论
为什么被折叠?



