Actualiser src/fr/iutfbleau/papillon/FenetreAjout.java

This commit is contained in:
2025-10-21 16:51:03 +02:00
parent 95e8ef3171
commit 07525b46dc
+105 -99
View File
@@ -1,99 +1,105 @@
// package fr.iutfbleau.papillon;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FenetreAjout extends JFrame implements ActionListener {
private final JTextField champTitre;
private final JTextArea champContenu;
private final JButton boutonValider;
private final JButton boutonAnnuler;
public FenetreAjout() {
super("Ajouter un rappel");
setSize(300, 200);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Layout principal
setLayout(new BorderLayout(10, 10));
JPanel centre = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(4, 4, 4, 4);
c.fill = GridBagConstraints.HORIZONTAL;
add(centre, BorderLayout.CENTER);
// Titre
JLabel lblTitre = new JLabel("Titre :");
champTitre = new JTextField(20);
c.gridx = 0;
c.gridy = 0;
c.weightx = 0;
centre.add(lblTitre, c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1;
centre.add(champTitre, c);
// Contenu
JLabel lblContenu = new JLabel("Contenu :");
champContenu = new JTextArea(4, 20);
JScrollPane scroll = new JScrollPane(champContenu,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
c.gridx = 0;
c.gridy = 1;
c.weightx = 0;
c.fill = GridBagConstraints.BOTH;
centre.add(lblContenu, c);
c.gridx = 1;
c.gridy = 1;
c.weightx = 1;
centre.add(scroll, c);
// Bas : boutons
JPanel bas = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
boutonValider = new JButton("Valider");
boutonAnnuler = new JButton("Annuler");
// on enregistre les actions ici
boutonValider.addActionListener(this);
boutonAnnuler.addActionListener(this);
bas.add(boutonValider);
bas.add(boutonAnnuler);
add(bas, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == boutonAnnuler) {
dispose(); // ferme la fenêtre
}
else if (source == boutonValider) {
String titre = champTitre.getText().trim();
String contenu = champContenu.getText().trim();
if (titre.isEmpty() || contenu.isEmpty()) {
JOptionPane.showMessageDialog(this,
"Veuillez remplir les deux champs.",
"Champs manquants",
JOptionPane.WARNING_MESSAGE);
return;
}
// pour linstant, juste un message (tu pourras relier à Main plus tard)
JOptionPane.showMessageDialog(this,
"Rappel ajouté :\nTitre : " + titre + "\nContenu : " + contenu);
dispose();
}
}
}
// package fr.iutfbleau.papillon;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FenetreAjout extends JFrame implements ActionListener {
private final JTextField champTitre;
private final JTextArea champContenu;
private final JButton boutonValider;
private final JButton boutonAnnuler;
private final Main parent;
public FenetreAjout(Main parent) {
super("Ajouter un rappel");
this.parent = parent;
setSize(350, 250);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Layout principal
setLayout(new BorderLayout(10, 10));
JPanel centre = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(4, 4, 4, 4);
c.fill = GridBagConstraints.HORIZONTAL;
add(centre, BorderLayout.CENTER);
// Titre
JLabel lblTitre = new JLabel("Titre :");
champTitre = new JTextField(20);
c.gridx = 0;
c.gridy = 0;
c.weightx = 0;
centre.add(lblTitre, c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1;
centre.add(champTitre, c);
// Contenu
JLabel lblContenu = new JLabel("Contenu :");
champContenu = new JTextArea(4, 20);
JScrollPane scroll = new JScrollPane(champContenu,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
c.gridx = 0;
c.gridy = 1;
c.weightx = 0;
c.fill = GridBagConstraints.BOTH;
centre.add(lblContenu, c);
c.gridx = 1;
c.gridy = 1;
c.weightx = 1;
centre.add(scroll, c);
// Bas : boutons
JPanel bas = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
boutonValider = new JButton("Valider");
boutonAnnuler = new JButton("Annuler");
// on enregistre les actions ici
boutonValider.addActionListener(this);
boutonAnnuler.addActionListener(this);
bas.add(boutonValider);
bas.add(boutonAnnuler);
add(bas, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src == boutonAnnuler) {
// revenir à la fenêtre principale
Point pos = this.getLocation();
parent.setLocation(pos);
parent.setVisible(true);
this.setVisible(false);
return;
}
if (src == boutonValider) {
String titre = champTitre.getText().trim();
String contenu = champContenu.getText().trim();
if (titre.isEmpty() || contenu.isEmpty()) {
JOptionPane.showMessageDialog(this, "Veuillez remplir les deux champs.");
return;
}
// TODO: ici appeler une méthode du parent pour ajouter le rappel
// parent.ajouterRappel(titre, contenu);
parent.setVisible(true);
dispose();
}
}
}