WindowBuilder、SWT、jface

GUI(Graphical User Interface,简称 GUI,图形用户界面)是指采用图形方式显示的计算机操作用户界面,与早期计算机使用的命令行界面相比,图形界面对于用户来说在视觉上更易于接受。

Java GUI主要有两个核心库,分别是AWT(java.awt:Abstract Windows ToolKit(抽象窗口工具包))和Swing(javax.swing:AWT的扩展)
AWT需要调用本地系统方法来实现功能,属重量级控件,
Swing是在AWT的基础上,建立的一套图像界面系统,其中提供了更多的组件,而且完全由Java实现,增强了移植性,属轻量级组件。

WindowBuilder

Esclipse包层级结构设置

要想显示为树形结构,则如下操作:选择package presentation ,然后选择hierarchical, 就是树形结构了。


Esclipse 快捷键

生成变量

【Ctrl + 1】

F:生成全局变量。

L:生成局部变量。


代码自动补全


WindowBuilder安装

插件的安装:https://blog.csdn.net/m0_51086313/article/details/111119825?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168030454016800192219416%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168030454016800192219416&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-111119825-null-null.142^v80^insert_down38,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=windowbuilder&spm=1018.2226.3001.4187

重启Esclipse后,可以File-New-Other,可以看到多出来WindowBuilder目录


项目引入SWT相关jar包

选中项目右键,点击 bulid path - configure build path

点击 库-添加外部jar包

swt.jar下载可以去看这篇文章
https://blog.csdn.net/u013642500/article/details/102791008

添加后效果如下,之后点击 应用并关闭

SWT

SWT:standard widget toolkit 标准小窗口工具箱

用SWT编写的UI无论在响应速度,还是在美观程度上都大大超越了SUN公司提供的AWT和Swing。

  1. SWT采用JNI技术,直接调用操作系统提供的图形组件。

  1. JFace库是SWT的增强库,是SWT的扩展。

  1. SWT提供丰富的窗口小部件(widget)用于创建独立的java应用程序

SWT的底层是由C编写的,通过C直接调用系统层的GUI API,执行效率非常高

SWT入门

SWT类库

SWT 类库提供了许多包,这些常用包及功能如下:


SWT入门hello world

如果你也使用springboot项目的话,像我这样在src新建lib目录。下面放esclipse相关的包并添加到项目中

public class SWTApplication {

    protected Shell shell;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            SWTApplication window = new SWTApplication();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");
        
        
        //创建一个Label,显示Hello World
        Label label = new Label(shell, SWT.NONE);
        label.setBounds(10, 10, 94, 17);
        label.setText("Hello World");

    }
}

很可能跟我一样,你会报这个错,莫慌

java.lang.UnsatisfiedLinkError: Cannot load 32-bit SWT libraries on 64-bit JVM

意思是不能在64位JVM上加载32位SWT库,说白了就是我的jre是64位,所以我用SWT32的库是不可以,反之32的jre用64位的SWT也是不可以,

解决办法:SWT库改成对应的jre版本,或者jre改成对应版本的SWT库

我是把jdk版本换成32位的了,就可以看到如下效果了


Display 和 Shell

Display:每一个基于 SWT 的应用程序都会有一个 Display 对象(实例),这个对象用来表示平台与 SWT 的连接,并在程序末尾释放。

Display 类维护着注册的事件监听器,通过 readAndDispatch()方法从操作系统的事件队列中读取事件,并将这些事件传递给相应的事件监听器来完成具体的实现。

display.dispose() 释放资源
1.如果你创建了某个资源,那么就有责任释放它
2.释放父组件资源的同时也释放了其子组件的资源

Shell :每一个窗体都有一个 Shell 类,它用来表示可交互的窗口容器。Shell通常有一个作为父控件的 Display,这也是默认设置。

构造器




//open()设置接收者的显示,将它标记为可见,并且请求窗口管理器激活 Shell。
shell.open()

实现主窗口的顶级 Shell

构造方法传入的是 Display 类的对象,将会创建一个顶级的 shell 窗口

Shell()//创建顶级shell 窗口
Shell(Display display) //创建顶级shell 窗口
Shell(Display display, int style)  //创建顶级shell 窗口
Shell(int style)  //创建顶级shell 窗口

默认情况下,shell的style是 SWT.CLOSE|SWT.MIN|SWT.MAX

  • SWT.CLOSE:带有标题栏和关闭按钮

  • SWT.MIN:带有标题栏和最小化按钮,最大化按钮处于没有激活状态

  • SWT.MAX :带有标题栏和最大化按钮,最小化按钮处于没有激活状态

如下:

试着改一下样式

SWT.NO_TRIM

窗口没有“特殊样式”,该窗口没有边框,没有标题栏。没有“关闭”、“最大化”、“最小化”按钮,不能修改其大小。不能拖拽位置

当还有很多别的其他样式,如:

SWT.ON_TOP样式可以附加到任何样式的窗口上,它唯一的作用就是使窗口永远处于
用户桌面的最上层。

实现对话框的 Shell

如下2种Shell的构造方法种传入的是Shell 类的对象,用于创建一个对话框

Shell(Shell parent) //创建一个对话框
Shell(Shell parent, int style)//创建一个对话框
        shell = new Shell();
        shell.setSize(612, 550);
        shell.setText("顶级窗口");
        
        //对话框
        Shell dialogShell = new Shell(shell, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);// 样式允许Shell组件阻拦对其父亲组件的输入
        dialogShell.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
        dialogShell.setText("对话框窗口");
        dialogShell.setSize(100, 200);
        //请求窗口管理器激活 Shel
        dialogShell.open();

Shell容器支持事件

shell容器支持5种事件,分别为Activate、Close、Deactivate、Deiconify、lconify

在一个shell窗口成为活动窗口后,会触发Activate事件,Activate事件可以在窗口被创
建出来时触发,也可以在用户的输入焦点落到shell窗口时触发;

当shell窗口被关闭时,触发close事件;

当shell窗口从活动状态变为非活动状态时,会触发Deactivate事件;

当shell窗口从最小化状态恢复时,会触发Deiconify事件;

当shell窗口最小化时,会触发Iconify事件

Widget类

widget直接继承Object类,是swt种所有窗口和控件的父类,主要提供了申请资源、释放资源、监听事件等功能。

Widget是窗口小部件, 是组成用户界面的元素, 包含Menu、Item、Control等, Control是最常用的


SWT简单组件

