站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题Eclipse插件开发系列5.SWT中的布局管理器(3)

Eclipse插件开发系列5.SWT中的布局管理器(3)

摘要: Eclipse插件开发系列5.SWT中的布局管理器(3)

*************************************************************************************
作者:陈刚,桂林人,97年毕业于广西师范大学数学系,专注于java平台。现(2004.2-?)暂在IBM中国研究中心进行Eclipse插件的开发。
Email:
glchengang@163.com
blog: glchengang.yeah.net
*************************************************************************************

 StackLayout称做堆栈式布局,所谓堆栈式就象一付叠在一起的扑克牌,一叠UI面板只显示位于最上面的那个面板。如下图,是Eclipse中比较典型的用到堆栈式布局的界面(打开方法:主菜单窗口首选项)。右边部分就是一个堆栈式的布局,当左边选不同的项,右边就显示这一项相应的面板。

?

StackLayout布局的使用是比较简单的。下面我们来写一个用StackLayout的例子,代码如下:

/*
?* 陈刚 ,创建日期 2004-6-10
?*
?* Email: glchengang@yeah.net
?* Blog : glchengang.yeah.net
?*/
package net.yeah.glchengang.layout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class StackLayoutApp {

??? /*
???? * 因为要在按钮b1,b2的事件中调用,所以以下变量必须定义成属性(类变量)
???? * 否则在按钮b1,b2的事件中是访问不到的
???? */
??? private Composite c;
??? private Text t1;
??? private Text t2;
??? private Text t3;
??? private StackLayout stackLayout = new StackLayout();

??? public static void main(String[] args) {
??????? StackLayoutApp window = new StackLayoutApp();
??????? window.open();
??? }
??? public void open() {
??????? Display display = new Display();
??????? Shell shell = new Shell();
??????? shell.setLayout(new FillLayout());
??????? shell.setText("SWT Application");
??????? //--------------核心代码如下:start------------------------
??????? {
??????????? /*
???????????? * 在c面板下有两个Text框,因为c为stackLayout布局,
???????????? * 所以这两个Text框是重叠在一起的
???????????? */
??????????? c = new Composite(shell, SWT.NONE);
??????????? c.setLayout(stackLayout);

??????????? t1 = new Text(c, SWT.BORDER);
??????????? t1.setText("11111111111111");

??????????? t2 = new Text(c, SWT.BORDER);
??????????? t2.setText("222222222222");
??????? }
??????? {
??????????? /*
???????????? * 建立两个按钮来改变上面两个text框的显示
???????????? */
??????????? Composite composite = new Composite(shell, SWT.NONE);
??????????? composite.setLayout(new RowLayout());

??????????? Button b1 = new Button(composite, SWT.NONE);
??????????? b1.setText("显示111111111");
??????????? b1.addSelectionListener(new SelectionListener() { //点击事件 
??????????????? public void widgetSelected(SelectionEvent e) {
??????????????????? /*****************************************************
???????????????????? * --------下面两句是StackLayout的关键----------------
???????????????????? ****************************************************/
??????????????????? stackLayout.topControl = t1; //将t1框显示
??????????????????? c.layout(); //将界面刷新一下,否则显示上会没任何反映的
??????????????? }
??????????????? public void widgetDefaultSelected(SelectionEvent e) {}
??????????? });

??????????? Button b2 = new Button(composite, SWT.NONE);
??????????? b2.setText("显示2222222222");
??????????? //这里也可以用SelectionAdapter这个适配器,它是SelectionListener的空实现
??????????? b2.addSelectionListener(new SelectionAdapter() {
??????????????? public void widgetSelected(SelectionEvent e) {
??????????????????? stackLayout.topControl = t2;
??????????????????? c.layout();
??????????????? }
??????????? });

??????? }

??????? //--------------核心代码如上:end------------------------???????
??????? shell.open();
??????? while (!shell.isDisposed()) {
??????????? if (!display.readAndDispatch())
??????????????? display.sleep();
??????? }
??? }
}

运行效果图如下:

?

点击按钮"显示11111"时,图变化如下:

?

点击按钮"显示22222"时,图变化如下:

?



↑返回目录
前一篇: Eclipse插件开发系列5.SWT中的布局管理器(4)
后一篇: Eclipse插件开发系列:打包文件