亲宝软件园·资讯

展开

Java 事件处理

小旺不正经 人气:0

Java程序设计 图形用户界面 【七】事件处理上

事件处理

事件和监听器

事件:表示一个对象发生状态变化

监听器:不断地监听事件的变化,并根据这些事件进行相应地处理

事件类 EventObject类

EventObject类的定义格式

public class EventObject extends Object implements Serializable{
	public EventObject(Object source){
	// 构造一个发生事件的对象
	}
	public Object getSource(){
	// 返回一个事件对象
	}
	public String toString(){
	// 得到信息
	}
}

Java事件处理流程

image-20220211152021277

窗体事件

WindowListener是专门处理窗体事件的监听接口

方法 作用
void windowActivated(WindowEvent e) 将窗口变为活动窗口时触发
void windowDeactivated(WindowEvent e) 将窗口变为不活动窗口时触发
void windowClosed(WindowEvent e) 当窗口被关闭时触发
void windowClosing(WindowEvent e) 当窗口正在关闭时触发
void windowIconified(WindowEvent e) 窗口最小化时触发
void windowDeiconified(WindowEvent e) 窗口从最小化恢复到正常状态时触发
void windowOpened(WindowEvent e) 窗口打开时触发

实现WindowListener

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class WindowEventHandle implements WindowListener {
    @Override
    public void windowActivated(WindowEvent e) {
        System.out.println("窗口被选中");
    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.out.println("窗口被关闭");
    }

    @Override
    public void windowClosing(WindowEvent e) {
        System.out.println("窗口关闭");
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        System.out.println("取消窗口选中");
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        System.out.println("窗口从最小化恢复");
    }

    @Override
    public void windowIconified(WindowEvent e) {
        System.out.println("窗口最小化");
    }

    @Override
    public void windowOpened(WindowEvent e) {
        System.out.println("窗口被打开");
    }
}

在窗体上注册事件监听器

import javax.swing.*;
import java.awt.*;

public class Hello {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一");
        frame.addWindowListener(new WindowEventHandle());
        frame.setSize(500,300);
        frame.setBackground(Color.BLUE);
        frame.setLocation(500,500);
        frame.setVisible(true);
    }
}

image-20220211153339270

监听适配器

方便用户进行事件处理的实现。以WindowAdapter为例,用户只要继承了此类,就可以根据自己的需要覆写方法,如果现在只关心窗口关闭方法,则只在子类中覆写windowClosing()方法即可

通过WindowAdapter实现监听

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class WindowEventHandle extends WindowAdapter {
    @Override
    public void windowClosing(WindowEvent e) {
        super.windowClosing(e);
        System.out.println("窗口关闭");
        System.exit(1);
    }
}

而在窗体操作的代码中,直接使用以上的监听器类即可

import javax.swing.*;
import java.awt.*;

public class Hello {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一");
        frame.addWindowListener(new WindowEventHandle());
        frame.setSize(500,300);
        frame.setBackground(Color.BLUE);
        frame.setLocation(500,500);
        frame.setVisible(true);
    }
}

image-20220211160600148

使用匿名内部类

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;

public class Hello {
    public static void main(String[] args) {
        JFrame frame = new JFrame("一");
        frame.addWindowListener(new WindowEventHandle(){
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
        frame.setSize(500,300);
        frame.setBackground(Color.BLUE);
        frame.setLocation(500,500);
        frame.setVisible(true);
    }
}

如果现在使用适配器操作类,则直接编写匿名内部类就可以减少监听类的定义

加载全部内容

相关教程
猜你喜欢
用户评论