Utilisation d'une barre de menu (JMenuBar)

/*
 * exemple.java
 *
 * Created on April 6, 2008, 8:42 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package exemple;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**

 *
 * @author gmonard
 */
public class exemple extends JFrame implements ActionListener
{
    JButton ok;
    JTextField message;
    JMenuBar menuprincipal;
    JMenu menu1;
    JMenu menu2;
    JMenuItem toto;
    JMenuItem titi;
    JMenuItem marcel;
    JMenuItem josette;
    JMenuItem gerard;

    /** Creates a new instance of exemple */
    public exemple()
    {
        super("Exemple d'utilisation des JMenuBar");

        this.setSize(400,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        BorderLayout agencement = new BorderLayout();
        this.setLayout(agencement);

        JPanel panneauHaut = new JPanel();
        JPanel panneauBas = new JPanel();
        JPanel panneauMilieu = new JPanel();

        /* une barre de menu (JMenuBar) contient des menus (JMenu)

         * qui sont eux-memes composes de sous-menus (JMenuItem)
         */

        menuprincipal = new JMenuBar();
        menu1 = new JMenu("Menu 1");
        menu2 = new JMenu("Menu 2");
        toto = new JMenuItem("Toto");
        titi = new JMenuItem("Titi");
        marcel = new JMenuItem("Marcel");
        josette = new JMenuItem("Josette");
        gerard = new JMenuItem("Gerard");

        toto.addActionListener(this);
        titi.addActionListener(this);
        marcel.addActionListener(this);
        josette.addActionListener(this);
        gerard.addActionListener(this);

        menu1.add(toto);
        menu1.add(titi);
        menu2.add(marcel);
        menu2.add(josette);
        menu2.add(gerard);
        menuprincipal.add(menu1);
        menuprincipal.add(menu2);

        // ATTENTION: pour rajouter le JMenuBar a la JFrame, il faut utiliser setJMenuBar
        this.setJMenuBar(menuprincipal);

        message = new JTextField("",30);
        message.setEditable(false);
        panneauMilieu.add(message);

        ok = new JButton("OK");
        ok.addActionListener(this);
        panneauBas.add(ok);

        this.add(panneauHaut, BorderLayout.NORTH);
        this.add(panneauMilieu, BorderLayout.CENTER);
        this.add(panneauBas, BorderLayout.SOUTH);

        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent evenement)
    {
        Object source = evenement.getSource();
        if (source == ok)
        {
            this.dispose();
        }
        else if (source == toto)
        { 
           message.setText("Menu Toto");
        }
        else if (source == titi)
        { 
           message.setText("Menu Titi");
        }
        else if (source == marcel)
        { 
           message.setText("Menu Marcel");
        }
        else if (source == josette)
        { 
           message.setText("Menu Josette");
        }
        else if (source == gerard)
        { 
           message.setText("Menu Gerard");
        }
        else
        {
            // rien
        }
    }

    public static void main(String[] arguments)
    {
        new exemple();
    }

}