Saturday, 4 November 2023

Write a java program to design following Frame using Swing.-using menu bar-core java slip 23

 

Write a java program to design following Frame using Swing.

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

public class SwingMenuExample {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Menu Example");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(400, 300);

 

        // Create a menu bar

        JMenuBar menuBar = new JMenuBar();

 

        // Create "File" menu

        JMenu fileMenu = new JMenu("File");

        JMenuItem exitMenuItem = new JMenuItem("Exit");

        exitMenuItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                System.exit(0);

            }

        });

        fileMenu.add(exitMenuItem);

 

        // Create "Edit" menu with submenus

        JMenu editMenu = new JMenu("Edit");

        JMenuItem undoMenuItem = new JMenuItem("Undo");

        JMenuItem redoMenuItem = new JMenuItem("Redo");

        JMenuItem cutMenuItem = new JMenuItem("Cut");

        JMenuItem copyMenuItem = new JMenuItem("Copy");

        JMenuItem pasteMenuItem = new JMenuItem("Paste");

        editMenu.add(undoMenuItem);

        editMenu.add(redoMenuItem);

        editMenu.addSeparator(); // Separator

        editMenu.add(cutMenuItem);

        editMenu.add(copyMenuItem);

        editMenu.add(pasteMenuItem);

 

        // Create "Search" menu

        JMenu searchMenu = new JMenu("Search");

 

        // Add menus to the menu bar

        menuBar.add(fileMenu);

        menuBar.add(editMenu);

        menuBar.add(searchMenu);

 

        frame.setJMenuBar(menuBar);

 

        frame.setVisible(true);

    }

}