0% found this document useful (0 votes)
11 views2 pages

Java 4 Prac

The document describes how to create a menu bar with menus and menu items in Java. It shows how to add menu items and submenus to the menu bar. It also demonstrates how to create a checkable menu item and display a dialog box when clicked.

Uploaded by

baburaomusk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Java 4 Prac

The document describes how to create a menu bar with menus and menu items in Java. It shows how to add menu items and submenus to the menu bar. It also demonstrates how to create a checkable menu item and display a dialog box when clicked.

Uploaded by

baburaomusk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Program :

menuBar.add(fileMenu);
import javax.swing.*;
menuBar.add(editMenu);
import java.awt.*;
menuBar.add(optionsMenu);
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
setJMenuBar(menuBar);
public class MenuExample extends JFrame {

exitItem.addActionListener(new ActionListener()
public MenuExample() { {
setTitle("Menu Example"); public void actionPerformed(ActionEvent e) {
setSize(400, 300); System.exit(0);
setDefaultCloseOperation(EXIT_ON_CLOSE); }
JMenuBar menuBar = new JMenuBar(); })

checkItem.addActionListener(new
ActionListener() {
JMenu fileMenu = new JMenu("File");
public void actionPerformed(ActionEvent e) {
JMenuItem openItem = new
JMenuItem("Open"); JCheckBoxMenuItem menuItem =
(JCheckBoxMenuItem) e.getSource();
JMenuItem saveItem = new
JMenuItem("Save"); if (menuItem.isSelected()) {
JMenuItem exitItem = new JMenuItem("Exit");
JOptionPane.showMessageDialog(MenuExample.this,
fileMenu.add(openItem);
"Checkable item is checked.");
fileMenu.add(saveItem);
} else {
fileMenu.addSeparator();

fileMenu.add(exitItem); JOptionPane.showMessageDialog(MenuExample.this,
"Checkable item is unchecked.");

}
JMenu editMenu = new JMenu("Edit");
}
JMenuItem cutItem = new JMenuItem("Cut");
});
JMenuItem copyItem = new
JMenuItem("Copy"); }

JMenuItem pasteItem = new


JMenuItem("Paste");
public static void main(String[] args) {
editMenu.add(cutItem);
SwingUtilities.invokeLater(new Runnable() {
editMenu.add(copyItem);
public void run() {
editMenu.add(pasteItem);
new MenuExample().setVisible(true);

}
JMenu optionsMenu = new JMenu("Options");
});
JCheckBoxMenuItem checkItem = new
}
JCheckBoxMenuItem("Checkable Item");
}
optionsMenu.add(checkItem);

Practical 4
Output :

Conclusion :
In this Practical we understand that how to to create a
menu bar with various menu items and sub menu items & Also how to
create a checkable menu item while On clicking a menu Item display
a suitable Dialog box.
K S P A T

You might also like