posts - 23, comments - 0, trackbacks - 0, articles - 3
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

swt学习小结

Posted on 2008-08-18 17:04 beauty9235 阅读(294) 评论(0)  编辑  收藏

作者: beauty9235  链接:http://beauty9235.javaeye.com/blog/229625  发表时间: 2008年08月04日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

swt学习小结
Widget —— 基本的 SWT GUI 组件
Control —— 拥有操作系统的对等物的窗口小部件
Composite —— 包含其他控件的控件
Item —— 其他控件包含的窗口小部件(该控件可能不是复合控件),比如列表和表

标准的SWT布局类
FillLayout:在容器中以相同的大小单行或单列的排列组件
RowLayout:以单行或多行的方式使用几个选项(fill,wrap,spacing,justify,type)定制组件的排列方式
GridLayout:类似于swing的GridLayout的方式以格子的方式排列组件
FormLayout(SWT 2.0的新特性):通过定义组件四个边的“粘贴”位置来排列组件,被引用的相对的组件可以父组件,也可以是同一容器中的其它组件。

/*
 * 参考文章
 * http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html
 * http://www.ibm.com/developerworks/cn/linux/opensource/os-ecgui1/index.html
 * http://www.ibm.com/developerworks/cn/linux/opensource/os-ecgui2/index.html
 * http://www.ibm.com/developerworks/cn/linux/opensource/os-ecgui3/index.html
 * http://www-128.ibm.com/developerworks/cn/opensource/os-swingswt/
 * http://www.blogjava.net/fortune/archive/2006/03/09/34463.html
 */ 
 
 布局实施
1.首先定义窗口和它的空白边
Display.getDefault().dispose();  //移去平台核心启动画面
        display = new Display();
        shell = new Shell(display, SWT.TITLE);
        FormLayout layout = new FormLayout();
        layout.marginHeight = 10;
        layout.marginWidth = 20;
        shell.setLayout(layout);
        shell.setText("用户登录");

2.创建窗口上的元素,其中下面的两个button由一个使用RowLayout的组件来包容。
        name = new Label(shell, SWT.NONE);
        name.setText("用户名");
        nameText = new Text(shell, SWT.SINGLE | SWT.BORDER);
        pass = new Label(shell, SWT.NONE);
        pass.setText("密码");
        passText = new Text(shell, SWT.SINGLE | SWT.BORDER);
        passText.setEchoChar('*');
        passText.setTabs(2);
        bottom = new Composite(shell, SWT.NONE);
        RowLayout rowLayout = new RowLayout();
        rowLayout.justify = true;  //justify定义组件在容器中分散开,效果类似于swing的FlowLayout
        bottom.setLayout(rowLayout);

3.定义name标签的位置。它的顶边离父组件(窗口shell)的空白边距离是父组件clientArea(除空白边以外的空间)高度(height)的15%,偏移的点数(points)为0。
FormData data = new FormData();
        data.top = new FormAttachment(15, 0);
        name.setLayoutData(data);

4.定义name文本输入的位置。它的顶边在name标签的中心位置(这不是正确的表达,但程序是这样解释,事实上它的中心位置与name标签在同一条水平线上),左边距name标签的右边有10个点。
data = new FormData();
        data.top = new FormAttachment(name, 0, SWT.CENTER);
        data.left = new FormAttachment(name, 10, SWT.RIGHT);
        nameText.setLayoutData(data);

5.定义pass标签的位置。它的顶边距name标签的底边有10个点数的偏移。
data = new FormData();
        data.top = new FormAttachment(name, 10, SWT.BOTTOM);
        pass.setLayoutData(data);

6.定义pass文本输入的位置。它的顶边在name标签的中心位置(同上),左边与name文本框的左边对齐。
data = new FormData();
        data.top = new FormAttachment(pass, 0, SWT.CENTER);
        data.left = new FormAttachment(nameText, 0, SWT.LEFT);
        passText.setLayoutData(data);

7.定义bottom组件的位置。它的顶边距pass标签的底边15个点数,左边与pass标签的左边对齐,右边与pass文本输入的右边对齐。
        data = new FormData();
        data.top = new FormAttachment(pass, 15, SWT.BOTTOM);
        data.left = new FormAttachment(pass, 0, SWT.LEFT);
        data.right = new FormAttachment(passText, 0, SWT.RIGHT);
        bottom.setLayoutData(data);

完整的源码
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import cn.com.efly.clientframe.core.Hook;

/**
 * @author efly
 * @version 1.0.0,11/22/02
 */
public final class LoginUI {
    private Display display;
    private Shell shell;
    private Composite bottom;
    private Label name;
    private Label pass;
    private Text nameText;
    private Text passText;
    private Button ok;
    private Button exit;
    private Rectangle clientArea;
    private RootHook rootHook;

    public LoginUI(Hook hook) {
        rootHook = (RootHook) hook;
    }

    /**
     * 显示登陆界面
     */
    public void show() {
        Display.getDefault().dispose();
        display = new Display();
        clientArea = display.getClientArea();
        shell = new Shell(display, SWT.TITLE);
        FormLayout layout = new FormLayout();
        layout.marginHeight = 10;
        layout.marginWidth = 20;
        shell.setLayout(layout);
        shell.setText("用户登录");

        name = new Label(shell, SWT.NONE);
        name.setText("用户名");
        nameText = new Text(shell, SWT.SINGLE | SWT.BORDER);
        pass = new Label(shell, SWT.NONE);
        pass.setText("密码");
        passText = new Text(shell, SWT.SINGLE | SWT.BORDER);
        passText.setEchoChar('*');
        passText.setTabs(2);
        bottom = new Composite(shell, SWT.NONE);
        RowLayout rowLayout = new RowLayout();
        rowLayout.justify = true;
        bottom.setLayout(rowLayout);
        ok = new Button(bottom, SWT.PUSH);
        ok.setText("确定");
        ok.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                ok();
            }
        });
        exit = new Button(bottom, SWT.PUSH);
        exit.setText("取消");
        exit.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                cancel();
            }
        });

        FormData data = new FormData();
        data.top = new FormAttachment(15, 0);
        name.setLayoutData(data);

        data = new FormData();
        data.top = new FormAttachment(name, 0, SWT.CENTER);
        data.left = new FormAttachment(name, 10, SWT.RIGHT);
        nameText.setLayoutData(data);

        data = new FormData();
        data.top = new FormAttachment(name, 10, SWT.BOTTOM);
        pass.setLayoutData(data);

        data = new FormData();
        data.top = new FormAttachment(pass, 0, SWT.CENTER);
        data.left = new FormAttachment(nameText, 0, SWT.LEFT);
        passText.setLayoutData(data);

        data = new FormData();
        data.top = new FormAttachment(pass, 15, SWT.BOTTOM);
        data.left = new FormAttachment(pass, 0, SWT.LEFT);
        data.right = new FormAttachment(passText, 0, SWT.RIGHT);
        bottom.setLayoutData(data);

        shell.pack();
        Rectangle shellBounds = shell.getBounds();
        shell.setLocation(
            (clientArea.width - shellBounds.width) / 2,
            (clientArea.height - shellBounds.height) / 2);

        shell.open();

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

    private void dispose() {
        display.dispose();

    }

    private void cancel() {
        dispose();
    }

    private void ok() {
        verify();
    }

    private void verify() {
        rootHook.runPlatform();
    }

        public static void main(String[]){
                new LoginUI(null).show();
        }
}


本文的讨论也很精彩,浏览讨论>>


JavaEye推荐




只有注册用户登录后才能发表评论。


网站导航: