Swing自定义事件

1.Swing自定义事件-将一个组件的事件传递给另一个组件.

使用EventListenerList来管理事件,当A组件触发事件的时候,调用方法fireActionPerformed()来触发事件,然后再B组件中 actionPerformed()方法来接收事件.

当在容器KeyTextComponent中按下鼠标,我们就可以在Jframe中捕获触发的事件.

Managing Listener Lists with EventListenerList

1import java.awt.event.ActionEvent; 2import java.awt.event.ActionListener; 3import java.awt.event.KeyAdapter; 4import java.awt.event.KeyEvent; 5import java.awt.event.KeyListener; 6import java.awt.event.MouseAdapter; 7import java.awt.event.MouseEvent; 8import java.awt.event.MouseListener; 9import java.util.EventListener; 10 11import javax.swing.JComponent; 12import javax.swing.JFrame; 13import javax.swing.JTextField; 14import javax.swing.event.EventListenerList; 15 16class KeyTextComponent extends JComponent { 17 private EventListenerList actionListenerList = new EventListenerList(); 18 19 public KeyTextComponent() { 20 KeyListener internalKeyListener = new KeyAdapter() { 21 public void keyPressed(KeyEvent keyEvent) { 22 System.out.println("keyPressed"); 23 if (actionListenerList != null) { 24 int keyCode = keyEvent.getKeyCode(); 25 String keyText = KeyEvent.getKeyText(keyCode); 26 ActionEvent actionEvent = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, keyText); 27 fireActionPerformed(actionEvent);//触发事件 28 } 29 } 30 }; 31 MouseListener internalMouseListener = new MouseAdapter() { 32 public void mousePressed(MouseEvent mouseEvent) { 33 requestFocusInWindow(); 34 } 35 }; 36 37 addKeyListener(internalKeyListener); 38 addMouseListener(internalMouseListener); 39 } 40 41 public void addActionListener(ActionListener actionListener) {//事件管理 42 actionListenerList.add(ActionListener.class, actionListener); 43 } 44 45 public void removeActionListener(ActionListener actionListener) { 46 actionListenerList.remove(ActionListener.class, actionListener); 47 } 48 49 protected void fireActionPerformed(ActionEvent actionEvent) {//触发事件 50 EventListener listenerList[] = actionListenerList.getListeners(ActionListener.class); 51 for (int i = 0, n = listenerList.length; i < n; i++) { 52 ((ActionListener) listenerList[i]).actionPerformed(actionEvent); 53 } 54 } 55 56 public boolean isFocusable() { 57 return true; 58 } 59} 60 61public class KeyTextComponentDemo { 62 public static void main(String[] args) { 63 JFrame aWindow = new JFrame(); 64 aWindow.setBounds(30, 30, 300, 300); // Size 65 aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 66 67 KeyTextComponent com = new KeyTextComponent(); 68 aWindow.add(com, "Center"); 69 com.addActionListener(new ActionListener() { 70 71 public void actionPerformed(ActionEvent e) { 72 System.out.println("action");//获取事件 73 74 } 75 76 }); 77 78 aWindow.add(new JTextField(), "South"); 79 80 aWindow.setVisible(true); // Display the window 81 } 82}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

tomcat 学习笔记之生命周期

1、Catalina包含许多组件Catalina启动关闭时,这些组件一起启动关闭实现org.apache.catalina.LifeCycle接口,可以达到统一启动/关闭这些组件2、实现了LifeCycle接口的组件会触发如下事件事件是org.apache.catalina.LifeCycleEvent类的实例

JavaScript中常用事件

原文链接:鼠标事件鼠标单击事件click在文档中鼠标进行单击,就会触发事件。javascriptvari0document.addEventListener('click',function()console.log(i))鼠标双击事件dblclick

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )