日期:2014-05-20  浏览次数:20717 次

请教按钮动作事件问题
我是照着书上打的代码,两种


第一种:
package swing;

import javax.swing.*;
import java.awt.event.*;

public class AE2 extends JFrame{
JButton jb6=new JButton();
int i =0;

public AE2(){
this.setTitle("按钮动作事件");

jb6.setText("按钮按下了0次");
jb6.setMnemonic('b');
jb6.addActionListener(this);
this.add(jb6);

this.setBounds(100,200,300,400);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e){
this.jb6.setText("按钮按下了" + (++i) + "次");
}

public static void main(String[] args){
AE2 ae2=new AE2();
}
}
这段代码的jb6.addActionListener(this);这里出现报错,错误信息为:
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (AE2)



第二种:
package swing;

import javax.swing.*;
import java.awt.event.*;

public class AE extends JFrame{
JButton jb5=new JButton();
int i=10;



public AE(){
this.setTitle("按钮动作事件");

jb5.setText("按钮按下了0次");
jb5.setMnemonic('w');
this.add(jb5);

jb5.addActionListener(new ActionListener(){
public void actionPerformed(AE e){
AE.this.jb5.setText("按钮按下了" + (++i) + "次");
}
}
);

this.setBounds(100,200,300,400);
this.setVisible(true);
}



public static void main(String[] args){
AE ae=new AE();
}
}

这段代码也是jb5.addActionListener(new ActionListener()里面的ActionListener方法报错,错误信息为:
The type new ActionListener(){} must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)


我不是很明白到底是哪里出了错 请高手指点下

------解决方案--------------------
没有实现ActionListener接口;
public class AE2 extends JFrame implememnts ActionListener{}
------解决方案--------------------
ls正解,没有实现ActionListener

The type new ActionListener(){} must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)

这句已经说的很明白了,Lz要加强英文阅读能力