Bonjour/soir à tous, j'ai besoin de changer la "Card" d'un JFrame à partir de son JPanel mais je me demande comment je peux accéder à ce JFrame à partir du JPanel. J'ai trouvé 2 solutions qui me semble un peu brouillon, la première est d'envoyé la frame via le constructeur du panel et la seconde de faire des getparents jusqu'a récupéré la jframe, je pense que le soucis vient surtout de la conception, si quelqu'un aurait des conseils

voici le code :
JFrame :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import java.awt.BorderLayout;
import java.awt.CardLayout;
 
import javax.swing.JFrame;
 
 
public class Gui extends JFrame {
	PanneauLogin plog;
	PanneauWebcam pweb;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Gui fenetre = new Gui();
	}
 
	public Gui (){
		this.plog=new PanneauLogin();
		this.pweb=new PanneauWebcam();
		this.setLayout(new CardLayout());
		this.setSize(250,180);
		this.setVisible(true);
		this.setResizable(false);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.add(plog,"Login");
		this.add(pweb,"Webcam");
	}
}
JPanel
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
 
public class PanneauLogin extends JPanel implements ActionListener {
 
private JLabel lablog;
private JLabel labmdp;
private JTextField login;
private JTextField mdp;
private JButton envoi;
	public PanneauLogin(){
		this.login=new JTextField(20);
		this.mdp=new JTextField(20);
		this.envoi=new JButton("Se connecter");
		this.envoi.addActionListener(this);
		this.lablog = new JLabel("Login : ");
		this.labmdp = new JLabel("MDP : ");
		this.setLayout(new FlowLayout());
		this.add(lablog);
		this.add(login);
		this.add(labmdp);
		this.add(mdp);
		this.add(envoi);
	}
	@Override
	public void actionPerformed(ActionEvent action) {
		if ((this.login.getText().length()==0)||(this.mdp.getText().length()==0))
		{
				System.out.println("Pas de login ou mdp");
		}
		else
		{
			// Changement de card
		}
	}
}
Merci