我已經在simpleGUI的java中編寫了一個小代碼。java中的事件處理和java中的actionPerformed方法的執行
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新手。我幾乎沒有問題涉及到這個程序。
有人可以解釋如何執行actionPerformed方法沒有任何調用?
這裏我已經在go()方法本地定義了框架對象,並且我們在actionPerformed中使用了按鈕,這是另一種方法。這是可能的嗎?按鈕是否嵌入到框架中?
謝謝..
作爲ActionListener的實現,actionPerformed()被覆蓋,所以任何操作都會在沒有任何調用的情況下進入actionPerformed()... – 2013-02-21 10:44:11