窗口组件在被创建的时候,必须伴随一个他的上层组件

组件通用方法

//设置组件在容器内的坐标和大小,就是组件在窗体的定位(绝对定位),宽度、高度(以像素为单位)
setBounds(int x ,int y ,int width ,int height)
setText(String string) //设置组件的标签文字
setToolTipText(String,string)方法是常用组件方法,用来为一些组件设置提示性信息。
使用该方法的效果是:当鼠标光标停留在该组件上时,将出现提示性的信息,提供最简单的帮助和容错方法
setEnabled(boolen enable)方法可以对组件设置是否可用状态
button.addSelectionListener(new SelectionAdapter() { 
public void widgetSelected(SelectionEvent e) { …} 
} )
当组件被鼠标、键盘选中时,触发此方法的事件处理程序。

Label标签

标签分栏样式举例:

Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
Label label_1 = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL);

Label标签不支持与用户交互。。。

构造器
public Label(Composite parent, int style)

//Composite类是什么鬼
public class Composite extends Scrollable {

}

使用   
//SWT.NONE 为 Label 的默认样式
Label label=new Label(shell,SWT.NONE); 
label.setBounds(20, 15, 30, 15); 
label.setText("Label");//设置label标签上的文字

Label标签作为静态控件,它将字符串或者图像或者分隔符作为本身的显示内容,其常用的 API包括:

getText()获取标签文字
setText(String string)设置标签文字。

getImage()获取标签上的图像
setImage(Image image)设置标签上的图像。

getAlignment()获取文本或图像显示方式(SWT.LEFT、SWT.CENTER、SWT.RIGHT)。
setAlignment(int alignment)设 置 文 本 或 图 像 如 何 在 容 器 上 显 示 。 对 齐 方 式 为  SWT.LEFT、SWT.CENTER、SWT.RIGHT

Button按钮

构造器
public Button(Composite parent, int style)

使用
Button button=new Button(shell,SWT.PUSH|SWT.BORDER)

Button按钮常用的 API

 getText(String string)获取按钮的标签文字
 setText(String string)设置按钮的标签文字。

 getImage()返回按钮上的图像,如果按钮上没有设置图像,则返回 null。
 setImage(Image imaget)在按钮上设置图像。

 getAlignment()获取文本或图像的在按钮中如何显示,必须是 SWT.LEFT、SWT.CENTER、SWT.RIGHT 中的一个值。
 setAlignment(int alignment)设置文本或图像如何在按钮上定位。对齐方式为:SWT.LEFT、SWT.CENTER、SWT.RIGHT。

 getSelecton()当按钮样式风格为 SWT.CHECK、SWT.RADIO、SWT.TOGGLE 时,按钮被选中,返回 true;否则,返回 false。
 setSelecton(Boolean selected)如果按钮样式为 SWT.CHECK、SWT.RADIO、SWT.TOGGLE 样式风格,则可以设置它的选择状态。

 addSelectionListener(SelectionListener listener )将监听器添加到监听器集合中,当窗口部件被选中时通知监听器集合,发送在 SelectionListener 接口中定义的一个信息来通知监听器。

常用的 SWT/JFace 的 API 可以在此网址中查到http://www.help.eclipse.org/help32/index.jsp


Text文本框(输入框)

文本框用来接收键盘的输入信息

构造器
public Text(Composite parent,int style)

使用
Text text=new Text(shell,SWT.NONE); 
text.setBounds(20, 15, 80, 25);

文本框常用的 API

 getText()获得文本内容
 setText(String string)设置接受者(这里指文本框)内的字符串

 getText(int start, int end)获取指定文本范围内的文本内容,文本起始位置为 0。
 getSelectionText()获取选中的文本。

 addSelectionListener(SelectonListener listener) 添加监听器到监听器集合中,当某个
窗口部件被选中时通知监听集合。通过监听器发送在 SelectonListener 接口中定义的一个消息来通知监听器。

 addModifyListener(ModifyListener listener) 添加监听器到监听器集合中,当接收的文
本被修改时通知监听集合,通过监听器发送在 ModifyListener 接口中定义的一个消息来通知监听器。

 addVerifyListener(VerifyListener listener) 添加监听器到监听器集合中,当接收的文
本检验无误时通知监听集合,通过监听器发送在 VerifyListener 接口中定义的一个消息来通知监听器。

 append(String string)添加字符串。
 insert(String string)插入字符串。
 copy()复制选中的文本。
 cut()剪切选中的文本。
 paste()从剪贴板上粘贴文本。
 getLineCount()返回文本中的行数。
 getLineHeight()返回文本中一行的高度(以像素为单位)。
 getOrientation()返回文本行的定位,从左到右或从右到左(SWT.LEFT_TO_RIGHT、
SWT.RIGHT_TO_LEFT)。

 getTextLimit()获取限制文本字符串的位数。
 setEditable(boolean editable)设置状态不可用(此处为文本不可编辑)。
 setTextLimit(int limit)设置文本框最多能输入的字符数。

 setEchoChar(char echo)设置为密码样式字符,如“*”。
 setOrientation(int orientation) 设置文本行的 定位,从左到右或从右到左
(SWT.LEFT_TO_RIGHT、SWT.RIGHT_TO_LEFT)。
 selectAll()选中接收者(这里为文本框)中的文本。
 showSelection()显示所选的文本。
 clearSelection()清除所选文本。

Combo下拉框

下拉框(Combo)作用是用户从下拉项中选择选项

构造器
public Combo (Composite parent, int style)

数组法、循环设值法对下拉选项进行设值

 // 用数组为 Combo 设置下拉框中的下拉项
 final Combo combo1 = new Combo(shell, SWT.SIMPLE);
 combo1.setItems(new String[] { "Eclipse", "SWT", "JFace", "AWT", 
 "Swing", "MyEclipse", "Lomboz" }); 


 final Combo combo2 = new Combo(shell, SWT.DROP_DOWN); 
 combo2.setBounds(150, 25, 90, 20); 
 // 用循环语句设置下拉框中的下拉项
 for (int i = 0; i < 8; i++) { 
 combo2.add("Page " + i); 
 }

下拉框常用的 API

 add(String string)在下拉框的下拉选项最后添加一项。
 add(String string, int index)在下拉框的下拉选项的指定位置添加一项。

 addModifyListener(ModifyListener listener) 添加监听器到监听器集合中,当接收的文
