import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LesMenus extends JFrame { JLabel statusline; JTextArea textArea; public static void main(String argv[]) { LesMenus toplevel = new LesMenus(); // initialiser la disposition et rendre visible toplevel.pack(); toplevel.show(); } public LesMenus() { setTitle("Les Menus"); Container contentPane = getContentPane(); //NB: Layout par defaut de contentPane: BorderLayout // creer la barre de menu JMenuBar menubar = new JMenuBar(); contentPane.add(BorderLayout.NORTH, menubar); // creer et ajouter les menus au MenuBar menubar.add(CreerFileMenu()); menubar.add(CreerEditMenu()); // Zone de texte (au "centre" de la fenêtre principale) textArea = new JTextArea(10, 25); textArea.setBackground(Color.white); contentPane.add(BorderLayout.CENTER, textArea); // "status line" statusline = new JLabel("Ready"); contentPane.add(BorderLayout.SOUTH, statusline); } JMenu CreerFileMenu() { JMenu menu = new JMenu("File"); JMenuItem open = new JMenuItem("Open"), save = new JMenuItem("Save"), exit = new JMenuItem("Exit"); menu.add(open); menu.add(save); menu.addSeparator(); menu.add(exit); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); statusline.setText(actionCommand); } }; // associer cet ActionListener aux MenuItems open.addActionListener(al); save.addActionListener(al); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); return menu; } JMenu CreerEditMenu() { JMenu menu = new JMenu("Edit"); JMenuItem undo = new JMenuItem("Undo"), cut = new JMenuItem("Cut"), copy = new JMenuItem("Copy"), paste = new JMenuItem("Paste"); menu.add(undo); menu.addSeparator(); menu.add(cut); menu.add(copy); menu.add(paste); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); statusline.setText(actionCommand); } }; // associer cet ActionListener aux MenuItems undo.addActionListener(al); cut.addActionListener(al); copy.addActionListener(al); paste.addActionListener(al); return menu; } }