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.
相关文章