本被修改时通知监听集合,通过监听器发送到 ModifyListener 接口中定义的一个消息来通知监听器。

 addSelectionListener(SelectonListener listener) 添加监听器到监听器集合中,当接收
者的选择改变时通知监听集合。通过监听器发送到 SelectonListener 接口中定义的一个消息来通知监听器。

 clearSelection()将下拉框文本域中的选择设置为空。
 copy()复制选中的文本。
 cut()剪切选中的文本。
 paste()从剪贴板上粘贴文本。
 deselect(int index)将下拉框中所指定的选项清除,index 为下拉框的下拉项索引。
 deselectAll()清除在下拉项中所选的选项(当前选项)。
 getItem(int index)获取接收者(这里是 Combo)下拉项中相对于零给定的索引选项。
 getItemCount()获取下拉框的下拉选项的数量。
 getItems()获取下拉框的下拉选项的字符串数组。
 select(int index)将下拉框的第 index+1 项设置为当前项。
 setItem(int index, String string)在下拉框的下拉项的指定位置设置选项。
 setText(String string)设置当前选项。
 setItems(String[] items)用数组为下拉框设置下拉项。
 remove(int index)将下拉框中清除相对于零的给定索引对应的选项。
 removeAll()将下拉框中的所有下拉选项清除。

List列表

SWT.NONE 为默认样式,只能选中一个

构造器
public List (Composite parent, int style)

列表框常用的 API

 add(String string)在接收者(这里为 List)列表选项最后添加一项。
 add(String string, int index)在接收者(这里指 Combo)列表选项的指定位置添加一项。
 addSelectionListener(SelectonListener listener) 添加监听器到监听器集合中,当接收
者的选择改变时通知监听集合。通过监听器发送在 SelectonListener 接口中定义的一
个消息来通知监听器。
 deselect(int index)将接收者(这里是 List)中所指定的选项清除,index 为列表框的
列表项索引。
 deselectAll()清除接收者(这里是 List)列表项中所选的选项(当前选项)。
 deselect(int start, int end)将接收者(这里是 List)中所指定的选项范围清除,start 为
指定的起始列表项索引;end 为指定的终止列表项。
 deselectAll()清除接收者。
 getItem(int index)获取接收者(这里是 List)列表项中相对于零给定的索引选项。
 getItemCount()获取接收者(这里是 List)列表选项的数量。
 getItems()获取接收者(这里是 List)列表选项的字符串数组。
 getSelection()获取接收者(这里是 List)当前所选中的列表项。
 getSelectionCount()获取接收者(这里是 List)当前所选中的列表项数量。
 remove(int index)将接收者(这里是 List)中清除相对于零的给定索引对应的选项。
 removeAll()将接收者(这里是 List)中的所有列表选项清除。
 remove(int start, int end)将指定范围内的列表项清除。
 setSelection(int index)设置接收者(这里是 List)相对于零的给定索引对应的选项。
 setSelection(String[] items)用数组设置列表框中的列表项。
 setSelection(int start, int end)在指定的列表项范围内设置列表项。

Group分组框

分组框是个容器,在分组框上能够容纳其他组件(复合控件),如按钮、标签、文本等。

分组框 Group 类是 Composite 的子类,它常用于具有同一类界面的组件。使用 Group 类

        Group group = new Group(shell, SWT.NONE);
        group.setText("风云合璧");
        group.setBounds(10, 50, 213, 122);
        
        
        text = new Text(group, SWT.BORDER | SWT.CENTER);
        text.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
        text.setBounds(10, 89, 193, 23);
        
        Button btnNieF = new Button(group, SWT.NONE);
        btnNieF.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText("风神腿");
            }
        });
        btnNieF.setBounds(10, 54, 80, 27);
        btnNieF.setText("聂风");
        
        Button button = new Button(group, SWT.NONE);
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                text.setText("排云掌");
            }
        });
        button.setBounds(123, 54, 80, 27);
        button.setText("步惊云");
        
        Label label = new Label(group, SWT.NONE);
        label.setAlignment(SWT.CENTER);
        label.setBounds(44, 21, 115, 17);
        label.setText("雄霸天下");

使同一类的窗口组件布局更加合理、清晰。

构造器
public Group(Composite parent,int style)

注意:分组框不接受任何事件

分组框常用的 API

 getClientArea() 返回 Group 的边界区域。
 getText() 获得分组框的标题。
 setText(String string) 设置分组框的标题。

Composite面板

Composite 支持常用样式。SWT.NONE 默认样式,即组件在窗口不可见。

  • SWT.H_SCROLL 带有水平滚动条。

  • SWT.V_SCROLL 带有垂直滚动条。

  • SWT.BORDER 带有可见边框。

//shell代表的是父复合控件
Composite composite = new Composite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

Composite 类和 Group 类一样都是容器类,在面板上同样也可以容纳窗口组件。

构造器
public Composite(Composite parent,int style)

面板常用的 API

 getLayout() 得到容器的布局方式。
 layout() 将容器上的组件重新布局。
 getParent() 获得容纳 Composite 的容器。
 layout(boolean changed) 当参数为 true 时作用和 layout()相同。
 getShell() 获得容纳 Composite 的 Shell。
 setLayout(Layout layout) 设置 Composite 的布局方式。
Layout layout = composite.getLayout();

ScrolledComposite滚动面板

构造器
public ScrolledComposite(Composite parent,int style)

在程序中使 ScrolledComposite 生效是通过 setContent()方法实现的,而不是通常采用的绝对定位方法 setBounds()方法。

滚动面板常用的 API

getContent() 获得 ScrolledComposite 上的内容。如果在 ScrolledComposite 上添加了
Button,则返回 Button。

getOrigin() 返回 ScrolledComposite 左上角当前显现内容的坐标点。
setContent(Control content) 设置将要被滚动的内容。
setLayout(Layout layout) 设置布局管理方式。

setExpandHorizontal(boolean expand) 当 expand 为 true 时,使 ScrolledComposite 上的内容
在水平方向改变大小,如果 ScrolledComposite 上的内容水平宽度大于 ScrolledComposite
宽度,则使之与 ScrolledComposite 宽度相等,如果小于则不改变 ScrolledComposite 上的内容水平方向宽度。

setExpandVertical(boolean expand) 当 expand 为 true 时,使 ScrolledComposite 上的内容在垂直方向改 变 大 小 , 如 果 ScrolledComposite 上 的 内 容 垂 直 高 度 大 于ScrolledComposite 高度,则使之与 ScrolledComposite 高度相等。如果小于则不改变ScrolledComposite 上的内容垂直方向高度

