changer de panel quand appuie sur bouton
Bonjour,
Ca fait un certain temps que je recherche et là je bloque.
En résumé, mon void doit créer ma fenêtre et mon panel 1 (panel insérer dans ma fenetre).
Dans ce panel, il y a un bouton qui doit faire changer le panel (panel1 doit devenir Panel2).
Puis un bouton dans mon panel 2 qui doit refaire changer panel: panel2 en panel 1.
Est-ce que c'est une bonne idée pour gérer la navigation entre les différentes pages de mon "logiciel"?
voici mon code
Mon void:
Code:
1 2 3 4 5 6 7 8 9 10
| public class Formulaire{
public static void main(String[] args) {
MaFrame fenetre=new MaFrame();
Panel1 p=new Panel1();
JPanel panel=p.creaPanel1(fenetre);
fenetre.changerPanel("coucou",200,200,panel);
}
} |
J'ai ma classe MaFrame:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class MaFrame extends JFrame {
public MaFrame() {
super();
acceuil=new Panel1();
client=new Panel2();
setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
setResizable(true); //On interdit la redimensionnement de la fenêtre
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit à l'application de se fermer lors du clic sur la croix
setVisible(true);
}
public void changerPanel(String titre,int x,int y,JPanel panel){
setTitle(titre); //On donne un titre à l'application
setSize(x,y); //On donne une taille à notre fenêtre
setContentPane(panel);
}
} |
ma classe Panel1:
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
| public class Panel1 extends JPanel{
MaFrame fenetre;
public JPanel creaPanel1(MaFrame fenetre){
this.fenetre=fenetre;
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(Color.white);
JComboBox liste = new JComboBox ();
liste.setMaximumRowCount(5);
liste.setBounds (550, 120,150,30);
liste.setBackground(Color.WHITE);
panel.add (liste);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:busnessplan");
Statement stmt = con.createStatement();
String sql="select distinct denomination from Clients";
ResultSet rst=stmt.executeQuery(sql);
liste.removeAllItems();
while(rst.next())
{
liste.addItem(rst.getString("denomination"));
}
} catch(Exception se) {
System.out.println("erreur commise est: "+se);
}
panel.add(liste);
JButton bouton = new JButton(new Action(this, "Nouveau Client",fenetre));
panel.add(bouton);
return panel;
}
} |
ma classe Action:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Action extends AbstractAction {
private Panel1 panel;
private MaFrame frame;
public Action(Panel1 panel, String texte,MaFrame frame){
super(texte);
this.panel = panel;
this.frame=frame;
}
public void actionPerformed(ActionEvent e) {
Panel2 pane=new Panel2();
JPanel panel2=pane.creaPanel2(frame);
frame.changerPanel("CREATION CLIENT",200,200,panel2);
}
} |
ma classe Panel2:
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
| public class Panel2 extends JPanel{
MaFrame fenetre;
public JPanel creaPanel2(MaFrame fenetre){
this.fenetre=fenetre;
JPanel panel=new JPanel();
//Déclaration des composants
//Label
JLabel lbDenomination = new JLabel("Dénomination:");
//Zone de saisie: JTextField
JTextField tfDenomination = new JTextField("");
//Bouton
JButton btValider=new JButton(new ActionBis(this,"Valider",fenetre));
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
GridBagConstraints gbh=new GridBagConstraints();
panel.setLayout(gb);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx=1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gb.setConstraints(lbDenomination, gbc); // mise en forme des objets
gb.setConstraints(tfDenomination, gbh);
gb.setConstraints(btValider, gbc);
panel.add(lbDenomination);
panel.add(tfDenomination);
panel.add(btValider);
return panel;
}
} |
et enfin ma classe ActionBis:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class ActionBis extends AbstractAction {
private Panel2 panel;
private MaFrame frame;
public ActionBis(Panel2 panel, String texte,MaFrame frame){
super(texte);
this.panel = panel;
this.frame=frame;
}
public void actionPerformed(ActionEvent e) {
Panel1 p=new Panel1();
JPanel panel=p.creaPanel1(frame);
frame.changerPanel("coucou",200,200,panel);
}
} |
Désolée de mettre tout le code mais à un moment ça a marché puis maintenant, pas de pb à la compile mais j'ai comme résultat ma fenetre avec pour titre "coucou" mais rien à l'interieur:cry:
merci beaucoup pour votre aide
claire