event handling in java and the execution of actionPerformed method in java(java中的事件处理和java中actionPerformed方法的执行)
问题描述
我用java写了一个simpleGUI的小代码.
I have written a small code in java for simpleGUI.
package guidemo1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GuiDemo1 implements ActionListener{
JButton button;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GuiDemo1 gui=new GuiDemo1();
gui.go();
}
public void go()
{
JFrame frame=new JFrame();
button=new JButton();
frame.getContentPane().add(button);
button.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
button.setText("I've been clicked");
}
}
我是 JAVA 的新手.我有几个关于这个程序的问题.
I am newbie to JAVA.I have few questions related to this program.
有人能解释一下 actionPerformed 方法是如何在没有任何调用的情况下执行的吗?
Can some one explain how actionPerformed method gets executed with out any call?
在这里,我在 go() 方法中定义了本地框架对象,我们在 actionPerformed 中使用按钮,这是另一种方法.这怎么可能?按钮不是嵌入在框架上吗?
Here I have defined frame object locally to the go() method and we are using button in actionPerformed which is another method.How is that possible?Isn't the button gets embedded on the frame?
谢谢..
推荐答案
有人能解释一下 actionPerformed 方法是如何在没有任何调用的情况下执行的吗?
Can some one explain how actionPerformed method gets executed with out any call?
GUI 框架 Swing 在后台运行动作处理代码.每当按下按钮或用户以其他方式与 GUI 交互时,Swing 都会通过许多 Listener
接口之一通知您的应用程序.为了接收这些事件,您的类需要实现正确的 Listener
接口,并在它感兴趣的每个组件上注册为侦听器.
The GUI framework Swing runs action handling code in the background. Whenever a button is pressed or the user interacts with the GUI in some other way, Swing will notify your application through one of many Listener
interfaces. In order to receive these events, your class needs to implement the proper Listener
interface and be registered as a listener on each component it is interested in.
您的类实现了 ActionListener
接口并调用 addActionListener
将自身注册到按钮.当单击按钮时,Swing 将尝试通过调用它们的 actionPerformed
方法来通知所有已注册的 ActionListener
.魔法"就是这样发生的.
Your class implements the ActionListener
interface and calls addActionListener
to register itself to the button. When a button is clicked, Swing will attempt to notify all registered ActionListener
s by calling their actionPerformed
method. That's how the "magic" happens.
在这里,我在 go() 方法中定义了本地框架对象,我们在 actionPerformed 中使用按钮,这是另一种方法.这怎么可能?按钮不是嵌入在框架上吗?
Here I have defined frame object locally to the go() method and we are using button in actionPerformed which is another method.How is that possible?Isn't the button gets embedded on the frame?
您将按钮 添加
到框架的内容窗格中 - 这会将按钮放在框架内的 布局 中,而不是放在 代码 中.因为您通过将 JButton button;
放在任何方法之外来将 button
声明为实例变量,所以仍然可以从任何(非 static
)方法访问它在那个班级.
You add
the button to the frame's content pane - this puts the button inside the frame in your layout, not in your code. Because you declare button
as an instance variable by putting JButton button;
outside any method, it is still accessible from any (non-static
) method in that class.
这篇关于java中的事件处理和java中actionPerformed方法的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java中的事件处理和java中actionPerformed方法的执行
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01