java gui 事件_Java GUI事件处理

按钮事件的侦听处理

一个最简单的按钮问题

如下程序仅可实现图示界面。问题:修正程序,当用户按“Click

me”时,希望在文本框中显示计数信息,如:Your click numbers:*

import java.awt.*;

import javax.swing.*;

class ClickMe extends JFrame {

JButton b,c;

JTextField t;

ClickMe(){

super("Click me");

this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

t=new JTextField(20);

b=new JButton("Click me");

c=new JButton("Clear");

Container cc=this.getContentPane();

cc.setLayout(new FlowLayout());

cc.add(t);

cc.add(b);

cc.add(c);

this.setSize(300,120);

this.setVisible(true);

}

public static void

main(String[] aa){

new ClickMe();

}

}

单按钮的ActionEvent事件处理

ActionListener

当JButton 被Clicked后, 会产生ActionEvent事件。

Java提供了该事件的侦听器ActioListener接口,该接口只有一个成员方法,在事件发生时被调用:public

void actionPerformed(ActionEvent e)

事件处理三要点

继承ActionListener接口(implements)

² 可以在extends JFrame时直接implements

ActionListener

² ActionListener接口在java.awt.event包中

对按钮对象设置侦听:

按钮对象名.addActionListener(类名)

² 类名为继承了ActionListener的类,以响应用户的事件。

重写public void

actionPerformed方法,给出处理方案

² 方法应写在继承接口的类中。

² ActionListener接口将事件发送给actionPerformed()方法处理

练习:对以上ClickMe.java进行修改,以实现事件侦听和合适的处理。

多按钮的ActionEvent事件处理

处理方法

² 可用事件的getSource()方法获取触发事件的对象(名)

² 各个按钮的事件处理在同一个actionPerformed方法中完成

Object

source=e.getSource();

public void actionPerformed(ActionEvent

e){

Object source=e.getSource();

if(source==){}

else if (source==) {}

else if.

else

}

练习:完善上面程序,实现两个按钮的事件侦听处理。(作业)

练习:在以下代码的基础上,设计如图所示的事件处理。

import java.awt.*;

import javax.swing.*;

import java.io.*;

class CreateFile extends JFrame{

JTextArea t;

JButton

clear,save,exit;

CreateFile(){

super("Create text file");

this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

init();

Container c=this.getContentPane();

c.setLayout(new FlowLayout());

c.add(new JScrollPane(t));

c.add(save);

c.add(clear);

c.add(exit);

this.setBounds(300,200,400,300);

this.setVisible(true);

}

void init(){

t=new JTextArea(10,30);

t.setLineWrap(true);

clear=new JButton("Clear");

save=new JButton("Save");

exit=new JButton("Exit");

}

public static void

main(String[] aa){

new CreateFile();

}

}

说明:按钮事件是一个施加在组件上的动作,按钮事件的处理方法也适合于其他组件。比如文本框,回车时同样会发生ActionEvent事件。

思考

1. 按钮单击时发生什么事件?

2. 事件和用以事件侦听处理的接口在哪个包中?

3. 一个类如何实现接口?

4. 接口中的方法是需要考虑还是必须考虑重写?

5. 如何对一个对象进行事件侦听?对于ActionEvent事件,事件侦听方法是?

6. 可处理ActionEvent事件的接口是?

7. 描述ActionEvent事件处理过程的方法名是?

8. 若多个组件在侦听同一个事件,当事件发生时,怎么测出是哪个组件发生了事件?

事件及其相应的监听器接口

事件及支持事件的组件

事件类别

支持事件的组件

ActionEvent

JButton、JList、JTextField、JMenuItem及其派生类

MouseEvent

鼠标移动,Component及其派生类。

包括:JButton、JCheckBox、JComboBox、Container、JPanel、JLabel、JList、JTextArea和JTextArea等。

MouseEvent

鼠标点击,Component及其派生类

KeyEvent

键盘输入,Component及其派生类

ItemEvent

选择了某项,JCheckBox、JComboBox、JList等

TextEvent

TextField、TextArea等组件内容编辑

