Java:将相同的 Action 监听器添加到多个组合框

标签 java callback actionlistener anonymous-types

我创建了一个 Action 监听器,用于监听 departingStop(组合框对象)是否有任何变化

departingStop.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent arg0) {
          //Lots of code here
     }
});

我还想将此 Action 监听器添加到另一个组合框 (finalStop),而不必像这样创建单独的监听器:

finalStop.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent arg0) {
          //Lots of code here
     }
});

如何实现?谢谢

最佳答案

您可以将监听器分配给一个变量...

ActionListener listener = new ActionListener() {
     public void actionPerformed(ActionEvent arg0) {
          //Lots of code here
     }
};

然后多次添加...

departingStop.addActionListener(listener);
finalStop.addActionListener(listener);

关于Java:将相同的 Action 监听器添加到多个组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820050/

相关文章:

java - 如何在 Java 中将 ActionListener 添加到框架中的按钮

java - 有没有办法为添加了鼠标适配器的东西设置操作命令?

iphone - 在静音模式下未调用 AudioServicesAddSystemSoundCompletion 回调

javascript - Phaser.io 加载时的 JSON 回调

iphone - 我的 foursquare 身份验证、callback_url 和应用程序网站是什么?

java - DAO方法中的事务回滚

java - 事件处理 Swing 组件

java - 读取jar中的文件

Java 无法在 JAR 中找到类

java - 有没有直接的方法来计算原始字段的大小?