Add Menus to JFrame - Java Swing

Java examples for Swing:Menu

Description

Add Menus to JFrame

Demo Code

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

public class Main extends JFrame {

  public Main() {
    super("FeedBar 2");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // create icons
    ImageIcon loadIcon = new ImageIcon("load.gif");
    ImageIcon saveIcon = new ImageIcon("save.gif");
    ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
    ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
    // create buttons
    JButton load = new JButton("Load", loadIcon);
    JButton save = new JButton("Save", saveIcon);
    JButton subscribe = new JButton("Subscribe", subscribeIcon);
    JButton unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
    // add buttons to toolbar
    JToolBar bar = new JToolBar();
    bar.add(load);//from   w  w  w .  j  ava  2s. c  o m
    bar.add(save);
    bar.add(subscribe);
    bar.add(unsubscribe);
    // create menu
    JMenuItem j1 = new JMenuItem("Load");
    JMenuItem j2 = new JMenuItem("Save");
    JMenuItem j3 = new JMenuItem("Subscribe");
    JMenuItem j4 = new JMenuItem("Unsubscribe");
    JMenuBar menubar = new JMenuBar();
    JMenu menu = new JMenu("Feeds");
    menu.add(j1);
    menu.add(j2);
    menu.addSeparator();
    menu.add(j3);
    menu.add(j4);
    menubar.add(menu);
    // prepare user interface
    JTextArea edit = new JTextArea(8, 40);
    JScrollPane scroll = new JScrollPane(edit);
    BorderLayout bord = new BorderLayout();
    setLayout(bord);
    add("North", bar);
    add("Center", scroll);
    setJMenuBar(menubar);
    pack();
    setVisible(true);
  }

  public static void main(String[] arguments) {
    Main frame = new Main();
  }
}

Related Tutorials