我在java中为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的新手.我对此计划几乎没有任何疑问.
有人可以解释一下如何在没有任何调用的情况下执行actionPerformed方法吗?
在这里我已经在go()方法本地定义了框架对象,我们在actionPerformed中使用了按钮,这是另一种方法.怎么可能?按钮是否嵌入到框架中?
谢谢..