recuperer une variable d'une autre classe: s'est y possible
Bonsoir,
Recuperer et afficher une variable grace a un bouton, tout ca dans le meme fichier (meme classe), j'y arrive. mais lorsque ce sont des classes differentes (fichiers), je bloque totalement.
Voici le code
Classe de la fenetre principale
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
public class MainWin extends JFrame {
// objets crees/importes
JPanel panContainer= new JPanel();
JPanel panNorth= new JPanel();
JPanel panCenter= new JPanel();
JPanel panSouth= new JPanel();
JPanel panEast= new JPanel();
JPanel panWest= new JPanel();
JLabel lblAfficheNom= new JLabel();
JLabel lblNom= new JLabel();
Humain h01= new Humain();
Btn btn= new Btn();
// variables
// constructeur
public MainWin() {
this.setSize(800, 822);
this.setTitle("");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//-----
this.setContentPane(panContainer);
this.setLayout(new BorderLayout());
//-----
this.getContentPane().add(panNorth, BorderLayout.NORTH);
this.getContentPane().add(panCenter, BorderLayout.CENTER);
this.getContentPane().add(panSouth, BorderLayout.SOUTH);
this.getContentPane().add(panEast, BorderLayout.EAST);
this.getContentPane().add(panWest, BorderLayout.WEST);
panNorth.setBackground(Color.black);
panCenter.setBackground(Color.white);
panSouth.setBackground(Color.black);
panEast.setBackground(Color.black);
panWest.setBackground(Color.black);
//-----
panCenter.setLayout(new GridLayout(1, 2));
lblAfficheNom.setText("affiche le nom");
panCenter.add(lblAfficheNom);
panCenter.add(lblNom);
panCenter.add(btn);
//-----
//JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
//JOptionPane.showInternalMessageDialog(panCenter, "information", "information", JOptionPane.INFORMATION_MESSAGE);
//JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION);
this.setVisible(true);
}
} |
code de la classe Humain
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public class Humain {
// variables
public String nom= "dupont";
public String prenom= "martin";
// constructeur
public Humain() {
}
// getters
public String getNom() {
return this.nom;
}
} |
et code de la classe Btn
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
import javax.swing.JButton;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Btn extends JButton implements ActionListener {
// variables
// contructeur
public Btn() {
this.addActionListener(this);
}
// getters
// setters
// methodes de classe
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
lblNom.setText(h01.getNom());
}
} |
Avec ce code dans Btn, eclipse dit que lblNom et h01 ne peuvent pas etre resolus.
Mais si jamais je fais heriter Btn de MainWin (ma classe principale), alors les noms de variable sont résolus mais je ne peux pas implémenter mon Actionlistener.
Alors je sais pas si concevoir une interaction de ce genre est possible ou pas.
En vous remerciant pour les reponses.
areuh