比如说 按纽的事件,同一个JFrame里可能有多个按钮的事件,为了避免冲突,给每个按钮设置不同的ActionCommand,在监听时间的时候,用这个做条件区分事件,以做不同的响应
追问
他与getSource有什么区别
回答
getSource() Returns: The object on which the Event initially occurred. 依赖于事件对象 getActionCommand() Returns the command name of the action event fired by this button. If the command name is null (default) then this method returns the label of the button. 依赖于按钮上的字符串 getSource得到的组件的名称,而getActionCommand得到的是标签。 如:Button bt=new Button("buttons"); 用getSource得到的是bt 而用getActionCommand得到的是:buttons e.getSource() 返回的当前动作所指向的对象,包含对象的所有信息 e.getActionCommand() 返回的是当前动作指向对象的名称