Practical No.
DEPARTMENT OF COMPUTER ENGINEERING
Subject: Client Side Scripting Subject Code: 22517
Semester: 5 Course: Advance java
Laboratory No: Name of Teacher: Pradeep Shirke
Name of Student: Jay Waigankar Roll ID: 22202B0010
XIII. Exercise
1. Find errors in following program and display output as shown below.
Errors-
1)haven't added the menu bar to the frame
2)Missing event handling
Correct code-
import java.awt.*;
import java.awt.event.*;
public class MenuDemo1 extends Frame {
MenuBar mb;
MenuItem m1, m2, m3, m4;
Menu mn;
MenuShortcut ms;
MenuDemo1() {
setTitle("MenuBar Demo");
setSize(500, 500);
setLayout(null);
Practical No. 5
ms = new MenuShortcut(KeyEvent.VK_X);
mn = new Menu("File");
mb = new MenuBar();
m1 = new MenuItem("New...");
m2 = new MenuItem("Open...");
m3 = new MenuItem("Save As...");
m4 = new MenuItem("Exit", ms);
mn.add(m1);
mn.add(m2);
mn.add(m3);
mn.addSeparator();
mn.add(m4);
mb.add(mn);
this.setMenuBar(mb); // Add menu bar to the frame
// Add event handling to menu items
m1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("New menu item clicked");
});
m2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Open menu item clicked");
});
Practical No. 5
m3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Save As menu item clicked");
);
m4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Exit menu item clicked");
System.exit(0); // Exit the application
});
public static void main(String[] args) {
MenuDemo1 md = new MenuDemo1();
md.setVisible(true);
Output-
Practical No. 5
XII. Practical Related Questions
1. Write the use of setEnabled() method
Ans-The setEnabled() method is used to enable or disable a menu item. When a menu item is
disabled, it appears grayed out and cannot be clicked.
Syntax- menuItem.setEnabled(boolean enabled);
2. Write the procedure to assign shortcut key to the Menu Item-
Ans- To assign a shortcut key to a menu item, you can use the setShortcut() method or create
a MenuShortcut object and pass it to the MenuItem constructor.
Syntax- menuItem.setShortcut(MenuShortcut shortcut);
3. Write a syntax and use of addSeparator() method
Ans- The addSeparator() method is used to add a separator line to a menu.
Syntax- menu.addSeparator();