Java 入门图形用户界面设计之事件处理上
Java程序设计 图形用户界面 【七】事件处理上
事件处理
事件和监听器
事件:表示一个对象发生状态变化
监听器:不断地监听事件的变化,并根据这些事件进行相应地处理
事件类 EventObject类
EventObject类的定义格式
public class EventObject extends Object implements Serializable{ public EventObject(Object source){ // 构造一个发生事件的对象 } public Object getSource(){ // 返回一个事件对象 } public String toString(){ // 得到信息 } }
Java事件处理流程
窗体事件
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); } }
监听适配器
方便用户进行事件处理的实现。以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); } }
使用匿名内部类
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); } }
如果现在使用适配器操作类,则直接编写匿名内部类就可以减少监听类的定义
到此这篇关于Java 入门图形用户界面设计之事件处理上的文章就介绍到这了,更多相关Java 事件处理内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)