45 lines
1.2 KiB
Java
45 lines
1.2 KiB
Java
import java.awt.*;
|
|
import javax.swing.*;
|
|
import java.awt.event.*;
|
|
|
|
class Observer implements ActionListener {
|
|
public Observer() {}
|
|
|
|
public void actionPerformed(ActionEvent evt) {
|
|
String input = evt.getActionCommand().toLowerCase().trim();
|
|
JTextField textField = (JTextField)evt.getSource();
|
|
JPanel panel = (JPanel)textField.getParent();
|
|
|
|
if (input.equals("cyan")) panel.setBackground(Color.CYAN);
|
|
else if (input.equals("jaune")) panel.setBackground(Color.YELLOW);
|
|
else if (input.equals("magenta")) panel.setBackground(Color.MAGENTA);
|
|
|
|
textField.setText("");
|
|
}
|
|
}
|
|
|
|
public class Commande {
|
|
|
|
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);
|
|
|
|
Observer observer = new Observer();
|
|
|
|
JTextField t = new JTextField();
|
|
t.setLocation(0, 0);
|
|
t.setSize(200, 20);
|
|
t.addActionListener(observer);
|
|
|
|
JPanel p = new JPanel();
|
|
p.setSize(200, 200);
|
|
p.setLocation(0, 0);
|
|
p.setLayout(null);
|
|
|
|
f.add(p);
|
|
p.add(t);
|
|
f.setVisible(true);
|
|
}
|
|
} |