Fermer JFrame depuis ActionListener
fab
-
R911 -
R911 -
Bonjour,
mon problème est le suivant:
Je voudrais fermer une fenêtre lorsque je clique sur le bouton Valider, une fois que l'opération affectée au bouton Valider a été effectuée. Que dois-je mettre si cela est réalisable à la place des points d'interrogation au bas du programme pour fermer (ou cacher) la fenêtre sans arrêter tout le programme (System.exit(0))? Voilà le code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class saisie_Produit extends JPanel implements ActionListener {
public JTextField stock,prix, designation, categorie , code;
public JLabel Stock,Prix, Designation, Categorie , Code;
public JButton Valider;
public JPanel jp1,jp2,jp3,jp4;
public JTextArea jta;
public saisie_Produit (){
this.jta = new JTextArea("Si rien ne s'affiche lorsque vous validez, vérifiez le code produit");
this.stock = new JTextField(10);
this.prix = new JTextField(10);
this.designation = new JTextField(10);
this.categorie = new JTextField(10);
this.code = new JTextField(10);
this.jp1 = new JPanel(new GridLayout(6,2));
this.jp2 = new JPanel(new FlowLayout());
this.jp3 = new JPanel(new GridLayout(2,1));
this.jp4 = new JPanel(new BorderLayout());
this.Valider = new JButton("Valider");
this.Valider.addActionListener(this);
this.Stock = new JLabel("Stock: ");
this.Prix = new JLabel("Prix: ");
this.Designation = new JLabel("Designation: ");
this.Categorie = new JLabel("Categorie: ");
this.Code = new JLabel("Code: ");
this.jp1.add(Stock);jp1.add(stock);
this.jp1.add(Prix);jp1.add(prix);
this.jp1.add(Designation);jp1.add(designation);
this.jp1.add(Categorie);jp1.add(categorie);
this.jp1.add(Code);jp1.add(code);
this.jp2.add(Valider);
this.jp3.add(jta);
this.jp4.add(jp1,BorderLayout.NORTH);
this.jp4.add(jp2,BorderLayout.CENTER);
this.jp4.add(jp3,BorderLayout.SOUTH);
this.add(this.jp4);
}
public static void affich_saisie_Produit(){
JFrame f = new JFrame("Saisie Produit");
f.setSize(500,250);
saisie_Produit jp = new saisie_Produit();
f.add(jp);
f.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
f.setVisible(true);
}
public void actionPerformed(ActionEvent evenement) {
if (evenement.getSource() == Valider){
String resp=TP_produits.ajoute_un_Produit(this.stock.getText(),this.prix.getText(),this.designation.getText(),this.categorie.getText(),this.code.getText());
jta.setText(resp);
????????????
}
}
}
mon problème est le suivant:
Je voudrais fermer une fenêtre lorsque je clique sur le bouton Valider, une fois que l'opération affectée au bouton Valider a été effectuée. Que dois-je mettre si cela est réalisable à la place des points d'interrogation au bas du programme pour fermer (ou cacher) la fenêtre sans arrêter tout le programme (System.exit(0))? Voilà le code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class saisie_Produit extends JPanel implements ActionListener {
public JTextField stock,prix, designation, categorie , code;
public JLabel Stock,Prix, Designation, Categorie , Code;
public JButton Valider;
public JPanel jp1,jp2,jp3,jp4;
public JTextArea jta;
public saisie_Produit (){
this.jta = new JTextArea("Si rien ne s'affiche lorsque vous validez, vérifiez le code produit");
this.stock = new JTextField(10);
this.prix = new JTextField(10);
this.designation = new JTextField(10);
this.categorie = new JTextField(10);
this.code = new JTextField(10);
this.jp1 = new JPanel(new GridLayout(6,2));
this.jp2 = new JPanel(new FlowLayout());
this.jp3 = new JPanel(new GridLayout(2,1));
this.jp4 = new JPanel(new BorderLayout());
this.Valider = new JButton("Valider");
this.Valider.addActionListener(this);
this.Stock = new JLabel("Stock: ");
this.Prix = new JLabel("Prix: ");
this.Designation = new JLabel("Designation: ");
this.Categorie = new JLabel("Categorie: ");
this.Code = new JLabel("Code: ");
this.jp1.add(Stock);jp1.add(stock);
this.jp1.add(Prix);jp1.add(prix);
this.jp1.add(Designation);jp1.add(designation);
this.jp1.add(Categorie);jp1.add(categorie);
this.jp1.add(Code);jp1.add(code);
this.jp2.add(Valider);
this.jp3.add(jta);
this.jp4.add(jp1,BorderLayout.NORTH);
this.jp4.add(jp2,BorderLayout.CENTER);
this.jp4.add(jp3,BorderLayout.SOUTH);
this.add(this.jp4);
}
public static void affich_saisie_Produit(){
JFrame f = new JFrame("Saisie Produit");
f.setSize(500,250);
saisie_Produit jp = new saisie_Produit();
f.add(jp);
f.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
f.setVisible(true);
}
public void actionPerformed(ActionEvent evenement) {
if (evenement.getSource() == Valider){
String resp=TP_produits.ajoute_un_Produit(this.stock.getText(),this.prix.getText(),this.designation.getText(),this.categorie.getText(),this.code.getText());
jta.setText(resp);
????????????
}
}
}
A voir également:
- Fermer JFrame depuis ActionListener
- Fermer compte paypal - Guide
- Comment fermer un compte gmail - Guide
- Fermer compte outlook - Guide
- Comment fermer un programme qui ne répond pas - Guide
- Comment fermer un compte facebook sans mot de passe ✓ - Forum Facebook
2 réponses
essai ça
public class saisie_Produit JPanel implements ActionListener {
........
public saisie_Produit (){
......
Valider.addActionListener(new BoutonAction());
...
}
class BoutonAction implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
.............. // + tous se que tu veut
}
}
}
public class saisie_Produit JPanel implements ActionListener {
........
public saisie_Produit (){
......
Valider.addActionListener(new BoutonAction());
...
}
class BoutonAction implements ActionListener{
public void actionPerformed(ActionEvent e){
dispose();
.............. // + tous se que tu veut
}
}
}