java - setActionCommand 设置的 Action 命令是什么?

标签 java swing awt actionlistener

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/

相关文章:

java - 使用 ANT 设置代理

java - Qt Jambi 死了吗?

java - 使 JFrame 标题栏不计入像素

java - 如何在 switch 语句中执行 actionPerformed?

java - MySQL查询抛出异常,而不是一个空的ResultSet

java - Tomcat 和 HSQL java.lang.ClassNotFoundException : org. hsqldb.jdbcDriver

java - 如何让嵌入式mongodb在应用程序关闭时保留数据?

java - 在 JPanel 中调整 ImageIcon 的大小,其中从文件夹中检索文件

java - 尝试 Java 乒乓球

java - 获取异常 : java. lang.IllegalArgumentException:无法添加到布局:约束必须是字符串(或 null)