在this Swing 示例代码我遇到了一个setActionCommand 方法。 Java 的引用仅说明它“为此组件设置操作命令”。什么是 Action 命令,为什么需要设置它?
最佳答案
事实上,JButton
将指定的 Action 命令重定向到ButtonModel
。这是转发到 ButtonModel
的方法,带有注释。
/**
* 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()
以区分单击了哪个按钮。
关于java - setActionCommand 设置的 Action 命令是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27916896/