Как заставить элемент JMenu делать что-то при щелчке по нему

Я делаю графический интерфейс с Jmenu; у него есть элементы jmenu, которые будут выполнять действия при нажатии. Это проблема. Я смотрел и смотрел, но я не могу понять, как заставить его что-то делать при нажатии. Кроме того, я немного нуб, поэтому, если бы вы могли сделать это довольно простым способом, это было бы здорово!

Вот код:

import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

public abstract class windowMaker extends JFrame implements ActionListener {
private JMenu menuFile;

public static void main(String[] args) {
    createWindow();

}

public static void createWindow() {
    JFrame frame = new JFrame();
    frame.setTitle("*Game Title* Beta 0.0.1");
    frame.setSize(600, 400);
    frame.setLocation(100, 100);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setJMenuBar(windowMaker.menuBarCreator());
    frame.add(windowMaker.setTitle());
}

public static void launchURL(String s) {
    String s1 = System.getProperty("os.name");
    try {

        if (s1.startsWith("Windows")) {
            Runtime.getRuntime()
                    .exec((new StringBuilder())
                            .append("rundll32   url.dll,FileProtocolHandler ")
                            .append(s).toString());
        } else {
            String as[] = { "firefox", "opera", "konqueror",   "epiphany",
                    "mozilla", "netscape" };
            String s2 = null;
            for (int i = 0; i < as.length && s2 == null; i++)
                if (Runtime.getRuntime()
                        .exec(new String[] { "which", as[i]   }).waitFor() == 0)
                    s2 = as[i];

            if (s2 == null)
                throw new Exception("Could not find web browser");
            Runtime.getRuntime().exec(new String[] { s2, s });
        }
    } catch (Exception exception) {
        System.out
                .println("An error occured while trying to open the            web browser!\n");
    }
}

public static  JMenuBar menuBarCreator() {
    // create the menu parts
    JMenuBar menuBar = new JMenuBar();
    JMenu menuFile = new JMenu("File");
    JMenu menuHelp = new JMenu("Help");
    JMenuItem menuFileWebsite = new JMenuItem("Website");
    JMenuItem menuFileExit = new JMenuItem("Exit");
    JMenuItem menuHelpRules = new JMenuItem("Rules");
    JMenuItem menuHelpAbout = new JMenuItem("About");
    JMenuItem menuHelpHow = new JMenuItem("How To Play");

    // make the shortcuts for the items
    menuFile.setMnemonic(KeyEvent.VK_F);
    menuHelp.setMnemonic(KeyEvent.VK_H);

    // put the menu parts with eachother
    menuBar.add(menuFile);
    menuBar.add(menuHelp);
    menuFile.add(menuFileWebsite);
    menuFile.add(menuFileExit);
    menuHelp.add(menuHelpRules);
    menuHelp.add(menuHelpAbout);
    menuHelp.add(menuHelpHow);


    return menuBar;
}

public static Component setTitle() {
    JLabel title = new JLabel("Welcome To *the game*");
    title.setVerticalAlignment(JLabel.TOP);
    title.setHorizontalAlignment(JLabel.CENTER);
    return title;
}

}

Кстати: я хочу, чтобы опция веб-сайта (давайте пока поработаем с ней) использовала метод launchURL; Я знаю, что один работает.

13
задан mKorbel 19 March 2012 в 22:05
поделиться