一、事件监听机制
-- 事件源:awt 或swing包中的那些图形界面组件,即发生事件的组件
-- 事件:Event 用户对组件的一个操作
-- 监听器:Listener 负责处理事件的方法
二、java.awt.event 包下的类
WindowEvent //窗口事件,比如用户点了半闭窗口,窗口得到或失去焦点,最大化最小化等
MouseEvent //鼠标事件,鼠标按下,鼠标释放,点击(按下后再松开)等
ActionEvent //动作事件,它不是代表一个具体动作,而是一种语义,比如按纽,或菜单被点击,在文本框中按下回车等,可以这样理解:用户的某一动作导致了某个组件本身的基本作用发生了,这就是ActionEvent事件
不同的事件类型,对应着不同的事件监听器接口,接口的名称和事件的名称是相对应的。
WindowEvent - >WindowListener
MouseEvent ->MouseListener
ActionEvent ->ActionListener
代码示例:
import java.awt.Frame;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;public class Test20 {public static void main(String[] args) { Frame f = new Frame(); f.setSize(400, 400); f.setVisible(true); f.addWindowListener(new WindowListener() { @Overridepublic void windowOpened(WindowEvent e) { // 窗口被打开// TODO Auto-generated method stub} @Overridepublic void windowClosing(WindowEvent e) { // 设置关闭事件// TODO Auto-generated method stubSystem.exit(0); } @Overridepublic void windowClosed(WindowEvent e) { // 用户已经关闭窗口// TODO Auto-generated method stub} @Overridepublic void windowIconified(WindowEvent e) { // 被最小化的时候// TODO Auto-generated method stub} @Overridepublic void windowDeiconified(WindowEvent e) { // 最小化被还原的时候// TODO Auto-generated method stub} @Overridepublic void windowActivated(WindowEvent e) { // 窗体被激活// TODO Auto-generated method stub} @Overridepublic void windowDeactivated(WindowEvent e) { // 失去焦点的时候// TODO Auto-generated method stub} }); } }
有没有发现,用WindowListener接口的时候,会引入一大堆不常用的代码(这里我们只想设置关闭),借口里的方法只能被覆盖,又不能删掉,这样就是的整个项目显得很罗嗦,为了解决这个事情,就有了事件适配器。
三、事件适配器
JDK 针对大多数事件监听器接口类定义了相应的实现类(里面有很多空实现的方法,方便我们创建侦听器对象),我们称为事件适配器类。这里我用到了WindowAdapter。
import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Test21 {public static void main(String[] args) { Frame f = new Frame("事件适配器的栗子"); f.setSize(400, 400); f.setVisible(true); f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
可以观察一下WindowAdapter类的源码,找找感觉。
public abstract class WindowAdapterimplements WindowListener, WindowStateListener, WindowFocusListener {/** * Invoked when a window has been opened. */public void windowOpened(WindowEvent e) {}/** * Invoked when a window is in the process of being closed. * The close operation can be overridden at this point. */public void windowClosing(WindowEvent e) {}/** * Invoked when a window has been closed. */public void windowClosed(WindowEvent e) {}/** * Invoked when a window is iconified. */public void windowIconified(WindowEvent e) {}/** * Invoked when a window is de-iconified. */public void windowDeiconified(WindowEvent e) {}/** * Invoked when a window is activated. */public void windowActivated(WindowEvent e) {}/** * Invoked when a window is de-activated. */public void windowDeactivated(WindowEvent e) {}/** * Invoked when a window state is changed. * @since 1.4 */public void windowStateChanged(WindowEvent e) {}/** * Invoked when the Window is set to be the focused Window, which means * that the Window, or one of its subcomponents, will receive keyboard * events. * * @since 1.4 */public void windowGainedFocus(WindowEvent e) {}/** * Invoked when the Window is no longer the focused Window, which means * that keyboard events will no longer be delivered to the Window or any of * its subcomponents. * * @since 1.4 */public void windowLostFocus(WindowEvent e) {} }
练习几个个事件处理程序的栗子吧。
例一:
import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;//例一:在窗体中放置一个按纽,点击后让程序退出class TestFrame implements ActionListener { // ActionListener接口里面只有一个方法,下面会重写private Frame f;public TestFrame() { f = new Frame("窗口"); init(); }private void init() { f.setSize(300, 300); f.setLayout(new FlowLayout());// 布局模式Button b = new Button("退出程序"); b.addActionListener(this); f.add(b); f.setVisible(true); } @Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubf.setVisible(false); f.dispose();// 在关闭的时候,可以用它来销毁窗体资源System.exit(0);// 退出 } }public class Test22 {public static void main(String[] args) {new TestFrame(); } }
上面的,点击退出程序按钮才可以退出,点击右上角的X,是不可以退出的哦。因为没有设置WindowListener哦。
这个例子用到ActionListener接口,可以看一下它的源代码,如下:
public interface ActionListener extends EventListener {/** * Invoked when an action occurs. */public void actionPerformed(ActionEvent e); }
例二:
TextField txtNo;= Frame("请输入密码"f.setBounds(50, 50, 400, 400);f.setLayout( FlowLayout());= TextField(10); code = (!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9)) {System.out.println(KeyEvent.getKeyText(code) + "输入有误" 0
测试类:
public class Test23 {public static void main(String[] args) {new TestFrame(); } }
例三:
列出指定目录的内容:
import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;public class Test24 {public static void main(String[] args) {new MyWindow(); } }class MyWindow { MyWindow() { init(); }private Frame f;private Button b;private TextField txtDir;// 用来输入目录名称private TextArea txtFileList;// 用来显示文件列表private void init() { f = new Frame("窗口"); f.setBounds(44, 44, 500, 500); f.setLayout(new FlowLayout()); txtDir = new TextField(8); b = new Button("显示"); txtFileList = new TextArea(20, 30);// 用来显示文件列表的区域f.add(txtDir); f.add(b); f.add(txtFileList); initEvent(); f.setVisible(true); }private void initEvent() {// TODO Auto-generated method stubf.addWindowListener(new WindowAdapter() {/** * Invoked when a window is in the process of being closed. The * close operation can be overridden at this point. */public void windowClosing(WindowEvent e) { System.exit(0); } }); b.addActionListener(new ActionListener() {/** * Invoked when an action occurs. */public void actionPerformed(ActionEvent e) { txtFileList.setText(""); String dirStr = txtDir.getText();// 取出用户输入的路径File file = new File(dirStr);if (file.isDirectory() && file.exists()) { String[] fileNameList = file.list();for (String s : fileNameList) { txtFileList.append(s + "\r\n");// 别忘了换行符 } } else { txtFileList.append("输入有误,请重新输入"); } } }); } }
结果:(我让它显示我D盘的目录)
Atas ialah kandungan terperinci Java中GUI编程讲解(下). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!