SWT复杂组件

菜单栏 Menu

Menu 定义了以下样式

  • SWT.BAR 用于主菜单栏。

  • SWT.DROP_DOWN 用于菜单下拉。

  • SWT.POP_UP 用于右键菜单。

MenuItem 定义了以下样式

SWT.CHECK 复选类型,选中后前面会带有一个小勾。
SWT.PUSH 普通类型,选中后直接产生动作的发生。
SWT.RADIO 单选类型,选中后前面会带有一个
SWT.SEPARATOR 分割类型,在下拉菜单上产生分割符。
SWT.CASCADE 级联菜单,具有 级联 子菜单的特殊菜单项

Menu中还可以有子Menu。菜单可以包含 menuItem(菜单项),而 menuItem 也可以包含菜单(即子菜单)

        //SWT.BAR 用于主菜单栏
        Menu menu = new Menu(shell, SWT.BAR);
        shell.setMenuBar(menu);
        
        MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
        mntmFile.setSelection(true);
        mntmFile.setText("文件");
        
        Menu menu_1 = new Menu(mntmFile);
        mntmFile.setMenu(menu_1);
        
        MenuItem mntmNewFile = new MenuItem(menu_1, SWT.CASCADE);
        mntmNewFile.setText("新建");
        
        Menu menu_2 = new Menu(mntmNewFile);
        mntmNewFile.setMenu(menu_2);
        
        MenuItem menuItem_2 = new MenuItem(menu_2, SWT.NONE);
        menuItem_2.setText("项目");
        MenuItem menuItem_3 = new MenuItem(menu_2, SWT.NONE);
        menuItem_3.setText("仓库");
        MenuItem menuItem_4 = new MenuItem(menu_2, SWT.NONE);
        menuItem_4.setText("文件");
        
        MenuItem mntmNewItem = new MenuItem(menu_1, SWT.CHECK);
        mntmNewItem.setText("打开");
        
        //在下拉菜单上产生分割符
        MenuItem menuItem_1 = new MenuItem(menu_1, SWT.SEPARATOR);
        
        MenuItem mntmNewItem_1 = new MenuItem(menu_1, SWT.RADIO);
        mntmNewItem_1.setText("启动窗口");
        MenuItem mntmNewItem_2 = new MenuItem(menu_1, SWT.NONE);
        mntmNewItem_2.setText("全部保存");
        
        MenuItem mntmEdit = new MenuItem(menu, SWT.NONE);
        mntmEdit.setText("编辑");
        
        MenuItem mntmSource = new MenuItem(menu, SWT.NONE);
        mntmSource.setText("视图");
        
        MenuItem mntmRefactor = new MenuItem(menu, SWT.NONE);
        mntmRefactor.setText("项目");
        
        MenuItem mntmNavigate = new MenuItem(menu, SWT.NONE);
        mntmNavigate.setText("生成");
        
        MenuItem mntmSearch = new MenuItem(menu, SWT.NONE);
        mntmSearch.setText("调试");
        
        MenuItem mntmProject = new MenuItem(menu, SWT.NONE);
        mntmProject.setText("测试");
        
        MenuItem mntmNewItem_3 = new MenuItem(menu, SWT.NONE);
        mntmNewItem_3.setSelection(true);
        mntmNewItem_3.setText("分析");

工具栏ToolBar

ToolBar 用 ToolItem 来设定工具栏上的控件,ToolItem 可以用文本或图片作按钮。

一般,ToolItem 都用 setToolTipText(String string)方法设置提示性的文字

ToolBar 定义了以下样式

SWT.HORIZONTAL 工具栏上按钮为水平方向。
SWT.VERTICAL 工具栏上按钮为垂直方向。
SWT.FLAT 工具栏上按钮为平铺方向。
SWT.RIGHT 工具栏上按钮右对齐方式。
SWT.WRAP 工具栏上按钮上的文字换行。
SWT.SHADOW_OUT 工具栏上按钮显示阴影。

ToolItem 定义了以下样式

SWT.PUSH 工具栏上按钮为普通按钮,可直接引发动作发生。
SWT.CHECK 工具栏上按钮为复选按钮,单击后下陷,再次单击后才弹起。
SWT.DROP_DOWN 工具栏上按钮旁边带下拉菜单。
SWT.RADIO 工具栏上按钮为单选按钮,单击后会立刻弹起来。
SWT.SEPARATOR 工具栏上按钮间的分隔符,没有实质的功能。

选项卡TabFolder

    protected void createContents() {
        shell = new Shell();
        shell.setSize(612, 550);
        shell.setText("SWT Application");
        
        Composite composite = new Composite(shell, SWT.BORDER);
        composite.setBackground(SWTResourceManager.getColor(255, 250, 240));
        composite.setBounds(10, 10, 581, 347);
        
        TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
        tabFolder.setBounds(10, 20, 383, 236);
        
        
        TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);
        tabItem_1.setText("首页");
        
        Composite composite_1 = new Composite(tabFolder, SWT.NONE);
        tabItem_1.setControl(composite_1);
        
        Composite composite_2 = new Composite(composite_1, SWT.NONE);
        composite_2.setBounds(10, 10, 251, 115);
        
        Label label = new Label(composite_2, SWT.NONE);
        label.setBounds(10, 10, 61, 17);
        label.setText("动态");
        
        Label label_1 = new Label(composite_2, SWT.NONE);
        label_1.setBounds(96, 10, 61, 17);
        label_1.setText("推荐");
        
        TabItem tabItem_2 = new TabItem(tabFolder, SWT.NONE);
        tabItem_2.setText("书影音");
        
        TabItem tabItem_3 = new TabItem(tabFolder, SWT.NONE);
        tabItem_3.setText("小组");
        
        TabItem tabItem_4 = new TabItem(tabFolder, SWT.NONE);
        tabItem_4.setText("市集");
        
        TabItem tabItem_5 = new TabItem(tabFolder, SWT.NONE);
        tabItem_5.setText("我");
        
    }
构造器
public TabFolder(Composite parent, int style)

public TabItem(TabFolder parent, int style)

TabFolder 常用 API

addSelectionListener (SelectionListener listener) 将监听器添加到监听集合,当选项卡
选项被选中时,通过给监听器发送在 SelectionListener 接口中定义的一个消息来通知监听器。
 
