Probleme pour changer le txt d'un jlabel

nykaula Messages postés 1 Date d'inscription samedi 5 avril 2003 Statut Membre Dernière intervention 27 décembre 2005 - 27 déc. 2005 à 17:02
gmi19oj19 Messages postés 545 Date d'inscription lundi 10 mai 2004 Statut Membre Dernière intervention 28 septembre 2011 - 28 déc. 2005 à 16:37
voila,
je test ce petit script et le but est que quand je clic sur un bouton, ca change le jlabel
mais ca ne fait rien

qq un pourrait m'aider
merci


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


class construction_interface extends JFrame implements ActionListener
{
public construction_interface()
{
JPanel pan_g = new JPanel();
JPanel pan_d = new JPanel();
JPanel pan = new JPanel();
JButton bt1 = new JButton("Bouton 1");
JButton bt2 = new JButton("Bouton 2");
bt1.addActionListener(this);
bt2.addActionListener(this);


JLabel texte = new JLabel("texte ici");
texte.setText("oui");
pan_g.setBackground(new Color(250,10,10));
pan_g.setMinimumSize(new Dimension(200,150));
pan.setMinimumSize(new Dimension(300,150));

pan_g.add(bt1);
pan_g.add(bt2);

pan_d.add(texte);

pan.add(pan_g);
pan.add(pan_d);
pan.setBackground(Color.yellow);
getContentPane().add(pan);


}

public void actionPerformed (ActionEvent e)
{
texte.setText("azerty");
}

private JButton bt1,bt2;
private JLabel texte;
private JPanel pan_g,pan_d,pan;

}


public class Jtagg
{
public static void main(String [] arg)
{
construction_interface fen=new construction_interface();
fen.setSize(300,150);

fen.setVisible(true);
}

}
A voir également:

2 réponses

luuuuudooooo Messages postés 64 Date d'inscription mardi 4 octobre 2005 Statut Membre Dernière intervention 2 août 2006 4
27 déc. 2005 à 17:17
Remplace ton actionPerformed par celui-ci.

public void actionPerformed (ActionEvent e)
{
JPanel pan = (JPanel)this.getContentPane().getComponent(0);
JPanel pan_d = (JPanel)pan.getComponent(1);
((JLabel)pan_d.getComponent(0)).setText("azerty");
}

Si tu ne comprend pas certaines choses n'hésite pas à le demander
0
gmi19oj19 Messages postés 545 Date d'inscription lundi 10 mai 2004 Statut Membre Dernière intervention 28 septembre 2011 2
28 déc. 2005 à 16:37
J'rajoute que c'est du au faie que "texte" est une variable propre au constructeyr et donc que tu ne peux l'appeler omme ça dans une méthode.

gmi19oj19
0
Rejoignez-nous