import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TipProg extends JFrame implements ActionListener { JLabel statusLine; JTextArea textArea; TipButton b1, b2, b3, b4; public static void main(String argv[]) { TipProg toplevel = new TipProg(); // initialiser la disposition et rendre visible toplevel.pack(); toplevel.show(); } public TipProg() { Container contentPane = getContentPane(); JPanel toolbar = new JPanel(); // Layout par defaut de contentPane: BorderLayout = disposition de type // "points cardinaux". le toolbar est placé au nord contentPane.add(BorderLayout.NORTH, toolbar); // Remarque: // on associe le meme ActionListener a tous les boutons // on pourrait aussi associer un Listener different pour chaque bouton b1 = new TipButton("New", "Cree un nouveau document"); b1.addActionListener(this); toolbar.add(b1); b2 = new TipButton("Open", "Ouvre un document"); b2.addActionListener(this); toolbar.add(b2); b3 = new TipButton("Save", "Sauve le document courant"); b3.addActionListener(this); toolbar.add(b3); b4 = new TipButton("Exit"); b4.addActionListener(this); toolbar.add(b4); // Zone de texte au centre textArea = new JTextArea(10, 25); textArea.setBackground(Color.white); contentPane.add(BorderLayout.CENTER, textArea); // Status Line au sud statusLine = new JLabel(); contentPane.add(BorderLayout.SOUTH, statusLine); } // méthode appelée quand on clique sur un des boutons // comme le meme ActionListener a ete associe a tous les boutons // on determine quel bouton a ete appele en comparant a "source" public void actionPerformed(ActionEvent e) { int id = e.getID(); Object which_button = e.getSource(); if (which_button == b1) System.out.println("bouton 1"); else if (which_button == b2) System.out.println("bouton 2"); else if (which_button == b3) System.out.println("bouton 3"); else if (which_button == b4) { System.out.println("bouton 4"); System.exit(0); } } }