Java setActionCommand设置的动作命令是什么?

Java setActionCommand设置的动作命令是什么?,java,swing,awt,actionlistener,Java,Swing,Awt,Actionlistener,在Swing示例代码中,我遇到了setActionCommand方法。Java的引用仅说明它“为该组件设置操作命令”。什么是操作命令?为什么需要设置它?事实上,JButton将指定的操作命令重定向到按钮模型。下面是转发到按钮模型的方法,带有注释 /** * Sets the action command string that gets sent as part of the * <code>ActionEvent</code> when the

在Swing示例代码中,我遇到了setActionCommand方法。Java的引用仅说明它“为该组件设置操作命令”。什么是操作命令?为什么需要设置它?

事实上,
JButton
将指定的操作命令重定向到
按钮模型。下面是转发到
按钮模型的方法,带有注释

   /**
     * Sets the action command string that gets sent as part of the
     * <code>ActionEvent</code> when the button is triggered.
     *
     * @param s the <code>String</code> that identifies the generated event
     * @see #getActionCommand
     * @see java.awt.event.ActionEvent#getActionCommand
     */
    public void setActionCommand(String s)
因此,在您的
ActionListener
中,当您获得
ActionEvent
时,您可以检查
getActionCommand()
以区分单击了哪个按钮