APL/APL2.1/TP10/Radio/Radio.java
2022-03-21 17:26:34 +01:00

54 lines
1.4 KiB
Java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Observer implements ActionListener {
public Observer() {
}
public void actionPerformed(ActionEvent evt) {
String name = evt.getActionCommand();
JRadioButton rb = (JRadioButton)evt.getSource();
JPanel f = (JPanel)rb.getParent();
if (name == "Jaune") {
f.setBackground(Color.YELLOW);
} else if (name == "Cyan") {
f.setBackground(Color.CYAN);
} else if (name == "Magenta") {
f.setBackground(Color.MAGENTA);
}
}
}
public class Radio {
public static void main(String[] args) {
JFrame f = new JFrame("Fond");
f.setSize(200, 200);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton b1 = new JRadioButton("Jaune");
JRadioButton b2 = new JRadioButton("Cyan");
JRadioButton b3 = new JRadioButton("Magenta");
Observer observer = new Observer();
ButtonGroup bg = new ButtonGroup();
bg.add(b1);
bg.add(b2);
bg.add(b3);
b1.addActionListener(observer);
b2.addActionListener(observer);
b3.addActionListener(observer);
JPanel p = new JPanel();
f.add(p);
p.add(b1);
p.add(b2);
p.add(b3);
f.setVisible(true);
}
}