/* Solution l'exercice -2- Affichage d'un cadran téléphonique Auteur: J-M Farinone Modifications: +) conversion AWT vers Swing. +) ajout constructeur appliTel */ import java.awt.*; import java.awt.event.*; //import javax.swing.*; public class appliTelAWT extends Frame { TextField tf; public appliTelAWT(int l, int h, String titre) { setSize(l,h); setTitle(titre); addWindowListener(new WindowAdapter () { public void windowClosing (WindowEvent e) {System.exit(0);} }); } public static void main(String args[]) { appliTelAWT ref = new appliTelAWT(250,350,"Un cadran téléphonique"); ref.affiche(); ref.pack(); ref.show(); } void affiche() { tf = new TextField(" ", 25); tf.setEditable(false); setLayout(new BorderLayout()); add("North", tf); Cadran cadran = new Cadran(tf); add("Center", cadran); } } class Cadran extends Panel implements ActionListener { TextField le_tf; Button buttons[] = new Button[12]; String chaine; Cadran(TextField tf) { le_tf = tf; super.setLayout(new GridLayout(4,3)); super.add(buttons[0] = new Button("0")); buttons[0].addActionListener(this); super.add(buttons[1] = new Button("1")); buttons[1].addActionListener(this); super.add(buttons[2] = new Button("2")); buttons[2].addActionListener(this); super.add(buttons[3] = new Button("3")); buttons[3].addActionListener(this); super.add(buttons[4] = new Button("4")); buttons[4].addActionListener(this); super.add(buttons[5] = new Button("5")); buttons[5].addActionListener(this); super.add(buttons[6] = new Button("6")); buttons[6].addActionListener(this); super.add(buttons[7] = new Button("7")); buttons[7].addActionListener(this); super.add(buttons[8] = new Button("8")); buttons[8].addActionListener(this); super.add(buttons[9] = new Button("9")); buttons[9].addActionListener(this); super.add(buttons[10] = new Button("Bis")); buttons[10].addActionListener(this); super.add(buttons[11] = new Button("Reset")); buttons[11].addActionListener(this); } public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if (obj == buttons[11]) le_tf.setText(" "); else if (obj == buttons[10]) le_tf.setText(chaine); else { chaine = le_tf.getText() + e.getActionCommand(); le_tf.setText(chaine); } } }