java 事件 分类 gui_java GUI 事件监听实例

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.graphics.Point;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.Text;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.widgets.Label;

public class ExMouseEvent {

private Shell sShell = null; // @jve:decl-index=0:visual-constraint="-122,-134"

private Button button = null;

private Text textArea = null;

private Text text = null;

private Text text1 = null;

private Button button1 = null;

private Text text2 = null;

public static void main(String[] args) {

// TODO Auto-generated method

stub

Display display =

Display.getDefault();

ExMouseEvent thisClass = new

ExMouseEvent();

thisClass.createSShell();

thisClass.sShell.open();

while

(!thisClass.sShell.isDisposed()) {

if

(!display.readAndDispatch())

display.sleep();

}

display.dispose();

}

private void createSShell() {

GridData gridData4 = new

GridData();

gridData4.horizontalSpan =

2;

GridData gridData3 = new

GridData();

gridData3.horizontalSpan =

2;

gridData3.widthHint =

150;

GridData gridData2 = new

GridData();

gridData2.widthHint =

210;

gridData2.horizontalSpan =

3;

GridData gridData1 = new

GridData();

gridData1.horizontalIndent =

0;

gridData1.heightHint =

100;

gridData1.horizontalSpan =

3;

gridData1.widthHint =

210;

GridLayout gridLayout = new

GridLayout();

gridLayout.numColumns =

3;

gridLayout.verticalSpacing =

15;

GridData gridData = new

GridData();

gridData.heightHint =

100;

gridData.horizontalSpan =

3;

gridData.widthHint = 200;

sShell = new Shell();

sShell.setText("Shell");

sShell.setLayout(gridLayout);

sShell.setSize(new Point(287,

399));

button = new Button(sShell,

SWT.NONE);

button.setText("初始按钮");

button1 = new Button(sShell,

SWT.NONE);

button1.setText("窗口移动到");

button1.setLayoutData(gridData3);

sShell.addMouseMoveListener(new

org.eclipse.swt.events.MouseMoveListener() {

public void

mouseMove(org.eclipse.swt.events.MouseEvent e) {

//System.out.println("mouseMove()");

// TODO Auto-generated Event stub mouseMove()

textArea.setText("鼠标移动到窗口中("+e.x+","+e.y+")的位置");

}

});

sShell.addControlListener(new

org.eclipse.swt.events.ControlListener() {

public void

controlMoved(org.eclipse.swt.events.ControlEvent e) {

//System.out.println("controlMoved()");

// TODO Auto-generated Event stub controlMoved()

button1.setText("窗口移动到("+sShell.getBounds().x+","+sShell.getBounds().y+")");

}

public void

controlResized(org.eclipse.swt.events.ControlEvent e) {

int widthShell=sShell.getBounds().width;

int heightShell=sShell.getBounds().height;

int widthButton1=button1.getBounds().width;

int heightButton1=button1.getBounds().height;

int button1X=widthShell/2-widthButton1/2;

int button1Y=heightShell/2-heightButton1/2;

button1.setLocation(button1X, button1Y);

button1.setVisible(true);

}

});

textArea = new Text(sShell,

SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);

textArea.setLayoutData(gridData);

button.addMouseListener(new

org.eclipse.swt.events.MouseListener() {

public void

mouseDown(org.eclipse.swt.events.MouseEvent e) {

//System.out.println("mouseDown()");

// TODO Auto-generated Event stub mouseDown()

String

btno=e.button==1?"左键":(e.button==2?"中键":"右键");

textArea.setText("鼠标在"+e.getSource().toString()+"按下");

textArea.append("\n你按下的是:"+btno);

}

public void

mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {

textArea.setText("鼠标双击了"+e.getSource().toString());

}

public void

mouseUp(org.eclipse.swt.events.MouseEvent e) {

String

btno=e.button==1?"左键":(e.button==2?"中键":"右键");

textArea.setText("鼠标在"+e.getSource().toString()+"键被松开");

textArea.append("\n你松开的是:"+btno);

}

});

text = new Text(sShell,

SWT.BORDER | SWT.MULTI);

text.setLayoutData(gridData1);

text.addKeyListener(new

org.eclipse.swt.events.KeyListener() {

public void

keyPressed(org.eclipse.swt.events.KeyEvent e) {

//System.out.println("keyPressed()");

// TODO Auto-generated Event stub keyPressed()

text.setText("你按下了键: "+e.character);

text.append(",该键的键码是:"+e.keyCode);

if ((e.stateMask & SWT.ALT)!=0) {

text.setText("你按下了ALT键");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值