java中的事件处理和java中的actionPerformed方法的执行 [英] event handling in java and the execution of actionPerformed method in java
问题描述
package guidemo1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GuiDemo1实现ActionListener {
JButton按钮;
/ **
* @param args命令行参数
* /
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(我已被点击);
}
}
我是JAVA的新手。
有人可以解释actionPerformed方法如何执行任何调用?
这里我在本地定义框架对象()方法,我们正在使用actionPerformed中的按钮,这是另一种方法。可能的是,这个按钮是不是嵌入在框架上?
谢谢..
有人可以解释actionPerformed方法如何被执行任何调用?
GUI框架Swing在后台运行动作处理代码。每当按下按钮或用户以其他方式与GUI进行交互时,Swing将通过许多 Listener
接口之一通知您的应用程序。为了接收这些事件,您的课堂需要实现正确的 Listener
接口,并在每个感兴趣的组件上注册为一个监听器。
您的类实现了 ActionListener
接口,并调用 addActionListener
将其注册到该按钮。当单击按钮时,Swing将尝试通过调用其 actionPerformed
方法来通知所有已注册的 ActionListener
。这就是魔法的发生。
这里我已经将本地定义的框架对象放在go()方法中,我们在actionPerformed中使用button这是另一种方法。可能的是,按钮是否嵌入在框架上?
您将
按钮添加到框架的内容窗格中 - 这将按钮放在布局中的框架中,而不是您的代码中。因为你通过在任何方法之外放置 JButton按钮
来声明按钮
作为实例变量,它仍然可以从任何非 static
)方法。
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");
}
}
I am newbie to JAVA.I have few questions related to this program.
Can some one explain how actionPerformed method gets executed with out any call?
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?
Thanks..
Can some one explain how actionPerformed method gets executed with out any call?
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.
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.
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?
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方法的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!