Why does ActionEvent ask for an id?

混江龙づ霸主 提交于 2019-12-11 04:28:20

问题


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 particular ActionEvent instance is not in the range from ACTION_FIRST to 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;

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!