Programmation d'un bouton

Résolu
Messages postés
10
Date d'inscription
jeudi 4 mai 2006
Statut
Membre
Dernière intervention
14 mai 2006
-
Messages postés
492
Date d'inscription
vendredi 30 janvier 2004
Statut
Membre
Dernière intervention
16 mai 2007
-
Voilà j'ai un bouton dans une fenêtre et je voudrais le programmer de telle manière que lorsque l'on clique dessus, cette fenêtre se ferme et une autre fenêtre (préalablement crée) s'ouvre.

Pourriez-vous m'aider?Merci

3 réponses

Messages postés
492
Date d'inscription
vendredi 30 janvier 2004
Statut
Membre
Dernière intervention
16 mai 2007
12
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
fen2.setVisible(true);
fen1.dispose();
}

en replacant fen1 et fen2 par les variables qui conviennent
Messages postés
492
Date d'inscription
vendredi 30 janvier 2004
Statut
Membre
Dernière intervention
16 mai 2007
12
Salut
Ajoute un actionListener sur ton bouton et dans l'actionPerformed, tu fais un setVisible(true) sur la fenetre à afficher et un dispose()(si tu n'as plus besoin du tout) sur la fenetre de base.

monBouton.addActionListener(this);
...
public void actionPerformed(ActionEvent e) {
fen2.setVisible(true);
fen1.dispose();
}

Voila
A++
Messages postés
10
Date d'inscription
jeudi 4 mai 2006
Statut
Membre
Dernière intervention
14 mai 2006

Le problème c'est que je ne sais pas trop où mettre ce code dans mon code. Je travaille avec NetBean et il génére du code automatiquement alors je suis un peu perdue.

package ihm;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Background extends javax.swing.JFrame {

public Background() {
initComponents();
setTitle("ComptoirFact");
setSize(550,550);

}




// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jPasswordField1 = new javax.swing.JPasswordField();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem();
saveAsMenuItem = new javax.swing.JMenuItem();
jMenuItem1 = new javax.swing.JMenuItem();
exitMenuItem = new javax.swing.JMenuItem();
editMenu = new javax.swing.JMenu();
cutMenuItem = new javax.swing.JMenuItem();
copyMenuItem = new javax.swing.JMenuItem();
pasteMenuItem = new javax.swing.JMenuItem();
deleteMenuItem = new javax.swing.JMenuItem();
helpMenu = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();

getContentPane().setLayout(new java.awt.GridBagLayout());

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Monotype Corsiva", 1, 30));
jLabel1.setText("Bienvenue sur le logiciel de facturation ");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 8;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
getContentPane().add(jLabel1, gridBagConstraints);

jLabel2.setText("Fonction");
jLabel2.setPreferredSize(new java.awt.Dimension(120, 23));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 30, 0);
getContentPane().add(jLabel2, gridBagConstraints);

jLabel3.setText("Nom de famille");
jLabel3.setPreferredSize(new java.awt.Dimension(120, 23));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 7;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 30, 0);
getContentPane().add(jLabel3, gridBagConstraints);

jLabel4.setText("Mot de passe");
jLabel4.setPreferredSize(new java.awt.Dimension(120, 23));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 9;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 80, 0);
getContentPane().add(jLabel4, gridBagConstraints);

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrateur", "Chef des ventes", "Employé" }));
jComboBox1.setPreferredSize(new java.awt.Dimension(120, 23));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 5;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 30, 0);
getContentPane().add(jComboBox1, gridBagConstraints);

jPasswordField1.setMinimumSize(new java.awt.Dimension(11, 18));
jPasswordField1.setPreferredSize(new java.awt.Dimension(120, 23));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 9;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 80, 0);
getContentPane().add(jPasswordField1, gridBagConstraints);

jTextField1.setMinimumSize(new java.awt.Dimension(120, 18));
jTextField1.setPreferredSize(new java.awt.Dimension(120, 23));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 7;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 30, 0);
getContentPane().add(jTextField1, gridBagConstraints);

jButton1.setText("Valider");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 10;
getContentPane().add(jButton1, gridBagConstraints);

jButton2.setText("Annuler");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 10;
getContentPane().add(jButton2, gridBagConstraints);

jButton3.setText("Mot de passe perdu ?");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 10;
getContentPane().add(jButton3, gridBagConstraints);

jLabel5.setFont(new java.awt.Font("Monotype Corsiva", 1, 30));
jLabel5.setText("ComptoirFact");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = 7;
gridBagConstraints.gridheight = 2;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 50, 0);
getContentPane().add(jLabel5, gridBagConstraints);

jLabel6.setFont(new java.awt.Font("Calisto MT", 0, 24));
jLabel6.setText("Identifiez-vous");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = 4;
gridBagConstraints.ipadx = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 50, 0);
getContentPane().add(jLabel6, gridBagConstraints);

fileMenu.setText("Facture");
fileMenu.setEnabled(false);
fileMenu.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fileMenuActionPerformed(evt);
}
});

openMenuItem.setLabel("Ouvrir Facture");
fileMenu.add(openMenuItem);

saveMenuItem.setLabel("Enregistrer Facture");
fileMenu.add(saveMenuItem);

saveAsMenuItem.setLabel("Imprimer");
fileMenu.add(saveAsMenuItem);

jMenuItem1.setLabel("Supprimer Facture");
fileMenu.add(jMenuItem1);

exitMenuItem.setText("Quitter");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitMenuItemActionPerformed(evt);
}
});

fileMenu.add(exitMenuItem);

menuBar.add(fileMenu);

editMenu.setText("Edition");
editMenu.setEnabled(false);
cutMenuItem.setLabel("Couper");
editMenu.add(cutMenuItem);

copyMenuItem.setLabel("Copier");
editMenu.add(copyMenuItem);

pasteMenuItem.setLabel("Pr\u00e9c\u00e9dent");
editMenu.add(pasteMenuItem);

deleteMenuItem.setLabel("Supprimer");
editMenu.add(deleteMenuItem);

menuBar.add(editMenu);

helpMenu.setText("Gestion des utilisateurs");
helpMenu.setEnabled(false);
menuBar.add(helpMenu);

jMenu3.setText("Aide");
jMenu3.setEnabled(false);
jMenuItem2.setLabel("Aide ComptoirFact");
jMenu3.add(jMenuItem2);

jMenuItem3.setLabel("A propos");
jMenu3.add(jMenuItem3);

menuBar.add(jMenu3);

setJMenuBar(menuBar);

pack();
}
// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

}

private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {


}

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {

}




public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Background.setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JMenuItem copyMenuItem;
private javax.swing.JMenuItem cutMenuItem;
private javax.swing.JMenuItem deleteMenuItem;
private javax.swing.JMenu editMenu;
private javax.swing.JMenuItem exitMenuItem;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenu helpMenu;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem openMenuItem;
private javax.swing.JMenuItem pasteMenuItem;
private javax.swing.JMenuItem saveAsMenuItem;
private javax.swing.JMenuItem saveMenuItem;
// End of variables declaration

}