50 lines
1.3 KiB
Java
50 lines
1.3 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 Combinaison {
|
|
|
|
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();
|
|
|
|
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);
|
|
}
|
|
} |