在这 Swing示例代码中,我遇到了一个setActionCommand方法。Java的引用只说明它“为该组件设置操作命令”。什么是操作命令,为什么我需要设置它?
发布于 2015-01-13 07:30:09
实际上,JButton将指定的操作命令重定向到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()以区分单击了哪个按钮。
https://stackoverflow.com/questions/27916896
复制相似问题