implements implements是一个类实现一个接口用的关键字,他是用来实现接口中定义的抽象方法。比如:people是一个接口,他里面有say这个方法。public interface people(){ public say();}但是接口没有方法体。只能通过一个具体的类去实现其中的方法体。比如chinese这个类,就实现了people这个接口。 public class chinese implements peopel{ public say() {System.out.println("你好!");}} 在java中implements表示子类继承父类,如类A继承类B写成 class A implements B{}
ActionListener是个监听器,
你如果要对某个按钮监听,
要写个listener实现ActionListener,
再将这个listener加到你要监听的对象上,
比如button.addActionListener(listener),
ActionListener的实现可以好几种:内部类,匿名类,外部类,自身(常用)
ActionListener是个监听器,
你如果要对某个按钮监听,
要写个listener实现ActionListener,
再将这个listener加到你要监听的对象上,
比如button.addActionListener(listener),
ActionListener的实现可以好几种:内部类,匿名类,外部类,自身(常用)
第一种
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class TestActionListener1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("你点击了按钮");
}
public void test() {
JButton s_button = new JButton("按钮");
s_button.addActionListener(this);
}
}
第二种
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class TestActionListener2 {
public void test() {
JButton s_button = new JButton("按钮");
MyButtonActionListener s_listener = new MyButtonActionListener();
s_button.addActionListener(s_listener);
}
private class MyButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("你点击了按钮");
}
}
}
第三种:
public class TestActionListener3 {
public void test() {
JButton s_button = new JButton("按钮");
s_button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("你点击了按钮");
}
});
}
}ActionListener就是一个动作监听器,通过它能传递你组件需要实现的功能,没有它你的组件就只是一个图标,没有实际的功能意义。