問題描述
我用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方法的執行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!