java getlabel_在java中e.getActionCommand()和e.getLabel()是什么意思?有什么区别?还有e.equal()和e.getSource()???...

展开全部

我用eclipse查java源码给你解释一32313133353236313431303231363533e59b9ee7ad9431333365653230下这个问题。e是ActionEvent的一个引用的前提下进行解释。

java源码中关于getActionCommand的定义如下(在eclipse中按住Ctrl左键点击方法名):/**

* Returns the command string associated with this action.

* This string allows a "modal" component to specify one of several

* commands, depending on its state. For example, a single button might

* toggle between "show details" and "hide details". The source object

* and the event would be the same in each case, but the command string

* would identify the intended action.

* Note that if a null command string was passed

* to the constructor for this ActionEvent, this

* this method returns null.

*

* @return the string identifying the command for this event

*/

public String getActionCommand() {

return actionCommand;

}

该方法直接返回一个actionCommand属性。直接查看该属性源码如下:/**

* The nonlocalized string that gives more details

* of what actually caused the event.

* This information is very specific to the component

* that fired it.

* @serial

* @see #getActionCommand

*/

String actionCommand;

java源码对此的解释是,这个属性是ActionEvent中的一个属性,它详细的描述了具体是哪一个组件引发了该次事件。比如是button,这个按钮上显示的是“提交”,那最后你通过e.getActionCommand得到的就是“提交”。

对于你说的e.getLabel,e是一个ActionEvent,ActionEvent及其父类以及其实现的接口,没有getLabel这个方法。你想表达的应该是e.getSource.getLabel吧。如果是,那么在下面我给你说一下他俩的区别。

e.getSource是ActionEvent的方法,该方法返回一个对象的引用,该引用指向触发时间的对象。简单来说,可以这么理解,getSource间接地得到了这个button的一切,但你还不知道它到底是那个button,需要用button的方法——getLabel来看一下它到底是哪个button。就好像你踢足球踢碎了学校的玻璃,安保主任捡到了写着你的学号的足球(假定每个人的足球上都有每个人的学号),主任拿到了你的学号,就间接地得到了你的一切信息(年级,姓名,班级,班主任,父母等)相当于e.getSource得到了button的引用,但只有学号,并不能确定你,需要去教务处查一下,这个过程相当于button.getLabel,得到你的姓名。纯手打,请采纳。到了打字上限不能在说了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值