java Event小问题
package extend;
import java.awt.event.WindowAdapter;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.*;
public class EventManagerDemo {
public static void main(String args[]){
final Frame f=new Frame("Test");
Button b=new Button("Press ME");
b.addActionListener(new ButtonHandler());
f.setLayout(new FlowLayout());
f.add(b);
f.setSize(200,100);
f.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent evt){
f.setVisible(true);
f.dispose();
System.exit(0);
}
});
f.setVisible(true);
}
}
class ButtonHandler implements ActionListener{
public void actionperformed(ActionEvent e){
System.out.print("已经捕获");
}
}
对于类ButtonHandler我已经重写了 接口的方法 还是报错 要我实现抽象方法 怎么办呢?
------解决方案--------------------你最后的 ButtonHandler的方法名称大小写有问题, actionperformed改成actionPerformed就OK
Java code
import java.awt.event.WindowAdapter;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.*;
public class EventManagerDemo {
public static void main(String args[]){
final Frame f=new Frame("Test");
Button b=new Button("Press ME");
b.addActionListener(new ButtonHandler());
f.setLayout(new FlowLayout());
f.add(b);
f.setSize(200,100);
f.addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent evt){
f.setVisible(true);
f.dispose();
System.exit(0);
}
});
f.setVisible(true);
}
}
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.print("已经捕获");
}
}
------解决方案--------------------
拼写错误
public void WindowClosing(WindowEvent evt){
技巧:
重写的方法加上 @Override