首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java SWT事件

Java SWT事件

作者头像
汤高
发布2018-01-11 17:28:35
1.9K0
发布2018-01-11 17:28:35
举报
文章被收录于专栏:积累沉淀积累沉淀

什么是事件?点击鼠标是一个事件,按下一个按钮也一个事件,关闭一个窗口也是一个事件。 什么是监听器?监听器就是监听事件什么时候发生的,用来控制事件发生的具体动作。(个人见解) 事件产生处的SWT组件称为事件源,(官方) 对事件作出具体动作称为监听器(Listener)。监听器负责监听组件上的事件,并对发生的事件进行处理。基本的模式是将一个监听器添加到已经创建的组件中,当相应的事件发生时,监听器的代码就会被执行。

SWT 的常用事件 每一种类型的监听器,都有一个接口来定义这种监听器,由类提供事件信息,由应用程序接口方法负责添加监听器。如果一个监听器接口中定义了多个方法,则会提供一个适配器来实现监听器接口并同时提供空方法。所有的事件、监听器和适配器都放在包org.eclipse.swt.events中。

例如,添加组件选择事件的监听器为addSelectionListener,事件为 SelectionEvent,相应的适配器为SelectionAdapter。添加鼠标事件的监听器为addMouseListener,事件为MouseEvent,相应的适配器为MouseAdapter。SWT中常用的事件如下: 1.addMouseListener 鼠标监听器。常用方法: mouseDown() 鼠标按下时触发。 mouseUP() 鼠标放开时触发。 mouseDoubleClick() 鼠标双击时触发。 2.addKeyListener 按键监听器。常用方法: keyPressed() 当焦点在组件上时,按下键盘任一键时触发。但对某些组件(如按钮Button),按回车键时不能触发。keyReleased() 按键弹起时触发。 3.addSelectionListener 组件选择监听器。常用方法: widgetSelected() 当组件被选择(单击鼠标、焦点在组件上时按回车键)时触发。 4.addFocusListener 焦点监听器。常用方法: focusGained() 得到焦点时触发。 focusLost() 失去焦点时触发。

SWT 的常用监听器应用实例 鼠标监听器,监听鼠标双击事件。

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_18 {
static Text text;
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
RowLayout rowLayout=new RowLayout();
rowLayout.marginWidth=20;
rowLayout.marginHeight=30;
shell.setLayout(rowLayout);
shell.setText("SWT事件处理示例");
PDF 文件使用 "pdfFactory" 试用版本创建www.fineprint.cn
text=new Text(shell,SWT.BORDER|SWT.WRAP);
RowData rowData=new RowData();
rowData.width=100;
rowData.height=50;
text.setLayoutData(rowData);
//将鼠标监听器用于text组件
text.addMouseListener(new MouseAdapter() { //采用鼠标监听适配器
public void mouseDoubleClick(MouseEvent e) { //监听鼠标双击事件的方法
text.setText("文本框中鼠标双击事件发生!"); //在text中显示信息
//声明信息对话框对象,并在对话框中显示信息
MessageBox dialog=new MessageBox(shell,SWT.OK|SWT.ICON_INFORMATION);
dialog.setText("Double click");
dialog.setMessage("文本框中鼠标双击事件发生!");
dialog.open();
}});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}

键盘监听器,监听键盘事件。

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;
PDF 文件使用 "pdfFactory" 试用版本创建www.fineprint.cn
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("Event demo");
Label label1=new Label(shell,SWT.RIGHT);
label1.setText("text1:");
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:");
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();
}
}

组件选择监听器,监听组件选择事件。

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();
static final Shell shell = new Shell(display,SWT.SHELL_TRIM);
public static void main(String[] args) {
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();
}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-06-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档