Java GUI addActionListener error.

betsprite -  
 betsprite -
Bonsoir,

I am currently working on a GUI in Java and when I try to introduce the addActionListener function, I get the following error:

*****
Exception in thread "main" java.lang.ClassCastException: Fenetre cannot be cast to java.awt.event.ActionListener
at Fenetre.<init>(Fenetre.java:36)
at Test.main(Test.java:17)
*****

Here is my code, which is not complicated (basically, I just want that when I click on a button, it shows me that I've clicked "so many" times on the button:

------------------------------------------------------------------------------------------------------------------------------
Class Panneau

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;

public class Panneau extends JPanel{

public void paintComponent(Graphics g){

Graphics2D g2d = (Graphics2D)g;

g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, this.getWidth(), this.getHeight());

g2d.setColor(Color.WHITE);
Font font = new Font("Arial", Font.BOLD, 14);
g2d.setFont(font);
g2d.drawString("Hello!", 10, 15);

}

}

------------------------------------------------------------------------------------------------------------------------------
Class Fenetre

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Fenetre extends JFrame{

private JButton bouton = new JButton("button");
private JPanel container = new JPanel();
private JLabel label1 = new JLabel("label1");
private Panneau pan = new Panneau();
private int compteur;

public Fenetre(){

this.setTitle("My first Java window");
this.setSize(400, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

container.setLayout(new BorderLayout());
container.add(label1, BorderLayout.NORTH);
container.add(bouton, BorderLayout.SOUTH);
container.add(pan, BorderLayout.CENTER);
container.setBackground(Color.WHITE);

bouton.addActionListener(this); // WARNING HERE
label1.setHorizontalAlignment(JLabel.CENTER);

this.setContentPane(container);
this.setVisible(true);

}

public void actionPerformed(ActionEvent arg0){
this.compteur++;
label1.setText("you have clicked " + this.compteur + " times");
}

}

------------------------------------------------------------------------------------------------------------------------------
The main class

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {

public static void main(String[] args) {

Fenetre fen = new Fenetre();

}

}
------------------------------------------------------------------------------------------------------------------------------

I remind you of the error (surely related to the WARNING in the Fenetre class):

*****
Exception in thread "main" java.lang.ClassCastException: Fenetre cannot be cast to java.awt.event.ActionListener
at Fenetre.<init>(Fenetre.java:36)
at Test.main(Test.java:17)
*****

I hope you can help me.

Thank you in advance!

Best regards.
Configuration: Windows XP Internet Explorer 8.0
</init></init>

2 answers

betsprite
 
Bon j'ai du nouveau. J'ai ajouté dans ma classe Fenetre deux classes internes permettant chacune d'exécuter une action lorsque 1 bouton (il y en a à présent 2) est écouté.

Et maintenant miracle ! il n'y a plus d'erreurs :p
J'aimerais bien comprendre quand même d'où cette erreur venait sur le addActionListener ...

Aussi, avec les deux nouvelles classes internes, maintenant, lorsqu'un bouton est écouté, je dois mettre :

"bouton1.addActionListener(new Bouton1Listener());" au lieu de "bouton1.addActionListener(this);" pour dire que c'était la Fenetre qui écoutait le bouton1.
Seulement maintenant que Bouton1Listener est une classe interne de Fenetre, comment le programme sait que c'est encore la Fenetre qui écoute le bouton1 et pas l'objet Bouton1Listener ?

Voici sinon le code modifié de la class Fenetre :

------------------------------------------------------------------------------------------------------------------------------

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Fenetre extends JFrame{

private JButton bouton1 = new JButton("bouton1");
private JButton bouton2 = new JButton("bouton2");
private JPanel container1 = new JPanel();
private JPanel container2 = new JPanel();
private JLabel label1 = new JLabel("label1");
private Panneau pan = new Panneau();

public Fenetre(){

this.setTitle("Ma première fenêtre java");
this.setSize(400, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

container1.setLayout(new BorderLayout());

container1.add(label1, BorderLayout.NORTH);
label1.setHorizontalAlignment(JLabel.CENTER);

container1.add(container2, BorderLayout.SOUTH);
container2.add(bouton1);
container2.add(bouton2);

container1.add(pan, BorderLayout.CENTER);
container1.setBackground(Color.WHITE);

bouton1.addActionListener(new Bouton1Listener());
bouton2.addActionListener(new Bouton2Listener());

this.setContentPane(container1);
this.setVisible(true);

}

class Bouton1Listener implements ActionListener{

public void actionPerformed(ActionEvent arg0) {
label1.setText("Vous avez cliqué sur le bouton 1");
}

}

class Bouton2Listener implements ActionListener{

public void actionPerformed(ActionEvent e) {
label1.setText("Vous avez cliqué sur le bouton 2");
}

}

}
------------------------------------------------------------------------------------------------------------------------------

Voilà j'espère que vous pourrez m'aider à comprendre ces quelques petites choses.

Merci encore :)
0
betsprite
 
No one has the courage to read these few lines of code to help me find my error (or a concept that I might not know since the error disappears with inner classes and I have asked a few questions above)?

Thank you in advance. I hope you can unblock me :s.

Have a great day!
0