免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3351 | 回复: 9
打印 上一主题 下一主题

SWT&Jface权威指南---SWT中控件的使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-03-30 20:15 |只看该作者 |倒序浏览
/**
* 作 者 :李天泉
*工作地:北京中关村软件园   
*/
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.graphics.*;

/**
* 此类演示Labels控件的使用
*/
public class LabelExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell();
    shell.setLayout(new GridLayout(1, false));

    // 创建一个label控件
    new Label(shell, SWT.NONE).setText("This is a plain label.";

    // 创建一个垂直的分割线
    new Label(shell, SWT.SEPARATOR);

    // 创建一个带有边框的Label控件
    new Label(shell, SWT.BORDER).setText("This is a label with a border.";

    // 创建一个水平的分割线
    Label separator = new Label(shell, SWT.HORIZONTAL | SWT.SEPARATOR);
    separator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // 创建一个带有图片的label控件
     Image image = new Image(display, "heihei.jpg";
    Label imageLabel = new Label(shell, SWT.NONE);
    imageLabel.setImage(image);
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

LabelExample.jpg (12.93 KB, 下载次数: 17)

LabelExample.jpg

论坛徽章:
0
2 [报告]
发表于 2005-03-30 20:21 |只看该作者

SWT&Jface权威指南---SWT中控件的使用

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
* 此类演示 Buttons控件
*/
public class ButtonExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(3, true));

    // 创建三个按钮控件
    new Button(shell, SWT.PUSH).setText("ush 1";
    new Button(shell, SWT.PUSH).setText("ush 2";
    new Button(shell, SWT.PUSH).setText("ush 3";

    // 常见三个复选框控件
    new Button(shell, SWT.CHECK).setText("Checkbox 1";
    new Button(shell, SWT.CHECK).setText("Checkbox 2";
    new Button(shell, SWT.CHECK).setText("Checkbox 3";

    // 创建三个绳钉控件(有两种状态的控件,同复选框,仅外观不一样)
    new Button(shell, SWT.TOGGLE).setText("Toggle 1";
    new Button(shell, SWT.TOGGLE).setText("Toggle 2";
    new Button(shell, SWT.TOGGLE).setText("Toggle 3";

    // 创建三个单选框控件
    new Button(shell, SWT.RADIO).setText("Radio 1";
    new Button(shell, SWT.RADIO).setText("Radio 2");
    new Button(shell, SWT.RADIO).setText("Radio 3");

    // 创建三个平面按钮
    new Button(shell, SWT.FLAT).setText("Flat 1");
    new Button(shell, SWT.FLAT).setText("Flat 2");
    new Button(shell, SWT.FLAT).setText("Flat 3");

    // 创建三个箭头按钮
    new Button(shell, SWT.ARROW);
    new Button(shell, SWT.ARROW | SWT.LEFT);
    new Button(shell, SWT.ARROW | SWT.DOWN);
    shell.setText("順天科技--勤學博思製作");
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

ButtonExample.jpg (14.4 KB, 下载次数: 17)

ButtonExample.jpg

论坛徽章:
0
3 [报告]
发表于 2005-03-30 20:26 |只看该作者

SWT&Jface权威指南---SWT中控件的使用

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
* 此类演示 text fields控件
*/
public class TextExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));


    // 创建一个单行的text field控件
    new Text(shell, SWT.BORDER);

    // 创建一个文本右对齐的text field控件
    new Text(shell, SWT.RIGHT | SWT.BORDER);

    // 常见一个密码text field控件
    new Text(shell, SWT.PASSWORD | SWT.BORDER);

    // 创建一个只读的text field控件
    new Text(shell, SWT.READ_ONLY | SWT.BORDER).setText("Read Only";

    // 创建一个多行文本的text field控件
    Text t = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    t.setLayoutData(new GridData(GridData.FILL_BOTH));
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

TextExample.jpg (14.59 KB, 下载次数: 21)

TextExample.jpg

论坛徽章:
0
4 [报告]
发表于 2005-03-30 20:31 |只看该作者

SWT&Jface权威指南---SWT中控件的使用

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
* 此类演示 List控件
*/
public class ListExample {
  // 字符串数组用于列表
  private static final String[] ITEMS = { "Alpha", "Bravo", "Charlie", "Delta",
    "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet", "Kilo", "Lima", "Mike",
    "November", "Oscar", "apa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform",
    "Victor", "Whiskey", "X-Ray", "Yankee", "Zulu"
};

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    //创建一个单选的list控件
    List single = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);

    // 添加List元素(单个方式添加)
    for (int i = 0, n = ITEMS.length; i < n; i++) {
      single.add(ITEMS);
    }

    // 默认选择第四个项目
    single.select(4);

    // 创建一个多选 list控件
    List multi = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);

    // 一次添加所有元素
    multi.setItems(ITEMS);

    // 选择10-12区间元素
    multi.select(9, 11);
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

ListExample.jpg (24.02 KB, 下载次数: 22)

ListExample.jpg

论坛徽章:
0
5 [报告]
发表于 2005-03-30 20:36 |只看该作者

SWT&Jface权威指南---SWT中控件的使用

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
* 此类演示Combo控件
*/
public class ComboExample {
  // 字符串数组用于列表项目
  private static final String[] ITEMS = { "Alpha", "Bravo", "Charlie", "Delta",
    "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet", "Kilo", "Lima", "Mike",
    "November", "Oscar", "apa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform",
    "Victor", "Whiskey", "X-Ray", "Yankee", "Zulu"
  };

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, true));

    // 创建下拉Combo控件
    Combo combo = new Combo(shell, SWT.DROP_DOWN);
    combo.setItems(ITEMS);

    // 常见一个只读的 Combo控件
    Combo readOnly = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    readOnly.setItems(ITEMS);

    // 创建一个“单行”的Combo控件
    Combo simple = new Combo(shell, SWT.SIMPLE);
    simple.setItems(ITEMS);
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

ComboExample.jpg (21.23 KB, 下载次数: 17)

ComboExample.jpg

论坛徽章:
0
6 [报告]
发表于 2005-03-30 20:39 |只看该作者

SWT&Jface权威指南---SWT中控件的使用

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
* 此类演示Sliders控件
*/
public class SliderExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    // 创建一个水平的Slider,默认情况下为水平
    new Slider(shell, SWT.HORIZONTAL);

    // 创建一个垂直的Slider控件,并设置相关属性
    Slider slider = new Slider(shell, SWT.VERTICAL);
    slider.setMinimum(0);
    slider.setMaximum(100);
    slider.setIncrement(5);
    slider.setPageIncrement(20);
    slider.setSelection(75);
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

SliderExample.jpg (7.16 KB, 下载次数: 20)

SliderExample.jpg

论坛徽章:
0
7 [报告]
发表于 2005-03-30 20:42 |只看该作者

SWT&Jface权威指南---SWT中控件的使用

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* 此类演示Groups控件
*/
public class GroupExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());

    //创建第一个Group(分组框)控件
    Group group1 = new Group(shell, SWT.SHADOW_IN);
    group1.setText("Who's your favorite?";
    group1.setLayout(new RowLayout(SWT.VERTICAL));
    new Button(group1, SWT.RADIO).setText("John";
    new Button(group1, SWT.RADIO).setText("aul";
    new Button(group1, SWT.RADIO).setText("George";
    new Button(group1, SWT.RADIO).setText("Ringo";

    // 创建第二个Group
    Group group2 = new Group(shell, SWT.NO_RADIO_GROUP);
    group2.setText("Who's your favorite?";
    group2.setLayout(new RowLayout(SWT.VERTICAL));
    new Button(group2, SWT.RADIO).setText("Barry";
    new Button(group2, SWT.RADIO).setText("Robin";
    new Button(group2, SWT.RADIO).setText("Maurice";
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

GroupExample.jpg (10.54 KB, 下载次数: 18)

GroupExample.jpg

论坛徽章:
0
8 [报告]
发表于 2005-03-30 20:45 |只看该作者

SWT&Jface权威指南---SWT中控件的使用

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
* 此类演示ScrollBars控件
*/
public class ScrollBarExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    //创建一个ScrollBar关联一个List
    List list = new List(shell, SWT.V_SCROLL);

    // 添加元素到List
    for (int i = 0; i < 500; i++) {
      list.add("A list item";
    }

    // 选择最后一个选项
    list.select(list.getItemCount() - 1);
    list.showSelection();

    // 取得ScrollBar
    ScrollBar sb = list.getVerticalBar();
    list.add("Selection: " + sb.getSelection());
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}

ScrollBarExample.jpg (23.99 KB, 下载次数: 19)

ScrollBarExample.jpg

论坛徽章:
0
9 [报告]
发表于 2005-03-30 20:51 |只看该作者

SWT&Jface权威指南---SWT中控件的使用

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
* 此类演示 ProgressBar控件
*/
public class ProgressBarExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());

    // 创建一个平滑的ProgressBar控件
    ProgressBar pb1 = new ProgressBar(shell, SWT.HORIZONTAL | SWT.SMOOTH);
    pb1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    pb1.setMinimum(0);
    pb1.setMaximum(30);

    // 创建一个模糊的ProgressBar控件
    ProgressBar pb2 = new ProgressBar(shell, SWT.HORIZONTAL | SWT.INDETERMINATE);
    pb2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    // 启动第一个ProgressBar
    new LongRunningOperation(display, pb1).start();
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}

/**
* 此类模拟一个运行时的操作
*/
class LongRunningOperation extends Thread {
  private Display display;
  private ProgressBar progressBar;

  public LongRunningOperation(Display display, ProgressBar progressBar) {
    this.display = display;
    this.progressBar = progressBar;
  }
  public void run() {
    for (int i = 0; i < 30; i++) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {

      }
      display.asyncExec(new Runnable() {
        public void run() {
          if (progressBar.isDisposed()) return;
          progressBar.setSelection(progressBar.getSelection() + 1);
        }
      });
    }
  }
}

ProgressBarExample.jpg (12.23 KB, 下载次数: 18)

ProgressBarExample.jpg

论坛徽章:
0
10 [报告]
发表于 2005-03-30 21:04 |只看该作者

SWT&Jface权威指南---SWT中控件的使用

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

/**
* 此类演示menus控件的使用
*/
public class Menus {
  private Image star;
  private Image circle;
  private Image square;
  private Image triangle;

  /**
   * 运行应用程序
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Menus";
    createContents(shell);
    shell.setText("順天科技--勤學博思製作";
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    if (circle != null) circle.dispose();
    if (star != null) star.dispose();
    if (square != null) square.dispose();
    if (triangle != null) triangle.dispose();
    display.dispose();
  }
  /**
   * 创建主窗口的内容
   *
   * @param shell 主窗口
   */
  private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());
    createBarMenu(shell);
    createPopUpMenu(shell);
    createNoRadioGroupPopUpMenu(shell);
  }

  /**
   * 创建菜单
   */
  private void createBarMenu(Shell shell) {
    //创建菜单

    Menu menu = new Menu(shell, SWT.BAR);

    // 创建菜单项
    MenuItem fileItem = new MenuItem(menu, SWT.CASCADE);
    fileItem.setText("File";
    MenuItem editItem = new MenuItem(menu, SWT.CASCADE);
    editItem.setText("Edit";
    MenuItem formatItem = new MenuItem(menu, SWT.CASCADE);
    formatItem.setText("Format";
    MenuItem viewItem = new MenuItem(menu, SWT.CASCADE);
    viewItem.setText("View";
    MenuItem helpItem = new MenuItem(menu, SWT.CASCADE);
    helpItem.setText("Help";

    // 创建File菜单的下拉菜单
    Menu fileMenu = new Menu(menu);
    fileItem.setMenu(fileMenu);

    // 创建文件菜单下拉菜单中的项目
    MenuItem newItem = new MenuItem(fileMenu, SWT.NONE);
    newItem.setText("New";
    MenuItem openItem = new MenuItem(fileMenu, SWT.NONE);
    openItem.setText("Open...";
    MenuItem saveItem = new MenuItem(fileMenu, SWT.NONE);
    saveItem.setText("Save";
    MenuItem saveAsItem = new MenuItem(fileMenu, SWT.NONE);
    saveAsItem.setText("Save As...");
    new MenuItem(fileMenu, SWT.SEPARATOR);
    MenuItem pageSetupItem = new MenuItem(fileMenu, SWT.NONE);
    pageSetupItem.setText("age Setup...");
    MenuItem printItem = new MenuItem(fileMenu, SWT.NONE);
    printItem.setText("rint...");
    new MenuItem(fileMenu, SWT.SEPARATOR);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.NONE);
    exitItem.setText("Exit");

    // 设置主窗口的菜单
    shell.setMenuBar(menu);
  }

  /**
   * 创建左边的上下文菜单
   */
  private void createPopUpMenu(Shell shell) {
    // 创建一个面板关联此上下文菜单
    Label label = new Label(shell, SWT.BORDER);
    label.setText("op-up Menu");

    // 创建弹出菜单
    Menu menu = new Menu(label);

    // 创建图片
    star = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
        "05.gif"));
    circle = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
        "05.gif"));
    square = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
        "05.gif"));
    triangle = new Image(shell.getDisplay(), this.getClass().getResourceAsStream(
        "05.gif"));

    // 创建谈出菜单的菜单项
    MenuItem newItem = new MenuItem(menu, SWT.CASCADE);
    newItem.setText("New");
    newItem.setImage(star);
    MenuItem refreshItem = new MenuItem(menu, SWT.NONE);
    refreshItem.setText("Refresh");
    refreshItem.setImage(circle);
    MenuItem deleteItem = new MenuItem(menu, SWT.NONE);
    deleteItem.setText("Delete");

    new MenuItem(menu, SWT.SEPARATOR);
    // 添加复选菜单项
    MenuItem checkItem = new MenuItem(menu, SWT.CHECK);
    checkItem.setText("Check");
    checkItem.setSelection(true);
    checkItem.setImage(square);

    // 添加一个push菜单项
    MenuItem pushItem = new MenuItem(menu, SWT.PUSH);
    pushItem.setText("ush");

    new MenuItem(menu, SWT.SEPARATOR);

    // 创建一些单选菜单项
    MenuItem item1 = new MenuItem(menu, SWT.RADIO);
    item1.setText("Radio One");
    item1.setImage(triangle);
    MenuItem item2 = new MenuItem(menu, SWT.RADIO);
    item2.setText("Radio Two");
    MenuItem item3 = new MenuItem(menu, SWT.RADIO);
    item3.setText("Radio Three");

    //创建一个单选的菜单项组
    new MenuItem(menu, SWT.SEPARATOR);

    // 创建一些单选项
    MenuItem itema = new MenuItem(menu, SWT.RADIO);
    itema.setText("Radio A");
    MenuItem itemb = new MenuItem(menu, SWT.RADIO);
    itemb.setText("Radio B");
    MenuItem itemc = new MenuItem(menu, SWT.RADIO);
    itemc.setText("Radio C");

    //创建一个新的下拉菜单
    Menu newMenu = new Menu(menu);
    newItem.setMenu(newMenu);

    // 创建此新的下拉菜单的菜单项
    MenuItem shortcutItem = new MenuItem(newMenu, SWT.NONE);
    shortcutItem.setText("Shortcut");
    MenuItem iconItem = new MenuItem(newMenu, SWT.NONE);
    iconItem.setText("Icon");

    // 设置此菜单与Label关联
    label.setMenu(menu);
  }
  /**
   * 创建非单选组的菜单
   */
  private void createNoRadioGroupPopUpMenu(Shell shell) {
    // 创建面板与菜单关联
    Label label = new Label(shell, SWT.BORDER);
    label.setText("No Radio Group Menu");


    Menu menu = new Menu(shell, SWT.POP_UP | SWT.NO_RADIO_GROUP);
    label.setMenu(menu);

    MenuItem item1 = new MenuItem(menu, SWT.RADIO);
    item1.setText("Radio One");
    MenuItem item2 = new MenuItem(menu, SWT.RADIO);
    item2.setText("Radio Two");
    MenuItem item3 = new MenuItem(menu, SWT.RADIO);
    item3.setText("Radio Three");

    label.setMenu(menu);
  }
  public static void main(String[] args) {
    new Menus().run();
  }
}

Menus.jpg (21.94 KB, 下载次数: 17)

Menus.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP