import javax.swing.*; import java.awt.*; import java.awt.event.*; class Mypanel extends JPanel implements ActionListener { public Mypanel(){ super(); // Création des boutons JButton cyanButton = new JButton("Cyan"); JButton magentaButton = new JButton("Magenta"); JButton yellowButton = new JButton("Jaune"); // Ajout des action listeners aux boutons cyanButton.addActionListener(this); magentaButton.addActionListener(this); yellowButton.addActionListener(this); // Création du panneau pour contrôler la couleur de fond this.add(cyanButton); this.add(magentaButton); this.add(yellowButton); } @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); switch (command) { case "Cyan": this.setBackground(Color.CYAN); break; case "Magenta": this.setBackground(Color.MAGENTA); break; case "Jaune": this.setBackground(Color.YELLOW); break; default: break; } } } public class Fond{ public static void main(String[] args) { JFrame fenetre = new JFrame(); fenetre.setSize(500, 500); fenetre.setTitle("Changement de Fond"); fenetre.setLayout(new GridLayout(1, 1)); fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Création du panneau pour contrôler la couleur de fond JPanel panneau = new Mypanel(); fenetre.add(panneau); fenetre.setVisible(true); } }