swt - 图文 联系客服

发布时间 : 星期三 文章swt - 图文更新完毕开始阅读c3231728647d27284b7351af

label.setText(\创建一个标签对象并且设置标题文字*/ label.pack(); shell.pack();

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

if(!display.readAndDispatch())

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

}

运行上述代码(run -> debug -> swt application)将产生如下所示的一个窗口

5.7.2 button的使用

按钮可能的类型有很多,例如: SWT.BORDER 含有边框的按钮 SWT.CHECK 复选按钮 SWT.PUSH 普通按钮

SWT.RADIO 单选按钮 3、Text的使用

文本框的类型也有很多种选择,例如: SWT.BORDER 含有边框 SWT.READ_ONLY 只读

下图为包含按钮以及文本框的窗口

设计上述窗口所对应的代码为: 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(\

final Button button = new Button(shell, SWT.BORDER); button.setText(\

final Text text = new Text(shell, SWT.BORDER); shell.open( );

while(!shell.isDisposed( )) {

if(!display.readAndDispatch( )) display.sleep( ); }

display.dispose( ); }

}

如果想对控件的位置以及大小进行精确的设置,可以使用setBounds(x, y, width, height)方法来取代shell.setLayout(new RowLayout( ))。例如:button.setBounds(80, 80, 90, 20); button的监听及事件处理

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

button.addSelectionListener(new SelectionListener( ) {

public void widgetSelected(SelectionEvent event) {

text.setText(\}

public void widgetDefaultSelected(SelectionEvent event)

{

text.setText(\} });

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

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

根据监听事件的原理,设计如下程序,该程序能够获得鼠标的X坐标,显示在文本框中: 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坐标转换为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(new MouseListener( ) {

public void mouseMove(MouseEvent e) {text.setText(\

public void mouseDoubleClick(MouseEvent e) {text.setText(\

public void mouseDown(MouseEvent e) {}

public void mouseUp(MouseEvent e) {}

});

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

5.7.3 List控件

List控件的样式包括:

SWT.BORDER 含有边框 SWT.H_SCROLL 含有水平滚动条 SWT.V_SCROLL 含有垂直滚动条 SWT.SINGLE 允许单选 SWT.MULTI 允许复选

若要创建一个含有从11个元素的List,可以通过以下代码来实现 final List list = new List (shell, SWT.SINGLE); for (int i=0;i<=10;i++) list.add(\

以下实例能够判断List控件中所选择的选项,并且输出显示在控制台中: 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(\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(\}

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 (\ou selected: \}

public void widgetDefaultSelected(SelectionEvent event) {

int [] selections = list.getSelectionIndices ( ); String outText = \