Bonjour, j'ai une fenêtre principale et 3 boutons (produit, client et accueil) qui permettent d'ouvrir les autres fenêtres mais quand j'ouvre une fenetre les autres ne se ferment pas. Merci pour votre aide. Voici mes codes:
/**
*
*/
package projetJava;
import java.awt.BorderLayout;
public class Fenetre extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
// frame principale
static JFrame fenetre = new JFrame();
Container conteneur;
static JPanel panelTitre, panelMenu, panelPrincipal;
JButton b2 = new JButton("Produit");
JButton b3 = new JButton("Client");
JLabel titre = new JLabel("TITRE DE LA PAGE");
JLabel choix = new JLabel("FAITES VOTRE CHOIX");
Font f;
public Fenetre() {
super();
conteneur = fenetre.getContentPane();
fenetre.setSize (new Dimension (800, 600));
fenetre.setResizable (false);
//fenetre.setVisible(true);
fenetre.setLocationRelativeTo(null);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelPrincipal = new JPanel(new BorderLayout());
panelPrincipal.setBackground(new Color(0xFABF8F));
choix.setHorizontalAlignment(JLabel.CENTER);
panelPrincipal.add(choix);
panelMenu = new JPanel();
panelMenu.setBackground(new Color(0x95B3D7));
panelMenu.setPreferredSize(new Dimension(150,20));
b2.addActionListener(this);
panelMenu.add(b2);
b3.addActionListener(this);
panelMenu.add(b3);
panelTitre = new JPanel();
f = new Font("Monospaced", Font.BOLD, 24);
titre.setFont(f);
titre.setForeground(Color.red);
panelTitre.setBackground(new Color(0xC4BC96));
panelTitre.add(titre);
conteneur.add(panelTitre, BorderLayout.NORTH);
conteneur.add(panelMenu, BorderLayout.WEST);
conteneur.add(panelPrincipal, BorderLayout.CENTER);
fenetre.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
//Si on clique sur Produit
if(ae.getActionCommand() == "Produit")
{
try {
fenetre.dispose();
Produit p = new Produit();
p.actionPerformed(ae);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
//Si on clique sur Client
if(ae.getActionCommand() == "Client")
{
try {
fenetre.dispose();
Client c = new Client();
c.actionPerformed(ae);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
/**
*
*/
package projetJava;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Produit extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
static JFrame Produit;
static JPanel menuProduit;
JButton btnProduit1 = new JButton("Accueil");
JButton btnProduit2 = new JButton("Ajouter une produit");
JButton btnProduit3 = new JButton("Modifier une produit");
JButton btnProduit4 = new JButton("Supprimer un produit");
JButton btnProduit5 = new JButton("Quitter");
public Produit() {
// TODO Auto-generated constructor stub
}
public ActionListener AjoutProduit() {
return null;
// TODO Auto-generated constructor stub
}
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
try {
dispose();
Produit = new JFrame();
Produit.setVisible(true);
Produit.setLocationRelativeTo(null);
Produit.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Produit.setSize(new Dimension (800, 600));
Produit.setResizable(false);
Produit.setLocationRelativeTo(null);
//Fenetre.b1.setEnabled(false);
Produit.add(Fenetre.panelTitre, BorderLayout.NORTH);
Produit.add(Fenetre.panelMenu, BorderLayout.WEST);
Produit.add(Fenetre.panelPrincipal, BorderLayout.CENTER);
menuProduit = new JPanel();
//menuProduit.setBackground(new Color(0x95B3D7));
menuProduit.setPreferredSize(new Dimension(150,20));
btnProduit1.addActionListener(new Accueil());
menuProduit.add(btnProduit1);
btnProduit2.addActionListener(AjoutProduit());
menuProduit.add(btnProduit2);
Fenetre.panelPrincipal.add(menuProduit);
Fenetre.panelPrincipal.setVisible(true);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/**
*
*/
package projetJava;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Client extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
static JFrame Client;
static JPanel menuClient;
JButton btnClient1 = new JButton("Accueil");
JButton btnClient2 = new JButton("Ajouter un client");
JButton btnClient3 = new JButton("Modifier un client");
JButton btnClient4 = new JButton("Supprimer un client");
JButton btnClient5 = new JButton("Quitter");
public Client() {
// TODO Auto-generated constructor stub
}
public ActionListener AjoutClient() {
return null;
// TODO Auto-generated constructor stub
}
@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
try {
dispose();
Client = new JFrame();
Client.setVisible(true);
Client.setLocationRelativeTo(null);
Client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Client.setSize(new Dimension (800, 600));
Client.setResizable(false);
Client.setLocationRelativeTo(null);
//Fenetre.b1.setEnabled(false);
Client.add(Fenetre.panelTitre, BorderLayout.NORTH);
Client.add(Fenetre.panelMenu, BorderLayout.WEST);
Client.add(Fenetre.panelPrincipal, BorderLayout.CENTER);
menuClient = new JPanel();
//menuClient.setBackground(new Color(0x95B3D7));
menuClient.setPreferredSize(new Dimension(150,20));
btnClient1.addActionListener(new Accueil());
menuClient.add(btnClient1);
btnClient1.addActionListener(AjoutClient());
menuClient.add(btnClient1);
Fenetre.panelPrincipal.add(menuClient);
Fenetre.panelPrincipal.setVisible(true);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/**
*
*/
package projetJava;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class Accueil extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
public Accueil() {
// TODO Auto-generated constructor stub
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
new Fenetre();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Normal que ça ne marche pas, tu as raté une étape.
Voilà le code de ton écouteur (un extrait) :
if(ae.getActionCommand() == "Produit")
Ici, tel que c'est fait, tu veux exécuter du code si la commande est "Produit" (ou passage, == pour des String n'est pas très appropriée, equals est meilleur). Sauf que tu n'as défini aucune commande pour tes composants, par exemple
monBOuton.setActionCommand("Produit");
Donc il faudrait que tu définisses des commandes sur tes boutons pour savoir sur lequel tu as fait ton clic. Enfin, ce n'est qu'une idée.
--
Pylouq Heureux sont ceux qui lisent le Réglement, ils ne finiront peut-être pas au bûcher.