??xml version="1.0" encoding="utf-8" standalone="yes"?>
1、Eclipse中swt的配|?br />配置Qjdk1.4.2以及(qing)eclipse3.1
在代码中调用swt控g之前Q首先徏立一个项目,然后选择该项目的properties -> Java Build PathQ将standard Widget ToolKit加入到Library当中。如下图所C:(x)
接下来可以徏立第一个eclipse程序,新徏一个classQƈ且在该class所对应的代码中输入如下E序Q其中package以及(qing)class名称Ҏ(gu)实际情况来确定名U?br />package mypakage;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
/*导入需要的cd*/
public class Myfrm1 {
public Myfrm1() {
super();
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
/*shellZ个窗口对?/
Label label = new Label(shell, SWT.NONE);
label.setText("Hello, World!"); /*创徏一个标{֯象ƈ且设|标题文?/
label.pack();
shell.pack();
shell.open(); /*打开q显C窗?/
while(!shell.isDisposed())
if(!display.readAndDispatch())
display.sleep(); /*在窗口没有销毁之前,昄对象一直处于等待状?/
display.dispose(); /*否则Q销毁对象,释放对象所占据的资?/
label.dispose();
}
}
q行上述代码Qrun -> debug -> swt applicationQ将产生如下所C的一个窗?
2、button的?br />按钮可能的类型有很多Q例如:(x)
SWT.BORDER 含有Ҏ(gu)的按?br />SWT.CHECK 复选按?br />SWT.PUSH 普通按?br />SWT.RADIO 单选按?br />3、Text的?br />文本框的cd也有很多U选择Q例如:(x)
SWT.BORDER 含有Ҏ(gu)
SWT.READ_ONLY 只读
下图为包含按钮以?qing)文本框的窗?br />设计上述H口所对应的代码ؓ(f)Q?br />package mypakage;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
public class Myfrm1 {
public Myfrm1() {
super();
}
public static void main(String[] args) {
Display display = new Display( );
Shell shell = new Shell(display);
shell.setSize(300, 200);
shell.setLayout(new RowLayout( ));
shell.setText("Button Example");
final Button button = new Button(shell, SWT.BORDER);
button.setText("Click Me");
final Text text = new Text(shell, SWT.BORDER);
shell.open( );
while(!shell.isDisposed( )) {
if(!display.readAndDispatch( )) display.sleep( );
}
display.dispose( );
}
}
如果惛_控g的位|以?qing)大进行精的讄Q可以用setBounds(x, y, width, height)Ҏ(gu)来取代shell.setLayout(new RowLayout( ))。例如:(x)button.setBounds(80, 80, 90, 20);
button的监听及(qing)事g处理
Ҏ(gu)钮单M件处理的代码Q?br /> button.addSelectionListener(new SelectionListener( )
{
public void widgetSelected(SelectionEvent event)
{
text.setText("No worries!");
}
public void widgetDefaultSelected(SelectionEvent event)
{
text.setText("No worries!");
}
});
以上代码加入到shell.open之前Q当点击按钮时生以下效果:(x)
分析Q由于ؓ(f)button按钮增加了一个监听器Q按钮时d于被“监控”的状态,当按钮控件被选择Q点击)(j)既选择事g发生ӞҎ(gu)本控件进行赋值”No worries”?br />Ҏ(gu)监听事g的原理,设计如下E序Q该E序能够获得鼠标的X坐标Q显C在文本框中Q?br />package mypakage;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
public class Myfrm1 {
public Myfrm1() {
super();
}
public static void main(String[] args) {
Display display = new Display( );
Shell shell = new Shell(display);
shell.setSize(300, 200);
shell.setLayout(new RowLayout( ));
final Text text = new Text(shell, SWT.SHADOW_IN);
shell.addMouseMoveListener(new MouseMoveListener( )
{
public void mouseMove(MouseEvent e)
{
Integer y=new Integer(e.x); /*x坐标转换为Integercd的对?/
text.setText(y.toString());
}
});
shell.open( );
while(!shell.isDisposed( )) {
if(!display.readAndDispatch( )) display.sleep( );
}
display.dispose( );
}
}
监听方式Q?br />ControlListener 用于处理Ud以及(qing)寸变化
FocusListener 用于处理得到焦点以及(qing)失去焦点
KeyListener 处理按键的输?/p>
MouseListener , MouseMoveListener, MouseTrackListener 寚w标的动作q行处理
SelectionListener 处理控g的选择行ؓ(f)Q包括按钮的点击Q?/p>
注意Q监听方式与其所能够处理的事件具有一定的兌性,既监听方式决定了所能够处理事g的种c,例如Q?/p>
shell.addMouseListener(new MouseListener( )
{
public void mouseMove(MouseEvent e)
{text.setText("mousemove");}
public void mouseDoubleClick(MouseEvent e)
{text.setText("mousedbclc");}
public void mouseDown(MouseEvent e)
{}
public void mouseUp(MouseEvent e)
{}
});
你会(x)发现在鼠标移动时Qtext.setText("mousemove");始终不能够执行;q且mouseDown、mouseUp事g不能够省略,原因在于MouseListener只能处理mouseDoubleClick、mouseDown、mouseUp三类事gQ而且q三cM件不能够分离?br />3、List控g
List控g的样式包括:(x)
SWT.BORDER 含有Ҏ(gu)
SWT.H_SCROLL 含有水^滚动?/p>
SWT.V_SCROLL 含有垂直滚动?/p>
SWT.SINGLE 允许单?/p>
SWT.MULTI 允许复?/p>
若要创徏一个含有从11个元素的ListQ可以通过以下代码来实?br />final List list = new List (shell, SWT.SINGLE);
for (int i=0;i<=10;i++)
list.add("item"+i);
以下实例能够判断List控g中所选择的选项Qƈ且输出显C在控制CQ?br />package mypakage;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
public class Myfrm1 {
public Myfrm1() {
super();
}
public static void main(String[] args) {
Display display = new Display ( );
Shell shell = new Shell (display);
shell.setText("List Example");
shell.setSize(300, 200);
shell.setLayout(new FillLayout(SWT.VERTICAL));
final List list = new List (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
for (int loopIndex = 0; loopIndex < 100; loopIndex++){
list.add("Item " + loopIndex);
}
list.addSelectionListener(new SelectionListener( )
{
public void widgetSelected(SelectionEvent event)
{
int selections[] = list.getSelectionIndices ( );
String outText = "";
for (int loopIndex = 0; loopIndex < selections.length;
loopIndex++) outText += selections[loopIndex] + " ";
System.out.println ("You selected: " + outText);
}
public void widgetDefaultSelected(SelectionEvent event)
{
int [] selections = list.getSelectionIndices ( );
String outText = "";
for (int loopIndex = 0; loopIndex < selections.length; loopIndex++)
outText += selections[loopIndex] + " ";
System.out.println ("You selected: " + outText);
}
});
shell.open ( );
while (!shell.isDisposed ( )) {
if (!display.readAndDispatch ( )) display.sleep ( );
}
display.dispose ( );
}
}
效果图:(x)
You selected: 4 5 6 7 8 9 10
分析Qlist.getSelectionIndices ( )Ҏ(gu)会(x)获得被选择目的集合, selections[]或者[] elections表示动态一l数l?/p>
4、Menu控g
建立菜单的一般步骤ؓ(f)Q?br />1、在建立菜单Ӟ首先需要徏立一个菜单栏Q需要用SWT.BAR属?br />Menu menuBar = new Menu(shell, SWT.BAR);
2、在菜单栏的基础之上Q创Z拉菜单的所对应的顶U菜单项Q需要用SWT.CASCADE属?br />fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
fileMenuHeader.setText("&File");
3、徏立与菜单相关的下拉式菜?br />dropMenu1 = new Menu(shell, SWT.DROP_DOWN);
4、将菜单与下拉菜单兌
MenuHeader1.setMenu(dropMenu1);
5、ؓ(f)下拉菜单d子菜单项
dropitem1= new MenuItem(dropMenu1, SWT.PUSH);
dropitem1.setText("open");
?br />?/p>
6、最后,在窗口中指定需要显C的菜单?br />shell.setMenuBar(menuBar);
菜单的监听及(qing)事g
参照按钮的监听以?qing)事Ӟ设计如下E序Q当点击 File子菜单下的“open”时Q在文本框中昄“click open menu!?br />dropitem1.addSelectionListener(new SelectionListener()
{
public void widgetSelected(SelectionEvent event)
{
text.setText("click open menu!");
}
public void widgetDefaultSelected(SelectionEvent event)
{
text.setText("click open menu!");
}
});
5、用工htoobar
建立工具栏可以通过如下方式QToolBar toolbar = new ToolBar(shell, SWT.NONE);
在工h的基之上创徏工具栏子按钮Qƈ且设|子按钮的标题:(x)
ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
item1.setText("item1");
例如Q?br /> ToolBar toolbar = new ToolBar(shell, SWT.NONE);
ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
item1.setText("item1");
ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);
item2.setText("item2");
工具栏的监听?qing)事?br />实例Q创Z个监听对象,该监听对象应用于每一个按钮,最l来判断鼠标点击的是哪一个按钮,效果囑֦下?br /> Listener listener = new Listener( ) {
public void handleEvent(Event event) {
ToolItem item =(ToolItem)event.widget;
String string = item.getText( );
text.setText("You selected:" + string); }
};
item1.addListener(SWT.Selection, listener);
item2.addListener(SWT.Selection, listener);
item3.addListener(SWT.Selection, listener);
item4.addListener(SWT.Selection, listener);
6、滚动条slider的?br />滚动条分为有Ҏ(gu)、垂直、水q三U类型,利用slider.setBoundsҎ(gu)可以指定滚动条所在的位置?br />滚动条所能够处理事g的包括:(x)
SWT.ARROW_DOWN 向下或向x钮被点击
SWT.ARROW_UP 向左或向上按钮被点击
SWT.DRAG 滑块按钮被托?/p>
SWT.END 滑块到达l点
SWT.HOME 滑块到达L(fng)
SWT.PAGE_DOWN 下方或右侧的滚动条被点击
SWT.PAGE_UP 上方或左侧的滚动条被点击
实例Q根据滑块的位置Ud按钮位置
slider.addListener(SWT.Selection, new Listener( ) {
public void handleEvent(Event event) {
switch(event.detail) {
case SWT.ARROW_DOWN: button.setBounds(slider.getSelection(),0,20,10);
break;
case SWT.ARROW_UP:button.setBounds(slider.getSelection(),0,20,10);
break;
case SWT.DRAG:button.setBounds(slider.getSelection(),0,20,10);
break;
case SWT.END:button.setBounds(slider.getSelection(),0,20,10);
break;
case SWT.HOME:button.setBounds(slider.getSelection(),0,20,10);
break;
case SWT.PAGE_DOWN:button.setBounds(slider.getSelection(),0,20,10);
break;
case SWT.PAGE_UP:button.setBounds(slider.getSelection(),0,20,10);
break;
}
}
});
7、树(wi)形控件Tree
?wi)Ş控g使用的方法ؓ(f)Q首先创Z个Treecd的对象,其次在该对象的基之上l箋扩展节点Q以?qing)扩展节点的子节炏V?br />final Tree tree = new Tree(shell, SWT.BORDER);
可以利用tree.setSizeҎ(gu)来改变树(wi)形控件的大小。在创徏节点Ӟ需要指明该节点所依赖的父节点的名Uͼ如TreeItem item0 = new TreeItem(tree, 0);Q那么item0成为tree对象中的0U(Q节炏V?br />如下E序在tree对象的基之上产生9个节点:(x)
final Tree tree = new Tree(shell, SWT.BORDER);
tree.setSize(290, 290);
for(int loopIndex1 = 2000; loopIndex1 <= 2008; loopIndex1++) {
TreeItem item0 = new TreeItem(tree, 0);
item0.setText("Year " + loopIndex1);
}
在上q实例的基础上ؓ(f)每一?U节点的基础上扩展出12个节点:(x)
for(int loopIndex1 = 2000; loopIndex1 <= 2008; loopIndex1++) {
TreeItem item0 = new TreeItem(tree, 0);
item0.setText("Year " + loopIndex1);
for(int loopIndex2 = 1; loopIndex2 <= 12; loopIndex2++) {
TreeItem item1 = new TreeItem(item0, 0);
item1.setText("Month " + loopIndex2);
}
}
8、对话框dialog
对话框是一个依托于ȝ体的子窗体,如图所C?br />
例如Q当在主H体中点?yn)L钮时Q弹Z个对话框dialogQ当关闭对话框时按钮昄“dialog is disposed?br />
Display display = new Display( );
final Shell shell = new Shell(display);
shell.setSize(300, 200);
shell.setText("main");
final Button opener = new Button(shell, SWT.PUSH);
opener.setText("Click Me");
opener.setBounds(20, 20, 50, 25);
final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL |
SWT.DIALOG_TRIM);
dialog.setText("dialog");
dialog.setBounds(10,10,50,60);
dialog.addDisposeListener(new DisposeListener(){
public void widgetDisposed(DisposeEvent e){
opener.setText("dialog is disposed");
}
});
Listener openerListener = new Listener( ) {
public void handleEvent(Event event) {
dialog.open( );
}
};
opener.addListener(SWT.Selection, openerListener);
shell.open( );
while(!dialog.isDisposed( )) {
if(!display.readAndDispatch( )) display.sleep( );
}
while (!shell.isDisposed( )) {
if (!display.readAndDispatch( ))
display.sleep( );
}
display.dispose( );