java JFrame 动作监听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6344269/
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
JFrame ActionListener
提问by kebyang
JFrame myframe = new JFrame("My Sample Frame");
JButton mybutton = new JButton("Okay");
Can someone explain to me these part.
有人可以向我解释这些部分。
mybutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
//Assuming that the content here will do something.
}
回答by Jesper
What exactly do you not understand about the code?
你到底有什么不明白的代码?
The code adds an action listener to the button. The actionPerformed
method of the action listener will be called when the button is clicked.
该代码向按钮添加了一个动作侦听器。actionPerformed
单击按钮时将调用动作侦听器的方法。
Note that an anonymous inner classis used here.
注意这里使用了匿名内部类。
回答by oliholz
You should read this Tutorial about writing Event Listeners.
您应该阅读有关编写事件侦听器的教程。
回答by Suhail Gupta
Anonymous Inner Class is used here.
这里使用匿名内部类。
You have technically implemented ActionListener. When you called addActionListener:
您已经在技术上实现了 ActionListener。当您调用 addActionListener 时:
mybutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
//Assuming that the content here will do something.
}
You created an instance of an anonymous class, or a class that implements ActionListener without a name.
您创建了匿名类的实例,或实现了没有名称的 ActionListener 的类。
For the same please visit this link .
对于相同的,请访问此链接。
回答by Jason S
In order to have a button react to events (such as a click) it must have an ActionListener
.
为了让按钮对事件(例如点击)做出反应,它必须有一个ActionListener
.
In the code you posted, you are creating an anonymous class implementing ActionListener
在您发布的代码中,您正在创建一个匿名类实现 ActionListener
public void mySetupFunction(){
mybutton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
//Do something when the button is clicked
});
}
is the same as doing :
和做一样:
public void mySetupFunction(){
mybutton.addActionListener(new MyEventHandler());
}
with :
和 :
public class MyEventHandler implements ActionListener{
public void actionPerformed(ActionEvent evt){
//Do something when the button is clicked
}
}
回答by JustMuddles
If it is safe to assume, you are either new to Event handling or new to Java either way I have generalised the answer to accomodate both cases :)
如果可以安全地假设,无论哪种方式,您要么是事件处理的新手,要么是 Java 的新手,我已经概括了适应这两种情况的答案:)
Expert Answer:ActionListener
专家解答:ActionListener
There isn't one, it's really self led and reading the APIs
没有,它真的是自我引导和阅读 API
Simple Answer:A sugar coated explanation of what You provided in your code.
简单答案:对您在代码中提供的内容的糖衣解释。
mybutton
is a reference to the instanciated Object JButton
是对实例化对象JButton的引用
xxxxxx.addActionListener()
is a function call to a Field inherited from class javax.swing.AbstractButton
是对从类 javax.swing.AbstractButton 继承的 Field 的函数调用
JButton inherits AbstractButtonfields and therefore takes us to the next explanation
JButton 继承了AbstractButton字段,因此带我们进入下一个解释
new ActionListener(){}
ActionListeneris an interface, we don't need to go this high up at all.. all we need to know is it listens for input to source from a given object
ActionListener是一个接口,我们根本不需要这么高......我们需要知道的是它监听来自给定对象的输入源
public void actionPerformed(ActionEvent e)
again this is a function call, it is the tip to our "Objects pyramid" and not atypically - will contain the response to what you want the base of your object to do .. in this case mybutton.
这又是一个函数调用,它是我们“对象金字塔”的提示,而不是非典型的 - 将包含对您希望对象的基础做什么的响应......在这种情况下是 mybutton.
What you do from here is dependent on what you want actionPerformed() to do.
你从这里做什么取决于你想要 actionPerformed() 做什么。
The question to ask yourself if this is part of an assignment is - What does this do? How does it Do it?
问问自己这是否是作业的一部分的问题是 - 这有什么作用?它是如何做到的?