65 lines
2.0 KiB
Java
65 lines
2.0 KiB
Java
|
|
import javax.swing.*;
|
||
|
|
import java.awt.*;
|
||
|
|
import java.awt.event.*;
|
||
|
|
|
||
|
|
public class Radio extends JFrame implements ActionListener {
|
||
|
|
private JPanel panel;
|
||
|
|
private JRadioButton cyanButton, magentaButton, yellowButton;
|
||
|
|
|
||
|
|
public Radio() {
|
||
|
|
setTitle("Radio");
|
||
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
|
|
setSize(300, 200);
|
||
|
|
setLocationRelativeTo(null);
|
||
|
|
|
||
|
|
// Création du panneau
|
||
|
|
panel = new JPanel();
|
||
|
|
panel.setLayout(new FlowLayout());
|
||
|
|
|
||
|
|
// Création des boutons radio
|
||
|
|
cyanButton = new JRadioButton("Cyan");
|
||
|
|
magentaButton = new JRadioButton("Magenta");
|
||
|
|
yellowButton = new JRadioButton("Yellow");
|
||
|
|
|
||
|
|
// Ajout des boutons radio au panneau
|
||
|
|
panel.add(cyanButton);
|
||
|
|
panel.add(magentaButton);
|
||
|
|
panel.add(yellowButton);
|
||
|
|
|
||
|
|
// Ajout des boutons radio à un groupe de boutons pour permettre la sélection unique
|
||
|
|
ButtonGroup buttonGroup = new ButtonGroup();
|
||
|
|
buttonGroup.add(cyanButton);
|
||
|
|
buttonGroup.add(magentaButton);
|
||
|
|
buttonGroup.add(yellowButton);
|
||
|
|
|
||
|
|
// Ajout d'un écouteur d'événements aux boutons radio
|
||
|
|
cyanButton.addActionListener(this);
|
||
|
|
magentaButton.addActionListener(this);
|
||
|
|
yellowButton.addActionListener(this);
|
||
|
|
|
||
|
|
// Ajout du panneau à la fenêtre
|
||
|
|
add(panel);
|
||
|
|
|
||
|
|
setVisible(true);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void actionPerformed(ActionEvent e) {
|
||
|
|
Color color = null;
|
||
|
|
// Vérifier quel bouton radio est sélectionné et définir la couleur correspondante
|
||
|
|
if (e.getSource() == cyanButton) {
|
||
|
|
color = Color.CYAN;
|
||
|
|
} else if (e.getSource() == magentaButton) {
|
||
|
|
color = Color.MAGENTA;
|
||
|
|
} else if (e.getSource() == yellowButton) {
|
||
|
|
color = Color.YELLOW;
|
||
|
|
}
|
||
|
|
// Définir la couleur de fond du panneau
|
||
|
|
panel.setBackground(color);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void main(String[] args) {
|
||
|
|
new Radio();
|
||
|
|
}
|
||
|
|
}
|