Java程序设计之swt教程

内容发布更新时间 : 2024/5/14 20:10:50星期一 下面是文章的全部内容请认真阅读。

}

}

text=new Text(shell,SWT.BORDER|SWT.WRAP); RowData rowData=new RowData();

rowData.width=100; rowData.height=50;

text.setLayoutData(rowData);

text.addMouseListener(new MouseAdapter() { //采用鼠标监听适配器 public void mouseDoubleClick(MouseEvent e) { //监听鼠标双击事件的方法 text.setText(\文本框中鼠标双击事件发生!\); //在text中显示信息 MessageBox dialog=new MessageBox(shell,SWT.OK|SWT.ICON_INFORMATION);

dialog.setText(\);

dialog.setMessage(\文本框中鼠标双击事件发生!\); dialog.open();

//将鼠标监听器用于text组件

//声明信息对话框对象,并在对话框中显示信息

}});

shell.pack(); shell.open();

while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } }

display.dispose();

运行结果如图4.36所示。当在文本框中双击鼠标时,文本框中显示信息并弹出一个对话框,如图4.37所示。

图4.36 鼠标双击事件 图4.37 双击后弹出的对话框 例4.19 键盘监听器,监听键盘事件。 package edu.ch4;

import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.events.*; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.*; public class Sample4_19 { Text text1,text2;

public Sample4_19() {

Display display = new Display();

Shell shell = new Shell(display,SWT.SHELL_TRIM); GridLayout layout=new GridLayout(); layout.numColumns=2; shell.setLayout(layout); shell.setText(\);

Label label1=new Label(shell,SWT.RIGHT); label1.setText(\);

text1=new Text(shell,SWT.BORDER|SWT.WRAP); GridData gridData1=new GridData(100,30); text1.setLayoutData(gridData1);

text1.addKeyListener(new KeyAdapter(){ //添加按键监听器于text1上 public void keyPressed(KeyEvent e) { //监听键盘按键

if(e.keyCode==SWT.CR) //当按键为回车键时触发

text2.setText(text1.getText());}});

Label label2=new Label(shell,SWT.RIGHT); label2.setText(\);

text2=new Text(shell,SWT.BORDER|SWT.WRAP); GridData gridData2=new GridData(100,30); text2.setLayoutData(gridData2); text2.setEditable(false);

text2.setBackground(new Color(display,255,255,255)); shell.pack(); shell.open();

while (!shell.isDisposed()) {

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

display.dispose(); }

public static void main(String[] args) { Sample4_19 s4_19=new Sample4_19(); } }

在文本框text1中输入信息,按回车键时将text1中的信息显示在text2中。运行结果如图4.38所示。

图4.38 键盘监听事件

例4.20 组件选择监听器,监听组件选择事件。 package edu.ch4;

import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.events.*; import org.eclipse.swt.SWT; public class Sample4_20 {

static Display display = new Display(); public static void main(String[] args) {

static final Shell shell = new Shell(display,SWT.SHELL_TRIM); shell.setText(\组件选择事件示例\); Button button=new Button(shell,SWT.PUSH); button.setText(\请点击我\); RowLayout layout=new RowLayout(); layout.marginHeight=10; layout.marginWidth=20; shell.setLayout(layout);

RowData data=new RowData(80,40); button.setLayoutData(data);

button.addSelectionListener(new SelectionAdapter(){ public void widgetSelected(SelectionEvent e){

MessageBox dialog=new MessageBox(shell,SWT.OK|SWT.ICON_INFORMATION); dialog.setText(\组件选择事件\); dialog.setMessage(\你好,SWT世界!\); dialog.open();

}

});

shell.pack(); shell.open();

while (!shell.isDisposed()) {

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

display.dispose(); }

运行结果如图4.39所示,当点击按钮时,弹出一个信息对话框,如图4.40所示。 }

图4.39 组件选择事件 图4.40 信息对话框

以上示例中,在button.addSelectionListener()方法内部建立了一个匿名内部类,该类继

承于SelectionAdapter,在widgetSelected()方法中写下了事件处理的代码。这种方式书写代码简单方便,容易掌握。但由于事件处理代码会随着组件一起分散在代码中的各个部分,当工具栏、菜单栏等也需要处理相同的用户行为时,无法重用事件中的处理代码。

事件处理的另一种方法是采用命名内部类。可以解决匿名内部类存在的问题。把例4.20改为命名内部类,代码如下: package edu.ch4;

import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.events.*; import org.eclipse.swt.SWT; public class Sample4_20_2 {

static Display display = new Display(); public static void main(String[] args) {

static final Shell shell = new Shell(display,SWT.SHELL_TRIM); shell.setText(\组件选择事件示例\); Button button=new Button(shell,SWT.PUSH); button.setText(\请点击我\); RowLayout layout=new RowLayout(); layout.marginHeight=10; layout.marginWidth=20; shell.setLayout(layout);

RowData data=new RowData(80,40); button.setLayoutData(data);

button.addSelectionListener(new MySelectionListener()); shell.pack(); shell.open();

while (!shell.isDisposed()) {

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

display.dispose(); }

}

private static final class MySelectionListener extends SelectionAdapter{ }

public void widgetSelected(SelectionEvent e){

MessageBox dialog=new MessageBox(shell,SWT.OK|SWT.ICON_INFORMATION); dialog.setText(\组件选择事件\); dialog.setMessage(\你好,SWT世界!\); dialog.open();

}

运行结果相同。类MySelectionListener在主类Sample4_20_2的内部,所以称为命名内部

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi