适配器类 Adapter,对接口中的抽象方法进行空实现
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/*
* 适配器类 Adapter,对接口中的抽象方法进行空实现
*/
public class Test07 extends Frame{
Button btn=new Button("点我");
public Test07(String title){
super(title);
init();
}
public void init(){
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("被点中了!");
}
});
add(btn);
setSize(200,200);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Test07("哈哈");
}
}
时间: 2024-10-10 23:26:59