9.2 AWT 事件处理

目录

事件理解

事件类

事件监听器

AWT事件以及相应的监听接口 

事件适配器


事件理解

Event  事件对象,用户对界面的操作

Event Source  事件源,事件发送的场所,通知指各组件

Event Handler 事件处理者,接受事件的对象并对事件处理的对象。或监听器。

点击一个Button,button就是事件源,程序运行时生产ActionEvent类的对象E,E就是Event事件对象,而监听接受并处理对象E的对象就是Event Handler。

授权处理机制Delegation Model ,事件源可能发生多种事件,把其可能发生的所有事件分别授权给不同的事件处理者(Event Handler)来处理。

事件处理者也叫监听器,一旦事件源发生和当前监听器所负责的事情,监听器就马上处理该事件。

import java.awt.*;
import java.awt.event.*;


public class DemoTest {
    public static void main(String[] args) {
        Frame fr = new Frame("test");
        Button button = new Button("laisa ");
        button.addActionListener(new ButtonHand());

        fr.setLayout(new FlowLayout());
        fr.add(button);
        fr.setSize(500,500);
        fr.setVisible(true);
    }

}

class ButtonHand implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("hahah ");
    }
}

实现关闭功能

package Awt01;
import java.awt.*;
import java.awt.event.*;
public class ATest {
    public static void main(String[] args) {
        Frame fr = new Frame("HELLO");
//        fr.setLayout();
        fr.setSize(500,600);

        Button test = new Button("Exit");
        test.setBackground(Color.PINK);
        test.addActionListener(new ATest().new ButtonAction());

        Button test2 = new Button("say Hello!");
        test2.setBackground(Color.DARK_GRAY);
        test2.addActionListener(new ATest().new BL());

        Panel p1 = new Panel();
        Panel p3 = new Panel();
        TextArea ta = new TextArea();
        ta.setBackground(Color.orange);
        p3.add(ta);

        p1.add(test);
        p1.add(test2);
        fr.add("North",p3);
        fr.add("Center",p1);

        fr.setVisible(true);
    }
    class ButtonAction implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }


    }
    class BL implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.out.println("hahaha");
        }
    }
}


事件类

顶级父类 java.util.EventObject;并且实现了串行化接口,getSource() 可以获得事件源对象。

与AWT所有事件类相关的都由java.awt.AWTEvent 类派生的;

getSource获取事件源对象

AWT分为:低级事件和高级事件

低级事件是基于容器和组件的事件,鼠标点击,点击,拖放等,组件窗口开开关;

ComponentEvent, ContainerEvent,WindowEvent,FocusEvent,MouseEvent

高级事件是基于语义的事件,依赖于触发此事件的类。如ActionEvent,

ActionEvent,AdjustmentEvent,ComponentEvent,ItemEvent,TextEvent


事件监听器

每类事件对应有相应的监听器。

public void add<ListenerType>(ListenerType listener);//添加监听器

public void remove<ListenerType>(ListenerType listener);//注销监听器

package Awt01;
import java.awt.*;
import java.awt.event.*;

public class ListenerTest {
    private Frame f;
    private Button b;

    public void toCreateUi(){
        f = new Frame("Test");
        b = new Button("Click here!!");
        b.addActionListener(new ButtonShow());
        f.addWindowListener(new AddListener());
        f.add(b,"North");
        f.setSize(600,800);
        f.setVisible(true);
    }
    public static void main(String[] args) {
        ListenerTest listenerTest = new ListenerTest();
        listenerTest.toCreateUi();
    }
}
class ButtonShow implements ActionListener{
    public void actionPerformed(ActionEvent e){
        System.out.println("Hello!!");
    }
}


class AddListener implements WindowListener{

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.exit(1);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}

AWT事件以及相应的监听接口 

事件类型接口名    方法               说明                                        
ActionEventActionListeneractionPormed(ActionEvent e)激活组件
ItemEventItemListeneritemStateChanged(ItemEvent)选择某些项目
MouseEventMouseListenermousePressed(MouseEvent)鼠标按住操作
mouseReleased(MouseEvent)鼠标释放操作
mouseEntered(MouseEvent)鼠标悬浮操作//进入组件
mouseExited(MouseEvent)鼠标离开组件操作
MouseMotionListenermouseDragged(MouseEvent)鼠标按住
mouseMoved(MouseEvent)鼠标移动
KeyEventKeyListenerkeyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTyped(KeyEvent)键盘输入内容
FocusEventFocusListenerfocusGained(FocusEvent)
foucsLost(FocusEvent)
AdjustmentEventAdjustmentListeneradjustmentValueChanged(AdjustmentEvent)
ComponentEventComponentListenercomponentMoved(ComponentEvent)组件移动
componentHidden(ComponentEvent)组件隐藏
componentResized(ComponentEvent)组件大小设置
componentShown(ComponentEvent)组件展示
WindowEventWindowListenerwindowClosing(WindowEvent)关闭窗口
windowOpend(WindowEvent)打开窗口后
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
windowClosed(WindowEvent)窗口关闭后动作
windowActived(WindowEvent)窗口激活
windowDeactived(WindowEvent) 
ContainerEventContainerListenercomponentAdded(containerEvent)
componentRemoved(containerEvent)
TextEventTextListenertextValueChanged(TextEvent)

 

                                                            

