什么是更好用:Action vs ActionListener? [英] What is better to use: Action vs ActionListener?

查看:204
本文介绍了什么是更好用:Action vs ActionListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己,我总是使用 ActionListener 作为swing事件处理程序(例如按钮单击),它是我在大多数swing应用程序中看到的最常见的监听器。

Myself, I always use ActionListener as swing event-handler (e.g. button-click), and it is the most common listener I've seen in most swing applications.

但是,stackoverflow中的一些Swing专业人员经常建议使用 行动 而不是 ActionListener 。我从中获得了什么好处?

However, Some Swing professionals here in stackoverflow often advise to use Action rather than ActionListener. What benefits I get from doing so?

推荐答案

首选行动如果您需要跨组件共享功能。来自 docs

An Action is preferred if you need to share functionality across components. From the docs


如果您有两个或多个执行相同功能的组件,请考虑使用Action对象来实现该功能。

if you have two or more components that perform the same function, consider using an Action object to implement the function.

但也说


行动object是一个动作侦听器,它不仅提供动作事件处理,还集中处理动作 - 事件触发组件的状态,例如工具栏按钮,菜单项,公共按钮和文本字段。操作可以处理的状态包括文本,图标,助记符,已启用和已选择状态。

An Action object is an action listener that provides not only action-event handling, but also centralized handling of the state of action-event-firing components such as tool bar buttons, menu items, common buttons, and text fields. The state that an action can handle includes text, icon, mnemonic, enabled, and selected status.

这篇关于什么是更好用:Action vs ActionListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