为什么 ActionEvent 要求提供 id? [英] Why does ActionEvent ask for an id?
问题描述
在创建 new ActionEvent
时,您需要提供一个整数作为 id
.文档说这是:
When making a new ActionEvent
, you're required to provide an integer as an id
. The documentation says that this is:
标识事件的整数.有关允许值的信息,请参阅 ActionEvent
An integer that identifies the event. For information on allowable values, see the class description for
ActionEvent
ActionEvent 的类描述说:
And the class description for ActionEvent says:
如果任何特定 ActionEvent
实例的 id
参数不在 ACTION_FIRST
到 的范围内,将导致未指定的行为ACTION_LAST
.
An unspecified behavior will be caused if the
id
parameter of any particularActionEvent
instance is not in the range fromACTION_FIRST
toACTION_LAST
.
我的困惑来自于 ACTION_FIRST
和 ACTION_LAST
的值:
My confusion comes in with the values of ACTION_FIRST
and ACTION_LAST
:
/**
* The first number in the range of ids used for action events.
*/
public static final int ACTION_FIRST = 1001;
/**
* The last number in the range of ids used for action events.
*/
public static final int ACTION_LAST = 1001;
如果 id
必须始终是 1001
以免发生未指定的行为,那为什么还要要求它?
If id
must always be 1001
lest unspecified behavior occur, then why even require it?
推荐答案
ActionEvent
是 AWTEvent
的子类,它是 id 的所有者.ActionEvent
的构造函数只是将 id 传递给 super 的构造函数,然后 super (...) 检查它是否是以下之一:
ActionEvent
is a subclass of AWTEvent
, who is the owner of the id. The constructor for ActionEvent
simply passes the id to super's constructor, where super (...) then checks that it's one of:
- ActionEvent.ACTION_PERFORMED
- ItemEvent.ITEM_STATE_CHANGED
- AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED
- TextEvent.TEXT_VALUE_CHANGED
因此,ActionEvent
想要确保 super 只从他那里获得一个 ActionEvent.ACTION_PERFORMED
,其中 ACTION_PERFORMED 是一个设置为 ACTION_FIRST 的常量.
So, ActionEvent
wants to ensure that super only gets an ActionEvent.ACTION_PERFORMED
from him, where ACTION_PERFORMED is a constant set to ACTION_FIRST).
这篇关于为什么 ActionEvent 要求提供 id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!