getItemCount()获得选项卡中包含的选项数。

getItems()返回选项卡中项的 TableItem 数组。

getSelection()获得接收者中当前选中的 TabItem 数组。

getSelectionIndex()获得接收者中当前选中选项相对于 0 的索引,若没有选中项则返回?1。

indexOf(TabItem item) 按参数查询选项卡选项,从头到尾直到查到为止,并返回选项卡选项所在的位置索引(索引从 0 开始)。

setSelection(int index)将给定索引对应的项设置为当前选项。

TabItem 常用 API

getControl()当用户选择选项卡选项时,返回填充在选项卡上客户区域的控件。
getText()获得选项卡选项的标签文字。
setToolTipText(String string)设置提示性的信息。
setImage(Image image)在选项卡上设置图像。
setText(String text)设置选项卡选项的标签文字。
setControl(Control control)当用户选择选项卡选项时,设置用于填充在选项卡客户区域的控件。

树Tree

树的样式主要表现为折叠的层次结构

Tree 用于表示一棵树,TreeItem 类用于表示一棵树的节点

    protected void createContents() {
        shell = new Shell();
        shell.setSize(612, 550);
        shell.setText("SWT Application");
        
        Composite composite = new Composite(shell, SWT.BORDER);
        composite.setBackground(SWTResourceManager.getColor(255, 192, 203));
        composite.setBounds(10, 10, 581, 347);
        
        Tree tree = new Tree(composite, SWT.BORDER);
        tree.setBounds(21, 52, 378, 225);
        
        TreeItem treeItem_food = new TreeItem(tree, SWT.NONE);
        treeItem_food.setText("食物");
        
        TreeItem treeItem_fruit = new TreeItem(treeItem_food, SWT.NONE);
        treeItem_fruit.setBackground(SWTResourceManager.getColor(0, 255, 0));
        treeItem_fruit.setText("水果");
        
        TreeItem treeItem_apple = new TreeItem(treeItem_fruit, SWT.NONE);
        treeItem_apple.setText("苹果");
        treeItem_apple.setExpanded(true);
        
        TreeItem treeItem_banana = new TreeItem(treeItem_fruit, SWT.NONE);
        treeItem_banana.setText("香蕉");
        treeItem_fruit.setExpanded(true);
        
        TreeItem treeItem_vegetable = new TreeItem(treeItem_food, SWT.NONE);
        treeItem_vegetable.setBackground(SWTResourceManager.getColor(0, 255, 0));
        treeItem_vegetable.setText("蔬菜");
        
        TreeItem ttreeItem_tomato = new TreeItem(treeItem_vegetable, SWT.NONE);
        ttreeItem_tomato.setText("西红柿");
        
        TreeItem treeItem_cucumber = new TreeItem(treeItem_vegetable, SWT.NONE);
        treeItem_cucumber.setText("黄瓜");
        
        TreeItem treeItem_potato = new TreeItem(treeItem_vegetable, SWT.NONE);
        treeItem_potato.setText("土豆");
        treeItem_vegetable.setExpanded(true);
        treeItem_food.setExpanded(true);
    }

构造器
public Tree(Composite parent, int style)

public TreeItem(Tree parent, int style) //用来构造一个树的根结点
public TreeItemI(TreeItem parent,int style) //用来在根结点上添加子结点

Tree 支持的样式

  • SINGLE 表示用户只能选择一个树的结点。

  • MULTI 表示用户只能选择多个树的结点。

  • CHECK 表示树的每个结点都会带有一个复选框。

1.Tree 常用 API

addSelectionListener (SelectionListener listener)将监听器添加到监听器集合中,当接
收者选择改变时通知监听器集合,通过监听器发送在 SelectionListener 接口中定义的一个消息通知监听器。

addTreeListener (TreeListener listener) 将监听器添加到监听器集合中,当接收者一项被展开或者收缩时通知监听器集合,通过监听器发送在 TreeListener 接口中定义的
一个消息通知监听器。

clearAll(boolean all)清除接收者的所有项。

clear(int index, boolean all)从接收者中清除相对于 0 的给定索引对应的项。

deselectAll()取消接收者中的所有选中项。

showItem(TreeItem item)在 tree 中显示 TreeItem 项。

showSelection()在 tree 中显示选中的 TreeItem 项。

getItemCount()获得 tree 的所有项的数量。

selectAll()选中接收者的所有项。

setTopItem(TreeItem item)设置接收者当前位于顶层的项。

TreeItem 常用 API

 clearAll(boolean all)清除接收者的所有项。

 clear(int index, boolean all)从接收者中清除相对于 0 给定索引对应的项。

 getBackground()获得接收者的背景颜色。

 getForeground()获得接收者的前景颜色。

 getExpanded()如果接收者处于展开状态,则返回 true;否则,返回 false。

 getItemCount()获得接收者包含的直接子项的数量。

 getGrayed()如果接收者处于灰度状态,则返回 true;否则,返回 false。

 getItem(int index)从接收者中获得相对于 0 给定索引对应的项。

 getParentItem()获得接收者的父项,这里一定是 Tree。

 setText(String text)设置接收者的标签文字。

 setGrayed (boolean grayed)设置接收者的灰度状态。

 setForeground (Color color)设置接收者的背景颜色。

 setExpanded (boolean expanded)设置接收者的展开状态。

 setImage(Image image)设置接收者的图像。

表格树TableTree

TableTree和TableTreeItem


SWT布局管理器

布局是将 UI(User Interface)组件合理地摆放在窗体中的过程,使用布局管理器可对窗体上的组件进行有效的管理。一方面使窗口界面更加友好美观;另一方面使用布局管理器提供的程序跨平台性,在不同系统平台下都能保持界面的稳定性

充满布局FillLayout

将所有窗口组件以相同尺寸放置到一行或者一列中,不能换行

Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new FillLayout(SWT.HORIZONTAL));

默认情况下设置布局为 FillLayout 是横向排列的(水平充满),在创建布局时构造函数传入布局样式即可显式声明布局排列方式

public FillLayout() 
public FillLayout(int type)//SWT.HORIZONTAL、SWT.VERTICAL

那么就改一下布局为垂直充满

 composite.setLayout(new FillLayout(SWT.VERTICAL));


行布局RowLayout

public RowLayout(); 
public RowLayout(int Style);//SWT.HORIZONTAL为默认  SWT.VERTICAL

RowLayout 布局支持自动换行

