|
@@ -0,0 +1,52 @@
|
|
|
|
|
+package formation.java.gui.bouton;
|
|
|
|
|
+
|
|
|
|
|
+import java.awt.Color;
|
|
|
|
|
+import java.awt.Container;
|
|
|
|
|
+import java.awt.event.ActionEvent;
|
|
|
|
|
+import java.awt.event.ActionListener;
|
|
|
|
|
+
|
|
|
|
|
+import javax.swing.JButton;
|
|
|
|
|
+import javax.swing.JFrame;
|
|
|
|
|
+import javax.swing.JLabel;
|
|
|
|
|
+import javax.swing.JPanel;
|
|
|
|
|
+
|
|
|
|
|
+public class DemoBouton {
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+ DemoBouton db = new DemoBouton();
|
|
|
|
|
+ db.creerFenetre("Démo Bouton 1");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void creerFenetre(String titre) {
|
|
|
|
|
+ JFrame fenetre = new JFrame(titre);
|
|
|
|
|
+ fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
+ fenetre.setBounds(50, 100, 500, 400);
|
|
|
|
|
+
|
|
|
|
|
+ Container panneauPrincipal = fenetre.getContentPane();
|
|
|
|
|
+ JPanel panneauDeTravail = new JPanel();
|
|
|
|
|
+ panneauPrincipal.add(panneauDeTravail);
|
|
|
|
|
+
|
|
|
|
|
+ JButton bouton = new JButton("Créer");
|
|
|
|
|
+ panneauDeTravail.add(bouton);
|
|
|
|
|
+
|
|
|
|
|
+ EcouteurAction ecouteur = new EcouteurAction(panneauDeTravail);
|
|
|
|
|
+ bouton.addActionListener(ecouteur); //Branche mon écouteur au bouton
|
|
|
|
|
+
|
|
|
|
|
+ fenetre.setVisible(true);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+class EcouteurAction implements ActionListener {
|
|
|
|
|
+ JPanel panneau ;
|
|
|
|
|
+
|
|
|
|
|
+ EcouteurAction(JPanel unPanneau){
|
|
|
|
|
+ panneau = unPanneau;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void actionPerformed(ActionEvent e) {
|
|
|
|
|
+ System.out.println("Bouton appuyé");
|
|
|
|
|
+ //panneauDeTravail
|
|
|
|
|
+ panneau.add(new JLabel("Du texte"));
|
|
|
|
|
+ panneau.revalidate();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|