如何在java中使用actionperformed

时间:2014-05-24 00:03:47

标签: java eclipse swing

我在这里重新编写代码,我收到以下错误:

ActionPerformed cannot be resolved to a type
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (fenetre)
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (fenetre)

at fenetre.<init>(fenetre.java:12)
at Test.main(Test.java:4)

代码

import java.awt.*;

import javax.swing.*;
import javax.swing.event.*;


public class fenetre extends JFrame implements ActionPerformed{
    JPanel pan=new JPanel (new FlowLayout());                   //  instancier un  objet jpanel qui contiendra nos composents 
    private JButton nouveau_utilisateur ;
    private JButton identification;

    // le constructeur 
    public fenetre (){

        setTitle("la reconnaissance de la siganteur manuscrite ");        // la taille de la fenetre 
        setSize(400,500);                                                   //positionné notre fenetre au centre 
        setLocationRelativeTo(null);                                    //terminer le processus lorsque on clique sur la croix rouge 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        nouveau_utilisateur=new JButton("nouveau_utilisateur");
        identification =new JButton("identification");
        identification.addActionListener(this);
        nouveau_utilisateur.addActionListener(this);


        pan.add(identification);
        pan.add(nouveau_utilisateur);

        add(pan);   

        this.setVisible(true);
    }

}

1 个答案:

答案 0 :(得分:1)

要添加为动作侦听器的类必须实现ActionListener接口。在您的情况下fenetre

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

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


public class fenetre extends JFrame implements ActionListener{
    JPanel pan=new JPanel (new FlowLayout());                   //  instancier un  objet jpanel qui contiendra nos composents 
    private JButton nouveau_utilisateur ;
    private JButton identification;

    // le constructeur 
    public fenetre (){

        setTitle("la reconnaissance de la siganteur manuscrite ");        // la taille de la fenetre 
        setSize(400,500);                                                   //positionné notre fenetre au centre 
        setLocationRelativeTo(null);                                    //terminer le processus lorsque on clique sur la croix rouge 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        nouveau_utilisateur=new JButton("nouveau_utilisateur");
        identification =new JButton("identification");
        identification.addActionListener(this);
        nouveau_utilisateur.addActionListener(this);

        pan.add(identification);
        pan.add(nouveau_utilisateur);

        add(pan);   

        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        //Some code
    }

}