import java.awt.*;
import java.awt.event.*;class Hehe extends Frame implements ActionListener{ Button b= new Button("hehe");

public Hehe() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
    
add(b);
pack();
b.addActionListener(this);

}

public static void main(String args[]) {
System.out.println("Starting Hehe...");
Hehe mainFrame = new Hehe();
mainFrame.setSize(400, 400);
mainFrame.setTitle("Hehe");
mainFrame.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
//按钮处理事件的代码

}
}

解决方案 »

  1.   

    处理事件用内部类的方法是个不错选择:
    import java.awt.*;
    import java.awt.event.*;public class Hehe extends Frame{
        Button b;
        public Hehe(){
            b= new Button("hehe");
            //设置布局
            setLayyout(new BorderLayout());
            add(b,BorderLayout.SOUTH);
            //添加监听,Disposal对象来处理单击事件
            b.addActionListener(new Disposal());
            setSize(200,300);
            setVisible(true);
        }
        //此内部类实现actionListener接口,来处理点击事件
        class Disposal implements ActionListener{
            //在此放法中写按钮点击后的代码
            public void actionPerformed(ActionEvent e){
                dispose();       
            }
        }
    }
      

  2.   

    我明白了,原来少了addActionListener,谢谢fool4