Submenu.java Source code

Java tutorial

Introduction

Here is the source code for Submenu.java

Source

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class Submenu {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        JMenuBar menubar = new JMenuBar();
        ImageIcon iconNew = new ImageIcon("new.png");
        ImageIcon iconOpen = new ImageIcon("open.png");
        ImageIcon iconSave = new ImageIcon("save.png");
        ImageIcon iconClose = new ImageIcon("exit.png");

        JMenu file = new JMenu("File");
        JMenu imp = new JMenu("Import");

        JMenuItem fileNew = new JMenuItem("New", iconNew);
        JMenuItem fileOpen = new JMenuItem("Open", iconOpen);
        JMenuItem fileSave = new JMenuItem("Save", iconSave);
        JMenuItem fileClose = new JMenuItem("Close", iconClose);

        file.setMnemonic(KeyEvent.VK_F);
        imp.setMnemonic(KeyEvent.VK_M);

        JMenuItem newsf = new JMenuItem("Import newsfeed list...");
        JMenuItem bookm = new JMenuItem("Import bookmarks...");
        JMenuItem mail = new JMenuItem("Import mail...");

        imp.add(newsf);
        imp.add(bookm);
        imp.add(mail);

        fileNew.setMnemonic(KeyEvent.VK_N);
        fileNew.setMnemonic(KeyEvent.VK_O);
        fileSave.setMnemonic(KeyEvent.VK_S);

        fileClose.setMnemonic(KeyEvent.VK_C);
        fileClose.setToolTipText("Exit application");
        fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));

        fileClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });

        file.add(fileNew);
        file.add(fileOpen);
        file.add(fileSave);
        file.addSeparator();
        file.add(imp);
        file.addSeparator();
        file.add(fileClose);

        menubar.add(file);

        f.setJMenuBar(menubar);

        f.setSize(360, 250);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}