Exercice 1 : Fenêtre simple
Créer une fenêtre avec un message.
🎯 Objectif :
Créer une JFrame
Ajouter un JLabel
💻 Code attendu :
import [Link].*;
public class Ex1 {
public static void main(String[] args) {
JFrame frame = new JFrame("Ma première fenêtre");
JLabel label = new JLabel("Bonjour Java Swing !");
[Link](label);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
Exercice 2 : Bouton avec événement
Ajouter un bouton qui affiche un message quand on clique.
🎯 Objectif :
Utiliser JButton
Gérer un ActionListener
💻 Exemple :
import [Link].*;
import [Link].*;
public class Ex2 {
public static void main(String[] args) {
JFrame frame = new JFrame("Bouton");
JButton button = new JButton("Clique ici");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](null, "Bouton cliqué !");
});
[Link](button);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
Exercice 3 : Addition de deux nombres
Créer une interface avec :
2 champs texte
1 bouton
Afficher la somme
🎯 Objectif :
JTextField
Conversion String → int
💻 Exemple :
import [Link].*;
import [Link].*;
public class Ex3 {
public static void main(String[] args) {
JFrame frame = new JFrame("Addition");
JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JButton btn = new JButton("Additionner");
[Link](50, 30, 100, 30);
[Link](50, 70, 100, 30);
[Link](50, 110, 120, 30);
[Link](t1);
[Link](t2);
[Link](btn);
[Link](null);
[Link](e -> {
int a = [Link]([Link]());
int b = [Link]([Link]());
[Link](frame, "Résultat : " + (a + b));
});
[Link](250, 200);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
Exercice 4 : Mini calculatrice
Créer une calculatrice avec :
Addition
Soustraction
Multiplication
Division
🎯 Bonus :
Ajouter un JComboBox pour choisir l’opération
Exercice 5 : Formulaire utilisateur
Créer un formulaire avec :
Nom
Email
Mot de passe
Bouton "Valider"
🎯 Objectif :
Utiliser JPasswordField
Vérifier si les champs sont vides
Exercice 6 (Avancé) : TODO List
Créer une application :
Ajouter une tâche
Supprimer une tâche
Liste affichée (JList)
💡 Conseils importants
Utilise setLayout(null) seulement pour débuter
Ensuite, apprends :
o BorderLayout
o FlowLayout
o GridLayout