|
In this example, you will see, how to create menus in Swing. Menu bar contains a collection of menus. Here we create four menu that is: "File", "Edit", "View" and "Help". The complete program code and output is as follows:
Sample code (SwingJMenu.java):
import javax.swing.*;
public class SwingJMenu extends JFrame {
private JMenuBar menuBar = new JMenuBar();
public SwingJMenu(String title) {
setTitle(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu viewMenu = new JMenu("View");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(viewMenu);
menuBar.add(helpMenu);
}
public static void main(String[] args) {
SwingJMenu window = new SwingJMenu("Menu");
window.setBounds(30, 30, 300, 300);
window.setVisible(true);
}
} |
|
|