package TestAWT;

import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;

public class MyFrame implements WindowListener, MouseListener,KeyListener{
        public  Frame frame;
        public  Button helloTest;
        public  TextField showPage,movePageOrigin,movePageDestination;

        public void mainPage(){
            frame = new Frame("Test");
            frame.setSize(500,600);
            frame.setVisible(true);

            helloTest = new Button("Do it");
            frame.add(helloTest,"North");

            showPage = new TextField(30);
            frame.add(showPage,BorderLayout.CENTER);

            movePageOrigin = new TextField("CH",30);
            movePageOrigin.setSize(100,100);
            frame.add(movePageOrigin,BorderLayout.WEST);

            movePageDestination = new TextField("Us",30);
            movePageDestination.setSize(100,100);
            frame.add(movePageDestination,BorderLayout.EAST);

            helloTest.addMouseListener(this);
            frame.addWindowListener(this);
            movePageOrigin.addKeyListener(this);
            movePageDestination.addKeyListener(this);
        }

    @Override
    public void mouseClicked(MouseEvent e) {
            showPage.setText("hahahah");

    }

    @Override
    public void mousePressed(MouseEvent e) {
            showPage.setText("ni shi hapimai");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
            showPage.setText("Released!!");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
            showPage.setText("Clicked me!");
    }

    @Override
    public void mouseExited(MouseEvent e) {
            showPage.setText("Realseme");
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
            System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {
        Scanner in = new Scanner(System.in);
        movePageOrigin.setText(in.nextLine());
    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
package TestAWT;

public class MainTest {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
        myFrame.mainPage();
    }

}


事件适配器

java为一些监听器提供了适配器Aapter,可以继承事件锁对应的Aapter重写需要的方法,对不相关的事宜不用实现。多种监听或该类有父类的事件无法使用适配器

1.使用内部类实现

package AdapterTest;
import java.awt.*;
import java.awt.event.*;

public class EventAdapter {
    private Frame f;
    private TextField test,east;

    public void toCreate(){
        f = new Frame("DDDDD");
        test = new TextField(50);
        east = new TextField(30);
        Label l = new Label("Click and drug here!");
        f.add(l,BorderLayout.NORTH);
        f.add(test,"South");

        f.add(east,"East");
        f.addMouseMotionListener(new TestMouseMotionListener());
        f.addWindowListener(new TestWindowListener());
        f.addMouseMotionListener(new TestMouseMotionListener2());
        f.setSize(600,600);
        f.setVisible(true);
    }

    class TestMouseMotionListener extends MouseMotionAdapter{
        public void mouseDragged(MouseEvent e){
            test.setText("x:"+e.getX()+"y:"+e.getY());
        }
    }
    class TestMouseMotionListener2 extends MouseMotionAdapter{
        public void mouseMoved(MouseEvent e){
            east.setText("east:"+"x:"+e.getX()+"y:"+e.getY());
        }
    }
    class TestWindowListener extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        EventAdapter e = new EventAdapter();
        e.toCreate();
    }
}

2.使用匿名类实现

当一个类的对象在创建时只使用一次

package AdapterTest;
import java.awt.*;
import java.awt.event.*;

public class EventAdapter {
    private Frame f;
    private TextField test,east;

    public void toCreate(){
        f = new Frame("DDDDD");
        test = new TextField(50);
        east = new TextField(30);
        Label l = new Label("Click and drug here!");
        f.add(l,BorderLayout.NORTH);
        f.add(test,"South");

        f.add(east,"East");
        f.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                test.setText("x:"+e.getX()+"y:"+e.getY());
            }
        });
        f.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(1);
            }
        });
        f.addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseMoved(MouseEvent e){
                east.setText("east:"+"x:"+e.getX()+"y:"+e.getY());
            }
        });
        f.setSize(600,600);
        f.setVisible(true);
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值