组件一次排放在窗体上,当 窗体宽度不足时,窗体上的组件自动折行
composite.setLayout(new RowLayout(SWT.HORIZONTAL));

默认创建了一个水平的row layout,并折行显示

当我把composite宽度拉伸时,组件也就跟着变化了。。

RowLayout把所有的控件放在一行或一列中。但是,它不会把所有的控件都设为相同的大小(相比于FillLayout)。如果空间不够,它会把控件放到另一行或另一列中

RowLayout使用RowData的实例来决定控件的初始宽度和高度

把一个RowData对象关联到一个控件;layout从控件中获取RowData以决定控件的大小和排布

        Button button = new Button(composite, SWT.NONE);
        //把一个RowData对象关联到一个控件
        button.setLayoutData(new RowData(64, SWT.DEFAULT));
        button.setText("新房");

        Button button_1 = new Button(composite, SWT.NONE);
        button_1.setLayoutData(new RowData(84, SWT.DEFAULT));
        button_1.setText("二手房");
        
        Button button_2 = new Button(composite, SWT.NONE);
        button_2.setLayoutData(new RowData(78, SWT.DEFAULT));
        button_2.setText("租房");

网格布局GridLayout

  • 网格布局是通过其构造方法构建窗口的基本布局

  • 通过其属性来对组件进行简单的定位设置

构造器
public GridLayout() //将容器设置成为一列,所有组件在容器中从上至下均排成一列。

public GridLayout(int numColumns, boolean makeColumnsEqualWidth)
//numColumns 设置窗口列数
//makeColumnsEqualWidth 设置为true组件等宽排列,一般为false

GridLayout 的属性

GridLayout有很多重要属性,主要包括numColunms、makeColumnsEqualWidth、marginWidth、marginHeight、verticalSpacing、horizontalSpacing

numColumns 属性用来设置容器的列数目。
make ColumnsEqualWidth=true 使组件各列等距离分开。

horizontalSpacing :控制各行组件之间横向的间距(以像素为单位)
verticalSpacing :控制各列组件之间纵向的距离

marginWidth :控制最左边组件和最右边组件与边框的距离
marginHeight :控制顶部组件和底部组件与边框的距离

marginLeft :控制左边的一列组件与左边框的距离
marginRight :控制右边的一列组件与右边框的距离
marginTop :控制顶部组件与上边框的距离
marginBottom :设置底部组件与底边框的距离

GridData 控制布局

注意:GridData的对象只能应用在一个组件上

//底色是黄色的Group框框
GridData gd_group = new GridData(SWT.CENTER, SWT.CENTER, false, false, 6, 1);

可以看到这里构造方法有六个参数分别对应着:

  1. int horizontalAlignment 水平对齐方式 (BEGINNING, CENTER, END, FILL)

  1. int verticalAlignment 垂直对齐方式 (TOP, CENTER, BOTTOM, FILL)

  1. boolean grabExcessHorizontalSpace 是否占用水平方向剩余空间

  1. boolean grabExcessVerticalSpace 是否占用竖直方向剩余空间。

  1. int horizontalSpan 水平空间(代表占据整个网格的多少个列)

  1. int verticalSpan 垂直空间(代表占据整个网格的多少行)

    protected void createContents() {
        shell = new Shell();
        shell.setSize(600, 300);
        shell.setText("SWT Application");
        shell.setLayout(new FormLayout());
        
        Composite composite = new Composite(shell, SWT.BORDER);
        composite.setLayoutData(new FormData());
        //设置窗口列数6列
        composite.setLayout(new GridLayout(6, false));
        
        Button button_1 = new Button(composite, SWT.CENTER);
        //最后俩个参数是占据网格的行数和列数
        GridData gd_button_1 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_button_1.widthHint = 75;
        button_1.setLayoutData(gd_button_1);
        button_1.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
            }
        });
        button_1.setText("视频");
        
        Button button_2 = new Button(composite, SWT.CENTER);
        GridData gd_button_2 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_button_2.widthHint = 75;
        button_2.setLayoutData(gd_button_2);
        button_2.setText("综艺");
        
        Button button_3 = new Button(composite, SWT.CENTER);
        button_3.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
            }
        });
        GridData gd_button_3 = new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1);
        //设置控件的宽度和高度
        gd_button_3.widthHint = 72;
        gd_button_3.heightHint = 28;
        button_3.setLayoutData(gd_button_3);
        button_3.setText("体育");
        
        Button button_4 = new Button(composite, SWT.CENTER);
        GridData gd_button_4 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_button_4.widthHint = 81;
        button_4.setLayoutData(gd_button_4);
        button_4.setText("游戏");
        
        Button button_5 = new Button(composite, SWT.NONE);
        GridData gd_button_5 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_button_5.widthHint = 77;
        button_5.setLayoutData(gd_button_5);
        button_5.setText("剧集");
        
        Button button = new Button(composite, SWT.NONE);
        GridData gd_button = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
        gd_button.widthHint = 78;
        button.setLayoutData(gd_button);
        button.setText("更多");
        
        Group group = new Group(composite, SWT.NONE);
        group.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
        GridData gd_group = new GridData(SWT.CENTER, SWT.CENTER, false, false, 6, 1);
        gd_group.heightHint = 145;
        gd_group.widthHint = 482;
        group.setLayoutData(gd_group);
        group.setText("视频热榜");
        
        List list = new List(group, SWT.BORDER | SWT.V_SCROLL);
        list.setItems(new String[] {"1 张国荣  1299万热度 93.8万次观看", "2 张继科  1073万热度 179万次观看", "3 不是所有人都适合午睡  927万热度  44.5万次观看"});
        list.setBounds(10, 25, 468, 61);
    }

GridData 的构造方法

public GridData() //为默认样式

public GridData(int style) //style 为 GridData 支持的样式

public GridData(int width,int height) //指定组件的宽度和高度,以像素为单位

public GridData(int horizontalAlignment, 
 int verticalAlignment, 
 boolean grabExcessHorizontalSpace, 
 boolean grabExcessVerticalSpace) 

public GridData(int horizontalAlignment, 
 int verticalAlignment, 
 boolean grabExcessHorizontalSpace, 
 boolean grabExcessVerticalSpace, 
 int horizontalSpan, 
 int verticalSpan)

GridData的样式

