Swt常用控件中文教程.docx

上传人:b****6 文档编号:8118054 上传时间:2023-01-28 格式:DOCX 页数:9 大小:18.48KB
下载 相关 举报
Swt常用控件中文教程.docx_第1页
第1页 / 共9页
Swt常用控件中文教程.docx_第2页
第2页 / 共9页
Swt常用控件中文教程.docx_第3页
第3页 / 共9页
Swt常用控件中文教程.docx_第4页
第4页 / 共9页
Swt常用控件中文教程.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

Swt常用控件中文教程.docx

《Swt常用控件中文教程.docx》由会员分享,可在线阅读,更多相关《Swt常用控件中文教程.docx(9页珍藏版)》请在冰豆网上搜索。

Swt常用控件中文教程.docx

Swt常用控件中文教程

Swt常用控件中文教程

1、Eclipse中swt的配置

建议配置:

jdk1.4.2以及eclipse3.1

在代码中调用swt控件之前,首先建立一个项目,然后选择该项目的properties->JavaBuildPath,将standardWidgetToolKit加入到Library页当中。

如下图所示:

接下来可以建立第一个eclipse小程序,新建一个class,并且在该class所对应的代码中输入如下程序,其中package以及class名称根据实际情况来确定名称。

packagemypakage;

importorg.eclipse.swt.widgets.*;

importorg.eclipse.swt.*;

/*导入需要的类库*/

