前文回顾:

插件学习篇

简单的建立插件工程以及模型文件分析

利用扩展点,开发透视图

SWT编程须知

5 SWT简单控件的使用与布局搭配

  前几篇讲到了简单控件的使用,复杂控件使用原则上与简单控件差不多,不过数据的使用还有一些布局还有些额外的技巧。

  成果展示:

    

  这里介绍下Tab页,列表,以及树的使用。

  Tab页

  这个tab页仍然采用SWT控件的一贯作风,子页都以挂载的方式连接到Tab容器上,但是需要使用一个组个对象才能在里面放置内容,并不支持直接进行布局。

  1.      TabFolder tabFolder = new TabFolder(shell,SWT.BORDER);
  2.  
  3. TabItem tabItem1 = new TabItem(tabFolder,SWT.NONE);
  4. tabItem1.setText("第一页");
  5.  
  6. Composite compsoite1 = new Composite(tabFolder,SWT.NONE);
  7. tabItem1.setControl(compsoite1);

  这样再在Composite容器内放置其他的控件。

  树形结构

  而列表以及树的使用基本上差不多,树稍微复杂一点,有一个父亲孩子的概念,多使用几次就了解其中的关系技巧了。

  1.        tree = new Tree(treeGroup,SWT.SINGLE);
  2. tree.setLayoutData(new GridData(GridData.FILL_BOTH));
  3.  
  4. TreeItem stu1 = new TreeItem(tree,SWT.NONE);
  5. stu1.setText("xingoo");
  6. {
  7. TreeItem info1 = new TreeItem(stu1,SWT.NONE);
  8. info1.setText("age:25");
  9.  
  10. TreeItem info2 = new TreeItem(stu1,SWT.NONE);
  11. info2.setText("tel:12345");
  12. }
  13. TreeItem stu2 = new TreeItem(tree,SWT.NONE);
  14. stu2.setText("halo");
  15. {
  16. TreeItem info3 = new TreeItem(stu2,SWT.NONE);
  17. info3.setText("age:25");
  18.  
  19. TreeItem info4 = new TreeItem(stu2,SWT.NONE);
  20. info4.setText("tel:67890");
  21. }

  表格

  比较常用的一般就是列表,一般导向页,对话框也都是使用Table来制作。

  1.        table = new Table(tableGroup,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
  2. table.setHeaderVisible(true);//设置表头可见
  3. table.setLinesVisible(true);//设置线条可见
  4. table.setLayoutData(new GridData(GridData.FILL_BOTH));
  5.  
  6. TableColumn column1 = new TableColumn(table,SWT.NULL);
  7. column1.setText("Tree Item");
  8. column1.pack();
  9. column1.setWidth();
  10.  
  11. TableColumn column2 = new TableColumn(table,SWT.NULL);
  12. column2.setText("Parent");
  13. column2.pack();
  14. column2.setWidth();
  1.        TableItem item = new TableItem(table,SWT.NONE);
  2. item.setText(new String[]{“”,“”});

  那么下面还是看一个搭配使用的例子

  首先应用的是一个Tab容器,在第一页放置了一个树形控件,和一个列表控件。点击树形控件的节点,会在列表中添加相关的内容。

  源码参考如下:

  1. public void todo(Shell shell) {
  2. TabFolder tabFolder = new TabFolder(shell,SWT.BORDER);
  3.  
  4. TabItem tabItem1 = new TabItem(tabFolder,SWT.NONE);
  5. tabItem1.setText("第一页");
  6.  
  7. Composite compsoite1 = new Composite(tabFolder,SWT.NONE);
  8. tabItem1.setControl(compsoite1);
  9.  
  10. GridLayout layout = new GridLayout();
  11. layout.numColumns = ;
  12. compsoite1.setLayout(layout);
  13. Group treeGroup = new Group(compsoite1,SWT.NONE);
  14. treeGroup.setText("Tree");
  15. GridData griddata = new GridData(GridData.FILL_BOTH);
  16. griddata.heightHint = ;
  17. treeGroup.setLayoutData(griddata);
  18. treeGroup.setLayout(new GridLayout(,false));
  19. {
  20. tree = new Tree(treeGroup,SWT.SINGLE);
  21. tree.setLayoutData(new GridData(GridData.FILL_BOTH));
  22.  
  23. TreeItem stu1 = new TreeItem(tree,SWT.NONE);
  24. stu1.setText("xingoo");
  25. {
  26. TreeItem info1 = new TreeItem(stu1,SWT.NONE);
  27. info1.setText("age:25");
  28.  
  29. TreeItem info2 = new TreeItem(stu1,SWT.NONE);
  30. info2.setText("tel:12345");
  31. }
  32. TreeItem stu2 = new TreeItem(tree,SWT.NONE);
  33. stu2.setText("halo");
  34. {
  35. TreeItem info3 = new TreeItem(stu2,SWT.NONE);
  36. info3.setText("age:25");
  37.  
  38. TreeItem info4 = new TreeItem(stu2,SWT.NONE);
  39. info4.setText("tel:67890");
  40. }
  41.  
  42. tree.addSelectionListener(new SelectionAdapter() {
  43. public void widgetSelected(SelectionEvent evt){
  44. TableItem item = new TableItem(table,SWT.NONE);
  45. item.setText(new String[]{tree.getSelection()[].toString(),tree.getSelection()[].getText()});
  46. }
  47. });
  48. }
  49. Group tableGroup = new Group(compsoite1,SWT.NONE);
  50. tableGroup.setText("Table");
  51. GridData gd = new GridData(GridData.FILL_BOTH);
  52. gd.heightHint = ;
  53. tableGroup.setLayoutData(gd);
  54. tableGroup.setLayout(new GridLayout(,false));
  55. { //创建一个单选的,有边界的,一行全选的表格
  56. table = new Table(tableGroup,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
  57. table.setHeaderVisible(true);//设置表头可见
  58. table.setLinesVisible(true);//设置线条可见
  59. table.setLayoutData(new GridData(GridData.FILL_BOTH));
  60.  
  61. TableColumn column1 = new TableColumn(table,SWT.NULL);
  62. column1.setText("Tree Item");
  63. column1.pack();
  64. column1.setWidth();
  65.  
  66. TableColumn column2 = new TableColumn(table,SWT.NULL);
  67. column2.setText("Parent");
  68. column2.pack();
  69. column2.setWidth();
  70. }
  71.  
  72. TabItem tabItem2 = new TabItem(tabFolder,SWT.NONE);
  73. tabItem2.setText("第二页");
  74. }

  全部源码

  1. package com.xingoo.plugin.swttest.test;
  2.  
  3. import javax.swing.text.StyleConstants.ColorConstants;
  4.  
  5. import org.eclipse.swt.SWT;
  6. import org.eclipse.swt.events.SelectionAdapter;
  7. import org.eclipse.swt.events.SelectionEvent;
  8. import org.eclipse.swt.layout.FillLayout;
  9. import org.eclipse.swt.layout.GridData;
  10. import org.eclipse.swt.layout.GridLayout;
  11. import org.eclipse.swt.widgets.Composite;
  12. import org.eclipse.swt.widgets.Group;
  13. import org.eclipse.swt.widgets.Shell;
  14. import org.eclipse.swt.widgets.TabFolder;
  15. import org.eclipse.swt.widgets.TabItem;
  16. import org.eclipse.swt.widgets.Table;
  17. import org.eclipse.swt.widgets.TableColumn;
  18. import org.eclipse.swt.widgets.TableItem;
  19. import org.eclipse.swt.widgets.Tree;
  20. import org.eclipse.swt.widgets.TreeItem;
  21.  
  22. import com.xingoo.plugin.swttest.Abstract.AbstractExample;
  23.  
  24. public class Test1 extends AbstractExample{
  25. private Table table;
  26. private Tree tree;
  27. public static void main(String[] args) {
  28. new Test1().run();
  29. }
  30.  
  31. public void todo(Shell shell) {
  32. TabFolder tabFolder = new TabFolder(shell,SWT.BORDER);
  33.  
  34. TabItem tabItem1 = new TabItem(tabFolder,SWT.NONE);
  35. tabItem1.setText("第一页");
  36.  
  37. Composite compsoite1 = new Composite(tabFolder,SWT.NONE);
  38. tabItem1.setControl(compsoite1);
  39.  
  40. GridLayout layout = new GridLayout();
  41. layout.numColumns = ;
  42. compsoite1.setLayout(layout);
  43. Group treeGroup = new Group(compsoite1,SWT.NONE);
  44. treeGroup.setText("Tree");
  45. GridData griddata = new GridData(GridData.FILL_BOTH);
  46. griddata.heightHint = ;
  47. treeGroup.setLayoutData(griddata);
  48. treeGroup.setLayout(new GridLayout(,false));
  49. {
  50. tree = new Tree(treeGroup,SWT.SINGLE);
  51. tree.setLayoutData(new GridData(GridData.FILL_BOTH));
  52.  
  53. TreeItem stu1 = new TreeItem(tree,SWT.NONE);
  54. stu1.setText("xingoo");
  55. {
  56. TreeItem info1 = new TreeItem(stu1,SWT.NONE);
  57. info1.setText("age:25");
  58.  
  59. TreeItem info2 = new TreeItem(stu1,SWT.NONE);
  60. info2.setText("tel:12345");
  61. }
  62. TreeItem stu2 = new TreeItem(tree,SWT.NONE);
  63. stu2.setText("halo");
  64. {
  65. TreeItem info3 = new TreeItem(stu2,SWT.NONE);
  66. info3.setText("age:25");
  67.  
  68. TreeItem info4 = new TreeItem(stu2,SWT.NONE);
  69. info4.setText("tel:67890");
  70. }
  71.  
  72. tree.addSelectionListener(new SelectionAdapter() {
  73. public void widgetSelected(SelectionEvent evt){
  74. TableItem item = new TableItem(table,SWT.NONE);
  75. item.setText(new String[]{tree.getSelection()[].toString(),tree.getSelection()[].getText()});
  76. }
  77. });
  78. }
  79. Group tableGroup = new Group(compsoite1,SWT.NONE);
  80. tableGroup.setText("Table");
  81. GridData gd = new GridData(GridData.FILL_BOTH);
  82. gd.heightHint = ;
  83. tableGroup.setLayoutData(gd);
  84. tableGroup.setLayout(new GridLayout(,false));
  85. { //创建一个单选的,有边界的,一行全选的表格
  86. table = new Table(tableGroup,SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
  87. table.setHeaderVisible(true);//设置表头可见
  88. table.setLinesVisible(true);//设置线条可见
  89. table.setLayoutData(new GridData(GridData.FILL_BOTH));
  90.  
  91. TableColumn column1 = new TableColumn(table,SWT.NULL);
  92. column1.setText("Tree Item");
  93. column1.pack();
  94. column1.setWidth();
  95.  
  96. TableColumn column2 = new TableColumn(table,SWT.NULL);
  97. column2.setText("Parent");
  98. column2.pack();
  99. column2.setWidth();
  100. }
  101.  
  102. TabItem tabItem2 = new TabItem(tabFolder,SWT.NONE);
  103. tabItem2.setText("第二页");
  104. }
  105. }

  引用的抽象类

  1. package com.xingoo.plugin.swttest.Abstract;
  2.  
  3. import org.eclipse.swt.layout.FillLayout;
  4. import org.eclipse.swt.widgets.Display;
  5. import org.eclipse.swt.widgets.Shell;
  6.  
  7. public abstract class AbstractExample{
  8. public void run(){
  9. Display display = new Display();
  10. Shell shell = new Shell(display);
  11. shell.setText("shell example");
  12. shell.setBounds(,,,);
  13. shell.setLayout(new FillLayout());
  14. todo(shell);
  15. shell.open();
  16.  
  17. while(!shell.isDisposed()){
  18. if(!display.readAndDispatch())
  19. display.sleep();
  20. }
  21. //dispose the resource
  22. display.beep();
  23. display.dispose();
  24. }
  25. public abstract void todo(Shell shell);//extension something here
  26. }

【插件开发】—— 6 SWT 复杂控件使用以及布局的更多相关文章

  1. iOS 开发 ZFUI framework控件,使布局更简单

    来自:http://www.jianshu.com/p/bcf86b170d9c 前言 为什么会写这个?因为在iOS开发中,界面的布局一直没有Android布局有那么多的方法和优势,我个人开发都是纯代 ...

  2. Qt基本控件及三大布局

    Qt基本控件及三大布局 来源: http://blog.csdn.net/a2604539133/article/details/73920696 Qt基本模块 一.Qt的三大布局 QHBoxLayo ...

  3. c#学习笔记之使用 TableLayoutPanel 控件设置窗体布局

    使用 TableLayoutPanel 控件设置窗体布局 在 Visual Studio IDE 左侧,找到“工具箱”选项卡. 选择“工具箱”选项卡,随即将显示工具箱.(或者,在菜单栏上,依次选择“视 ...

  4. Windows程序控件升级==>>构建布局良好的Windows程序

    01.菜单栏(MenuStrip) 01.看看这就是menuStrip的魅力: 02.除了一些常用的属性(name.text..)外还有: 03.有人会问:上图的快捷键: 方法: 方式一:1.设置菜单 ...

  5. WinForm DataGridView控件、duck布局

    1.DataGridView控件 显示数据表 (1)后台数据绑定: List<xxx> list = new List<xxx>(); dataGridView1.DataSo ...

  6. iOS Masonry控件等比例布局

    一.先解释相关API 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /**  *  distribute with fixed spacing  *  *  ...

  7. struts中的dojo控件sx:submit布局问题

    想在一个四列的表格中插入两个按钮,希望实现下面的布局效果: 其中保存按钮为<sx:submit />控件.按照下面的代码布局: <tr><td align="c ...

  8. DevExpress的图形按钮菜单栏控件WindowsUIButtonPanel的布局、使用和设置按钮的点击事件

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  9. C#用户控件实战01_CSS布局

    很多应用系统的主页布局,一般采用如下案例所示布局较多,如下图的CSS布局框架,上.中.下,接下来我们演示,在C#中实现如下的业务架构布局. 代码范例: 在<body></body&g ...

随机推荐

  1. 转: ORACLE存储过程笔记2----运算符和表达式

    运算符和表达式     关系运算 =等于<>,!=不等于<小于>大于<=小于等于>=大于等于       一般运算   +加-减*乘/除:=赋值号=>关系号. ...

  2. Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

    题目链接:http://codeforces.com/contest/451/problem/B --------------------------------------------------- ...

  3. 海量数据处理面试题学习zz

    来吧骚年,看看海量数据处理方面的面试题吧. 原文:(Link, 其实引自这里 Link, 而这个又是 Link 的总结) 另外还有一个系列,挺好的:http://blog.csdn.net/v_jul ...

  4. libevent HTTP client 的实现

    my_conn_ = evhttp_connection_base_new(ev_base_,ev_dns_,host,port); struct evhttp_request *http_req; ...

  5. Tomcat和Jetty对WebSocket的支持

    公司项目须要,了解了下眼下几种支持WebSocket的框架.曾经用jWebSocket做过一些项目.相对来说.改jWebSocket的源代码略复杂,也不是一天两天能搞定的. 一调研才发现,如今非常多主 ...

  6. FancyCoverFlow

    https://github.com/davidschreiber/FancyCoverFlow

  7. 一张图理清js原型链(通过内置对象的引用关系)

    很多同学估计写了几年js也没有搞清内置对象之间的原型链关系,鄙人抽空手绘了一张简图,以作参考: 简单说明一下,上图中annonymous()函数相当于是所有函数的根(它本身也是函数),他上面提供了一些 ...

  8. linux路由表解析

    1 格式 Destination 这个和Genmask一起构成目标网络.路由是路由到目标网络,知道目标网络就可以到达目标路由器,然后在该网络中找到目标机器. Gateway 网关,数据包的下一跳.比如 ...

  9. Infrastructure for container projects.

    Linux Containers https://linuxcontainers.org/

  10. 不同节点 IP 时间同步 分布式时间同步系统的参考时间获取技术分析

    linux linux下时间同步的两种方法分享_LINUX_操作系统_脚本之家 http://www.jb51.net/LINUXjishu/73979.html 分布式时间同步系统的参考时间获取技术 ...