FocusEvent

收到或失去焦点,Component及其派生类

AdjustmentEvent

移动了滚动条等组件,实现Adjustable接口的类

ComponentEvent

对象移动缩放显示隐藏等,Component及其派生类

WindowEvent

窗口事件,Window及其派生类

ContainerEvent

容器增删组件,Container及其派生类

事件及其侦听器(方法)

事件类别

接口名和方法

ActionEvent

ActionListener

actionPerformed(ActionEvent e)

MouseEvent

MouseMotionListener

mouseDragged(MouseEvent

e)

mouseMoved(MouseEvent

e)

MouseEvent

MouseListener

mousePressed(MouseEvent

e)

mouseReleased(MouseEvent

e)

mouseEntered(MouseEvent

e)

mouseExited(MouseEvent

e)

mouseClicked(MouseEvent

e)

KeyEvent

KeyListener

keyPressed(KeyEvent

e)

keyReleased(KeyEvent

e)

keyTyped(KeyEvent

e)

ItemEvent

ItemListener

itemStateChanged(ItemEvent

e)

TextEvent

TextListener

textValueChanged(TextEvent

e)

FocusEvent

FocusListener

focusGained(FocusEvent

e)

focusLost(FocusEvent

e)

AdjustmentEvent

AdjustmentListener

adjustmentValueChanged(…)

ComponentEvent

ComponentListener

componentMoved(ComponentEvent e)

componentHidden(ComponentEvent e)

componentResized(ComponentEvent e)

componentShown(ComponentEvent e)

WindowEvent

WindowListener

windowClosing(WindowEvent

e)

windowOpened(WindowEvent

e)

windowIconified(WindowEvent e)

windowDeiconified(WindowEvent e)

windowClosed(WindowEvent

e)

windowActivated(WindowEvent e)

windowDeactivated(WindowEvent e)

ContainerEvent

ContainerListener

componentAdded(ContainerEvent e)

componentRemoved(ContainerEvent e)

事件处理示例

单种事件处理

练习1:文本框打字回声,如图所示。

设计分析

发生事件:KeyEvent

侦听器:KeyListener

KeyListener中的方法:

public void keyPressed(KeyEvent

e){}

public void keyReleased(KeyEvent

e){}

public void keyTyped(KeyEvent e)

{}

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

class EventTest1 extends JFrame

implements{

JTextField t1,t2;

EventTest1(){

super("KeyEvent Test");

setDefaultCloseOperation(EXIT_ON_CLOSE);

textInit();

Container c=this.getContentPane();

c.setLayout(new FlowLayout());

c.add(new

JLabel("打字回声测试"));

c.add(t1);c.add(t2);

this.setSize(250,120);

this.setVisible(true);

}

void textInit(){

t1=new JTextField(15);

t2=new JTextField(15);

//事件侦听

t1.addKeyListener(this);

t2.addKeyListener(this);

}

//请填充事件处理代码

public static void

main(String[] aa){

new EventTest1();

}

}

练习2:键盘操作按钮测试,每按一次“Enter”改变按钮文字,如图所示。

练习3:鼠标移动事件测试,如图所示显示鼠标在窗口上的位置。

设计分析

发生事件:MouseEvent

侦听器:MouseMotionListener、MouseListener

MouseMotionListener事件处理方法:

public void mouseDragged(MouseEvent

e){}

public void mouseMoved(MouseEvent

e){}

获取坐标点的方法:

getX()——返回鼠标当前列位置

getY()——返回鼠标当前行位置

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

class EventTest2 extends JFrame

implements

{

JTextArea t;

EventTest2(){

super("MouseEvent Test");

setDefaultCloseOperation(EXIT_ON_CLOSE);

t=new JTextArea(10,20);

Container c=this.getContentPane();

c.setLayout(new FlowLayout());

c.add(new JLabel("鼠标移动测试"));

c.add(new JScrollPane(t));

//事件侦听

this.setSize(300,300);

this.setVisible(true);

}

//请填充事件处理代码

public static void

main(String[] aa){

new EventTest2();

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值