publicclassMyfrm1{

publicMyfrm1(){

super();

}

publicstaticvoidmain(String[]args){

Displaydisplay=newDisplay();

Shellshell=newShell(display);

/*shell为一个窗口对象*/

Labellabel=newLabel(shell,SWT.NONE);

label.setText("Hello,World!

");/*创建一个标签对象并且设置标题文字*/

label.pack();

shell.pack();

shell.open();/*打开并显示窗口*/while(!

shell.isDisposed())

if(!

display.readAndDispatch())

display.sleep();/*在窗口没有销毁之前,显示对象一直处于等待状态*/display.dispose();/*否则,销毁对象,释放对象所占据的资源*/

label.dispose();

}

}运行上述代码(run->debug->swtapplication)将产生如下所示的一个窗口2、button的使用

按钮可能的类型有很多,例如:

SWT.BORDER含有边框的按钮SWT.CHECK复选按钮SWT.PUSH普通按钮SWT.RADIO单选按钮3、Text的使用

文本框的类型也有很多种选择,例如:

SWT.BORDER含有边框SWT.READ_ONLY只读下图为包含按钮以及文本框的窗口设计上述窗口所对应的代码为:

packagemypakage;

importorg.eclipse.swt.widgets.*;

importorg.eclipse.swt.SWT;

importorg.eclipse.swt.events.*;

importorg.eclipse.swt.layout.*;

publicclassMyfrm1{

publicMyfrm1(){

super();

}

publicstaticvoidmain(String[]args){

Displaydisplay=newDisplay();

Shellshell=newShell(display);

shell.setSize(300,200);

shell.setLayout(newRowLayout());

shell.setText("ButtonExample");

finalButtonbutton=newButton(shell,SWT.BORDER);

button.setText("ClickMe");

finalTexttext=newText(shell,SWT.BORDER);

shell.open();while(!

shell.isDisposed()){

if(!

display.readAndDispatch())display.sleep();

}

display.dispose();

}

}如果想对控件的位置以及大小进行精确的设置,可以使用setBounds(x,y,width,height)方法来取代shell.setLayout(newRowLayout())。

例如:

button.setBounds(80,80,90,20);button的监听及事件处理

对按钮单击事件处理的代码:

button.addSelectionListener(newSelectionListener()

{

publicvoidwidgetSelected(SelectionEventevent)

{

text.setText("Noworries!

");

}

publicvoidwidgetDefaultSelected(SelectionEventevent){

text.setText("Noworries!

");

}

});将以上代码加入到shell.open之前,当点击按钮时产生以下效果:

分析:

由于为button按钮增加了一个监听器,按钮时刻处于被“监控”的状态,当按钮控件被选择(点击)既选择事件发生时,对文本控件进行赋值”Noworries”。

根据监听事件的原理,设计如下程序,该程序能够获得鼠标的X坐标,显示在文本框中:

packagemypakage;

importorg.eclipse.swt.widgets.*;

importorg.eclipse.swt.SWT;

importorg.eclipse.swt.events.*;

importorg.eclipse.swt.layout.*;publicclassMyfrm1{

publicMyfrm1(){

super();

}

publicstaticvoidmain(String[]args){

Displaydisplay=newDisplay();

Shellshell=newShell(display);

shell.setSize(300,200);

shell.setLayout(newRowLayout());

finalTexttext=newText(shell,SWT.SHADOW_IN);shell.addMouseMoveListener(newMouseMoveListener()

{

publicvoidmouseMove(MouseEvente)

{

Integery=newInteger(e.x);/*将x坐标转换为Integer类型的对象*/

text.setText(y.toString());

}

});shell.open();

while(!

shell.isDisposed()){

if(!

display.readAndDispatch())display.sleep();

}

display.dispose();

}

}监听方式:

ControlListener用于处理移动以及尺寸变化FocusListener用于处理得到焦点以及失去焦点KeyListener处理按键的输入MouseListener,MouseMoveListener,MouseTrackListener对鼠标的动作进行处理SelectionListener处理控件的选择行为(包括按钮的点击)注意:

监听方式与其所能够处理的事件具有一定的关联性,既监听方式决定了所能够处理事件的种类,例如:

shell.addMouseListener(newMouseListener()

{

publicvoidmouseMove(MouseEvente)

{text.setText("mousemove");}

publicvoidmouseDoubleClick(MouseEvente)

{text.setText("mousedbclc");}

publicvoidmouseDown(MouseEvente)

{}

publicvoidmouseUp(MouseEvente)

{}

});你会发现在鼠标移动时,text.setText("mousemove");始终不能够执行;并且mouseDown、mouseUp事件不能够省略,原因就在于MouseListener只能处理mouseDoubleClick、mouseDown、mouseUp三类事件,而且这三类事件不能够分离。

3、List控件

List控件的样式包括:

SWT.BORDER含有边框SWT.H_SCROLL含有水平滚动条SWT.V_SCROLL含有垂直滚动条SWT.SINGLE允许单选SWT.MULTI允许复选若要创建一个含有从11个元素的List,可以通过以下代码来实现

finalListlist=newList(shell,SWT.SINGLE);

for(inti=0;i<=10;i++)

list.add("item"+i);以下实例能够判断List控件中所选择的选项,并且输出显示在控制台中:

packagemypakage;

importorg.eclipse.swt.widgets.*;

importorg.eclipse.swt.SWT;

importorg.eclipse.swt.events.*;

importorg.eclipse.swt.layout.*;

publicclassMyfrm1{

publicMyfrm1(){

super();

}

publicstaticvoidmain(String[]args){

Displaydisplay=newDisplay();

Shellshell=newShell(display);

shell.setText("ListExample");

shell.setSize(300,200);

shell.setLayout(newFillLayout(SWT.VERTICAL));

finalListlist=newList(shell,SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);

for(intloopIndex=0;loopIndex<100;loopIndex++){

list.add("Item"+loopIndex);

}

list.addSelectionListener(newSelectionListener()

{

publicvoidwidgetSelected(SelectionEventevent)

{

intselections[]=list.getSelectionIndices();

StringoutText="";

for(intloopIndex=0;loopIndex

loopIndex++)outText+=selections[loopIndex]+"";

System.out.println("Youselected:

"+outText);

}

publicvoidwidgetDefaultSelected(SelectionEventevent)

{

int[]selections=list.getSelectionIndices();

StringoutText="";

for(intloopIndex=0;loopIndex

outText+=selections[loopIndex]+"";

System.out.println("Youselected:

"+outText);

}

});

shell.open();

while(!

shell.isDisposed()){

if(!

display.readAndDispatch())display.sleep();

}

display.dispose();

}

}效果图:

Youselected:

45678910

分析:

list.getSelectionIndices()方法将会获得被选择项目的集合,selections[]或者[]elections表示动态一维数组。

4、Menu控件

建立菜单的一般步骤为:

1、在建立菜单时,首先需要建立一个菜单栏,需要使用SWT.BAR属性

MenumenuBar=newMenu(shell,SWT.BAR);2、在菜单栏的基础之上,创建下拉菜单的所对应的顶级菜单项,需要使用SWT.CASCADE属性

fileMenuHeader=newMenuItem(menuBar,SWT.CASCADE);

fileMenuHeader.setText("&File");3、建立与顶级菜单项相关的下拉式菜单

dropMenu1=newMenu(shell,SWT.DROP_DOWN);4、将顶级菜单项与下拉菜单关联

MenuHeader1.setMenu(dropMenu1);5、为下拉菜单添加子菜单项

dropitem1=newMenuItem(dropMenu1,SWT.PUSH);

dropitem1.setText("open");

…6、最后,在窗口中指定需要显示的菜单栏

shell.setMenuBar(menuBar);菜单的监听及事件

参照按钮的监听以及事件,设计如下程序,当点击File子菜单下的“open”时,在文本框中显示“clickopenmenu!

dropitem1.addSelectionListener(newSelectionListener()

{

publicvoidwidgetSelected(SelectionEventevent)

{

text.setText("clickopenmenu!

");

}

publicvoidwidgetDefaultSelected(SelectionEventevent)

{

text.setText("clickopenmenu!

");

}

});5、使用工具栏toobar

建立工具栏可以通过如下方式:

ToolBartoolbar=newToolBar(shell,SWT.NONE);

在工具栏的基础之上创建工具栏子按钮,并且设置子按钮的标题:

ToolItemitem1=newToolItem(toolbar,SWT.PUSH);

item1.setText("item1");例如:

ToolBartoolbar=newToolBar(shell,SWT.NONE);

ToolItemitem1=newToolItem(toolbar,SWT.PUSH);

item1.setText("item1");

ToolItemitem2=newToolItem(toolbar,SWT.PUSH);

item2.setText("item2");工具栏的监听及事件

实例:

创建一个监听对象,将该监听对象应用于每一个按钮,最终来判断鼠标点击的是哪一个按钮,效果图如下。

Listenerlistener=newListener(){

publicvoidhandleEvent(Eventevent){

ToolItemitem=(ToolItem)event.widget;

Stringstring=item.getText();

text.setText("Youselected:

"+string);}

};

item1.addListener(SWT.Selection,listener);

item2.addListener(SWT.Selection,listener);

item3.addListener(SWT.Selection,listener);

item4.addListener(SWT.Selection,listener);6、滚动条slider的使用

滚动条分为有边框、垂直、水平三种类型,利用slider.setBounds方法可以指定滚动条所在的位置。

滚动条所能够处理事件的包括:

SWT.ARROW_DOWN向下或向右按钮被点击SWT.ARROW_UP向左或向上按钮被点击SWT.DRAG滑块按钮被托动SWT.END滑块到达终点SWT.HOME滑块到达起点SWT.PAGE_DOWN下方或右侧的滚动条被点击SWT.PAGE_UP上方或左侧的滚动条被点击

实例:

根据滑块的位置移动按钮位置

slider.addListener(SWT.Selection,newListener(){publicvoidhandleEvent(Eventevent){switch(event.detail){caseSWT.ARROW_DOWN:

button.setBounds(slider.getSelection(),0,20,10);break;caseSWT.ARROW_UP:

button.setBounds(slider.getSelection(),0,20,10);break;caseSWT.DRAG:

button.setBounds(slider.getSelection(),0,20,10);break;caseSWT.END:

button.setBounds(slider.getSelection(),0,20,10);break;caseSWT.HOME:

button.setBounds(slider.getSelection(),0,20,10);break;caseSWT.PAGE_DOWN:

button.setBounds(slider.getSelection(),0,20,10);break;caseSWT.PAGE_UP:

button.setBounds(slider.getSelection(),0,20,10);break;}

}});7、树形控件Tree

树形控件使用的方法为,首先创建一个Tree类型的对象,其次在该对象的基础之上继续扩展节点,以及扩展节点的子节点。

finalTreetree=newTree(shell,SWT.BORDER);

可以利用tree.setSize方法来改变树形控件的大小。

在创建节点时,需要指明该节点所依赖的父节点的名称,如TreeItemitem0=newTreeItem(tree,0);,那么item0将成为tree对象中的0级(顶级)节点。

如下程序将在tree对象的基础之上产生9个节点:

finalTreetree=newTree(shell,SWT.BORDER);tree.setSize(290,290);for(intloopIndex1=2000;loopIndex1<=2008;loopIndex1++){TreeItemitem0=newTreeItem(tree,0);item0.setText("Year"+loopIndex1);}在上述实例的基础上为每一个0级节点的基础上扩展出12个节点:

for(intloopIndex1=2000;loopIndex1<=2008;loopIndex1++){TreeItemitem0=newTreeItem(tree,0);item0.setText("Year"+loopIndex1);for(intloopIndex2=1;loopIndex2<=12;loopIndex2++){TreeItemitem1=newTreeItem(item0,0);item1.setText("Month"+loopIndex2);

}}8、对话框dialog

对话框是一个依托于主窗体的子窗体,如图所示。

例如:

当在主窗体中点击按钮时,弹出一个对话框dialog,当关闭对话框时按钮显示“dialogisdisposed”Displaydisplay=newDisplay();finalShellshell=newShell(display);shell.setSize(300,200);shell.setText("main");finalButtonopener=newButton(shell,SWT.PUSH);opener.setText("ClickMe");opener.setBounds(20,20,50,25);finalShelldialog=newShell(shell,SWT.APPLICATION_MODAL|SWT.DIALOG_TRIM);dialog.setText("dialog");dialog.setBounds(10,10,50,60);dialog.addDisposeListener(newDisposeListener(){publicvoidwidgetDisposed(DisposeEvente){opener.setText("dialogisdisposed");}

});ListeneropenerListener=newListener(){publicvoidhandleEvent(Eventevent){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();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教学研究 > 教学案例设计

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1