问题
When making a new ActionEvent
, you're required to provide an integer as an id
. The documentation says that this is:
An integer that identifies the event. For information on allowable values, see the class description for
ActionEvent
And the class description for ActionEvent says:
An unspecified behavior will be caused if the
id
parameter of any particularActionEvent
instance is not in the range fromACTION_FIRST
toACTION_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;
If id
must always be 1001
lest unspecified behavior occur, then why even require it?
回答1:
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
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).
来源:https://stackoverflow.com/questions/25897234/why-does-actionevent-ask-for-an-id