首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >setActionCommand设置的操作命令是什么?

setActionCommand设置的操作命令是什么?
EN

Stack Overflow用户
提问于 2015-01-13 07:26:12
回答 1查看 4.6K关注 0票数 3

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-13 07:30:09

实际上,JButton将指定的操作命令重定向到ButtonModel。以下是转发给ButtonModel的带有注释的方法。

代码语言:javascript
复制
   /**
     * 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()以区分单击了哪个按钮。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27916896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档