使用举例:

    public static void main(String[] args) {
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
//--------------------------------
        shell.setText("GridData 实例");
        //将容器分为 2 列
        shell.setLayout(new GridLayout(2, false));
        final Button button1 = new Button(shell, SWT.NONE);
        button1.setText("Button1");
        final Button button2 = new Button(shell, SWT.NONE);
        button2.setText("Button2");
        final Button button3 = new Button(shell, SWT.NONE);
        button3.setText("Button3");
        final Button button4 = new Button(shell, SWT.NONE);
        button4.setText("B4");
        //sytle为 GridData.Fill_BOTH,双向充满
        GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
        //一个GridData只能设置一个button4
        button4.setLayoutData(gridData);

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

如下图:是双向对齐的样式。看的出来没有水平或者垂直充满窗体。也没有说因为B4字少,就把B4按钮缩小成原样。

GridData 的属性

horizontalSpan 用来设置组件占用网格的列数,但组件本身大小不变

    public static void main(String[] args) {
        shell.setLayout(new GridLayout(3, false));
        new Button(shell, SWT.NONE).setText("Button1");
        new Button(shell, SWT.NONE).setText("Button2");
        new Button(shell, SWT.NONE).setText("Button3");
        final Button button = new Button(shell, SWT.NONE);
        button.setText("Button4");
        GridData griddata = new GridData();
        // 水平抢占 3 列
        griddata.horizontalSpan = 3;
        button.setLayoutData(griddata);
        new Button(shell, SWT.NONE).setText("Button5");
    }

verticalSpan 属性用来设置组件占有的行数目

horizontalIndent 属性设置组件向右移动的距离,以像素为单位

verticalIndent 属性设置组件向下移动的距离,以像素为单位

widthHint 属性用来设置组件的宽度,以像素为单位

heightHint 属性用来设置组件的高度,以像素为单位

horizontalAlignment 和 verticalAlignment 属性用来设置组件的对齐方式

griddata.horizontalAlignment=GridData.BEGINNING; 
griddata.horizontalAlignment=GridData.CENTER; 
griddata.horizontalAlignment=GridData.END; 
griddata.horizontalAlignment=GridData.FILL

表格布局FormLayout

构造器
public FormLayout()

marginWidth、marginHeight 为 FomLayout 的两个属性。

marginWidth 属性用来设置组件距离容器 左边框的距离(以像素为单位)。
marginHeight 属性用来设置组件距离容器 上边框的距离(以像素为单位)。

FormAttachment、FormData

使用 FormLayout 与 FormData 配合可以对组件大小进行设置

FormData用FomAttachment类来控制窗口小部件的大小和位置:一个FormData最多用4个FormAttachment,它们分别对应这个小部件的4个面:顶部,底部,左边和右边

FormAttachment计算位置和大小的方法:
y=ax+b
 
在这个等式中,在数学上y代表的是纵坐标,x是横坐标值.a是斜率,b是偏移量.

按照FormAttachment的规则,y是 高,x是宽度,a是一个相对其它部件的百分率,b是偏移量FormAttachment实例中的每个数据成员分别代表这些值.

Constructor

Description

FormAttachment(Control control)

以另外的某个部件为参照物.

FormAttachment(Control control, int offset)

以另外的某个部件为参照物,加上偏移量.

FormAttachment(Control control, int offset, int alignment)

以另外的某个部件为参照物,加上偏移量和alignment

FormAttachment(int numerator)

设定分子.分母为100,没有偏移量

FormAttachment(int numerator, int offset)

指定分子和偏移量,分母默认100

FormAttachment(int numerator, int denominator, int offset)

特定的分子,分母,偏移量

        shell.setLayout(new FormLayout());        
        Composite composite = new Composite(shell, SWT.NONE);
        FormData fd_composite = new FormData();
        //一个FormData最多用4个FormAttachment
        fd_composite.bottom = new FormAttachment(0, 204);
        fd_composite.right = new FormAttachment(0, 384);
        fd_composite.top = new FormAttachment(0, 27);
        fd_composite.left = new FormAttachment(0, 38);
        composite.setLayoutData(fd_composite);
        composite.setLayout(new FormLayout());
        
        text_1 = new Text(composite, SWT.BORDER);
        text_1.setText("文本输入框");
        FormData fd_text_1 = new FormData();
        //一个FormData最多用4个FormAttachment
        fd_text_1.top = new FormAttachment(100, -168);
        fd_text_1.bottom = new FormAttachment(100, -145);
        fd_text_1.right = new FormAttachment(100, -252);
        fd_text_1.left = new FormAttachment(0);
        text_1.setLayoutData(fd_text_1);
        
        Button button_1 = new Button(composite, SWT.BORDER | SWT.CHECK);
        button_1.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
            }
        });
        FormData fd_button_1 = new FormData();
        //一个FormData最多用4个FormAttachment
        fd_button_1.top = new FormAttachment(text_1, 31);
        fd_button_1.left = new FormAttachment(0);
        fd_button_1.right = new FormAttachment(0, 94);
        button_1.setLayoutData(fd_button_1);
        button_1.setText("复选按钮");
        
        List list = new List(composite, SWT.BORDER | SWT.V_SCROLL);
        list.setItems(new String[] {"我的", "热搜", "文娱", "要闻", "同城", "更多"});
        FormData fd_list = new FormData();
        //一个FormData最多用4个FormAttachment
        fd_list.top = new FormAttachment(text_1, -1, SWT.TOP);
        fd_list.right = new FormAttachment(100, -21);
        fd_list.bottom = new FormAttachment(0, 84);
        fd_list.left = new FormAttachment(100, -226);
        list.setLayoutData(fd_list);

FormData最多包含4个FormAttachment 实例,每个对应了与它联系的部件的一边.另外,FormData也可以指定宽和高.表四列出了FormData的数据成员:

Attribute

Description

FormAttachment bottom

The FormAttachment corresponding to the bottom side of the control.

这个FormAttachment 用 来指定部件的底部位置

int height

这个部件的高度.单位为象素.

FormAttachment left

这个FormAttachment 用来指定部件的左部位置

FormAttachment right

这个FormAttachment 用来指定部件的右部位置

FormAttachment top

这个FormAttachment 用来指定部件的顶部位置

int width

这个部件的宽度.单位为象素.


SWT事件与监听器

display 管理事件循环

SWT 是通过 display 管理事件循环的。一旦启动 SWT 应用程序,SWT 使用 readAndDispath()方法从系统平台的事件队列中读取事件,并把读取的事件分配给适当的接收者。

