JAVA的GUI监听问题 高分追加!!

来源:百度知道 编辑:UC知道 时间:2024/07/16 06:24:19
以下我所编写的一段代码 但是处理不好监听 请高人指点以实现按钮监听功能 例如"取消"为清空卡号密码 高分追加!!
import java.awt.*;
import java.awt.event.*;
public class atm implements ActionListener
{
Frame f;
Label a,b;
TextField card,key;
Button sure,cancel;
public static void main(String args[])
{ atm myatm = new atm();
myatm.go();
}
public void go()
{
f = new Frame("ATM");
a=new Label("卡号");
b=new Label("密码");
card = new TextField();
key = new TextField();
sure = new Button("确认");
cancel = new Button("取消");

f.setLayout(new GridLayout(3,2));
f.add(a);
f.add(card);
f.add(b);
f.add(key);
f.add(sure);
f.add(cancel);

f.setSize(400,400);
f.pack();
f.setVisible(true);

f.addActionListener(this);

public void perform(ActionEvent e)
{
Object o = e.getSource();//得到事件发生的对象
if(o==cancel ){ //如果是“取消”按钮
card .setText(""); //把textField的内容设为空
key.setText("");
}
}

public void perform(ActionEvent e)
{

}
这儿错了,函数名应该是actionPerfrmed

Frame可以添加ActionListener吗
要给按钮cancel添加监视器
我用内部类方法写的监视器代码
import java.awt.*;
import java.awt.event.*;
public class atm implements ActionListener
{
Frame f;
Label a,b;
TextField card,key;
Button sure,cancel;
public static void main(String args[])
{ atm myatm = new atm();
myatm.go();
}
public void go()
{
f = new Frame("ATM");
a=new Label("卡号");
b=new Label("密码");
card = new TextField();
key = new TextField();
sure = new Button("确认");
cancel = new Button("取消");
cancel.ad