Java ActionEvent e 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31216051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does ActionEvent e mean?
提问by MooseMan55
I am learning Java and would really like to have a deeper understanding of what the ActionEvent e perameter means and stands for. When I code I don't just want to spit out lines that work, but I don't understand. I want to have a full understanding of concepts before I use them.
我正在学习 Java,并且真的很想更深入地了解 ActionEvent e 参数的含义和代表的含义。当我编码时,我不只是想吐出有效的行,但我不明白。我想在使用它们之前对概念有一个完整的理解。
So what specifically is it asking for and what do the two parts(ActionEvent and e) mean?
那么它的具体要求是什么,两部分(ActionEvent 和 e)是什么意思?
class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
}
}
Thanks.
谢谢。
采纳答案by Eric Guan
ActionEvent
is a class, e
is an instance of that class. You can use e to call it's methods/properties, found here
ActionEvent
是一个类,e
是该类的一个实例。您可以使用 e 来调用它的方法/属性,在此处找到
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionEvent.html
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionEvent.html
ActionEvent
is just a type, it informs you what type of object e
is. By the way, you can change e
to whatever you prefer, for ex. event
or object
.
ActionEvent
只是一种类型,它会告诉您对象e
是什么类型。顺便说一句,您可以更改e
为您喜欢的任何内容,例如。event
或object
。
ActionEvent event
, ActionEvent object
(remember, not to be confused with Object
, its object with lower case "o"), ActionEvent anyVariableName
etc...
ActionEvent event
, ActionEvent object
(请记住,不要与Object
带有小写“o”的对象混淆)ActionEvent anyVariableName
等...
Then inside actionPerformed()
you can call stuff like event.doSomething();
然后在里面actionPerformed()
你可以调用类似的东西event.doSomething();
回答by InsidiarumCrassescit
The ActionEvent
is an "event" that your listener catches, as sent by a dispatcher. This mean, in layman's terms, that some thread somewhere has decided that your actions (i.e. clicking a button, etc.) have caused an action to occur, and informs the System. Your listener picks up on this, and takes a reference as the parameter e
. Thismay help to shed a bit more light on what/why the action is; and, it may be beneficial to check out the Event Dispatch Thread (EDT).
这ActionEvent
是您的侦听器捕获的“事件”,由调度程序发送。这意味着,用外行人的话说,某处的某个线程已决定您的操作(即单击按钮等)导致了某个操作的发生,并通知系统。您的听众会注意到这一点,并将参考作为参数e
。这可能有助于更清楚地了解行动是什么/为什么;并且,查看事件调度线程 ( EDT)可能会有所帮助。
回答by Fayaz
This should help you: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
这应该可以帮助您:http: //docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
Basically,ButtonListener is your ActionListener Implementation. You will use it like
基本上,ButtonListener 是您的 ActionListener 实现。你会像这样使用它
someButton1.addActionListener(new ButtonListener());
someButton2.addActionListener(new ButtonListener());
It will listen for any action events on the buttons 'someButton1' and 'someButton2'.But we might want to handle clicks on both buttons in a different way. Thats when ActionEvent is of use.
它将侦听按钮“someButton1”和“someButton2”上的任何动作事件。但我们可能希望以不同的方式处理这两个按钮上的点击。那就是使用 ActionEvent 的时候。
Inside method,we can do this by following
内部方法,我们可以通过以下方式做到这一点
@Override
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Button 1")){
//Process Button 1 action event here
}
else if(e.getActionCommand().equals("Button 2")){
//Process Button 2 action event here
}
}