对于每种 事件有与之对应的 Listener类。
如果一个事件为 X,那么对应的监听器(Listener)类就是 XListener,它的添加监听方法为 addXListener。如下的 SelectionListener
//SelectionEvent   当某个 GUI 元素被选中时发生
button.addSelectionListener(new SelectionAdapter() { 
     public void widgetSelected(SelectionEvent e) { 
     MessageDialog.openInformation(shell, null, "HelloWorld!!"); 
     } 
});

值得注意的是:SelectionAdapter

适配器是接口的标准实现,定义若干空方法。样编写事件处理程序更方便、更简单。


适配器、监听器、事件

SWT 适配器和与之对应的监听器接口


事件和与之对应的监听器


无类型事件机制

addListener (int eventType, Listener listener)增加一个监听器到一个监听器集合,当触发了指定类型的事件时,监听器通过 handleEvent()来获知事件。

eventType可能如下:

使用举例

如果只是单一的事件而不是选择方式(“case 样式”的多种分支监听模式)代码可以写成如下方式:

//widget 为窗口小部件对象,可以为按钮、下拉框、列表框等。 
widget.addListener(int eventType, new Listener() { 
  public void handleEvent(Event event) { 
  ... 
  } 
 });

无类型事件机制: 允许监听器实现“case 样式”的监听模式,也可以用 if 语句来实现

Listener listener = new Listener() {
    public void handleEvent(Event e) {
        switch (e.type) {
            case SWT.MouseDown:
                MessageDialog.openInformation(shell, null, "MouseDown!!");
                break;
            case SWT.Resize:
                MessageDialog.openInformation(shell, null, "Shell Resize!!");
                break;
        }
    }
};
shell.addListener(SWT.MouseDown, listener);

键盘鼠标事件属性

KeyEvent属性


MouseEvent属性

JFace

JFace 是建立在 SWT 之上的 UI 部件,它是 SWT 的扩展。

JFace窗体

窗体的布局默认为网格式布局(GridLayout)

public class JFaceWindowsClass extends ApplicationWindow {
    JFaceWindowsClass() {
        super(null); // 部署窗体
    }

    public void run() {
        //直到窗口关闭方能从 open()方法返回
        setBlockOnOpen(true);
        //打开窗口
        open();
        //关闭窗口时释放在操作系统中用到的资源
        Display.getCurrent().dispose();
    }

    public Control createContents(Composite parent) {
        // 设置窗体大小
        parent.getShell().setSize(500, 375);
        // 设置窗体标题
        parent.getShell().setText("JFace ApplicationWidows 实例");
        return parent;
    }

    public static void main(String[] args) {
        new JFaceWindowsClass().run();

    }
}

在 JFace 窗口添加 SWT 基本组件:createContents()

ApplicationWindow 将在所有其他窗口控件创建之后,窗口在屏幕上显示之前调用createContents方法。

动作Action

Action 通常被称为动作命令, 可以关联到菜单,工具条,以及按钮。Action 允许共享多个控件触发代码从而消除冗余代码。

Action构造器
protected Action()
protected Action(String text) 
protected Action(String text,ImageDescriptor image) 
protected Action(String text,int style)

Action使用


对话框Dialog

确认信息对话框

信息提示对话框用来向用户显示提示性的信息,用户通过对话框上的按钮来对信息进行确认。

构造
public static boolean openConfirm(Shell parent, String title, String message)
//使用举例
MessageDialog.openConfirm(shell, "对话框","请确认信息");

提示信息对话框

构造
public static void openInformation(Shell parent, String title, String message)
//使用举例
MessageDialog.openInformation(shell, "对话框","提示信息");

错误信息提示对话框

构造
public static void openError(Shell parent, String title, String message)
//使用举例
MessageDialog.openError(shell, "对话框","错误信息");

自定义对话框

自定义对话框是通过自定义一个类通过继承 Dialog 类来定制的。

public class CustomDialog extends Dialog { 
 ......
}

查看器Viewer

TableViewer

TableViewer 类是 Table 类的扩展,并封装了 Table 窗口小部件。

TableViewer把Table作为一个实例变量,从而实现了对Table功能的扩展

public class TableViewer extends AbstractTableViewer {
    private Table table;
}

相比于SWT的表格组件Table类,在实际项目开发中一般还是用JFace的表格组件TableViewer比较多

TableViewer构造
public TableViewer(Composite parent) 
public TableViewer(Composite parent,int style) 
public TableViewer(Table table)

在得到由List装载的包含数据信息的实体类对象后,

接下来就是使用TableViewer来显示这些数据,实现过程一般要经过如下步骤:

第一步:创建一个TableViewer对象,并在构造函数中用式样设置好表格的外观,这与其他SWT组件的用法一样。

第二步:通过表格内含的Table对象设置布局方式,一般都使用TableViewer的专用布局管理器TableLayout。该布局方式将用来管理表格内的其他组件(如TableColumn表格列)。

第三步:用TableColumn类创建表格列

第四步:设置内容器和标签器。内容器和标签器是JFace组件中的重要概念,它们分别是IStructuredContentProvider、ITableLabelProvider两个接口的实现类,它们的作用就是定义好数据应该如何在TableViewer中显示

第五步:用TableViewer的setInput方法将数据输入到表格。

TreeViewer

构造器
TreeViewer(Composite parent) 
TreeViewer(Composite parent, int style) 
TreeViewer(Tree tree)

TreeViewer 常用的 API

 editElement(Object element, int column)开始编辑给定元素。
 getCellEditors()获得这个树查看器的单元编辑器。
 getCellModifier()获得这个树查看器的单元修改器。
 getControl()获得与这个查看器相关的控件。
 getTree()获得树查看器的树控件。
 getExpanded(Item item)返回给定的 SWT 项是否展开或收缩。
 showItem(Item item)显示给定项。
 isExpandable(Object element)返回代表给定元素的树的节点是展开还是收缩。
 setSelection(List items)设置选择给定的列表项。
 setCellEditors(CellEditor[] editors)为树查看器设置单元格编辑器。
 removeAll(Control widget)从给定的控件中移除所有项。
 setCellModifier(ICellModifier modifier)为树查看器设置单元格修改器。
 setLabelProvider(IBaseLabelProvider labelProvider)设置标签提供器。
 setExpanded(Item node, boolean expand)设置给定项的展开状态。

猜你喜欢

转载自blog.csdn.net/m0_56799642/article/details/129020998
今日推荐