Create menus in a Frame

From Applets and Application chapter in PU/ Programming in Java

Asked on 2 Feb, 2021

Like 0 Dislike 276 Views

The menu must be of this structure:

file => open, close | quit

edit => cut, copy | paste

special => first, second, third

Add a comment

1 Answers

arjun adhikari on 2 Feb, 2021 Like 0 Dislike

import java.awt.* ;

class Menus extends Frame{
    Menus(){
        super("Menus Demo") ;
        MenuBar menuBar = new MenuBar() ;
        setMenuBar(menuBar) ;

// Lets make a multidimensinal array in which the first element
// in array is the name of Menu,and other 
// following elements are the MenuItems

        String[][] menus = {
            {"file", "open", "close", "-", "quit"},
            {"edit", "cut", "copy", "-", "paste"},
            {"special", "first", "second", "third"}
        };

        int i, j ;
        for(i = 0 ; i < 3; i++){
            Menu m = new Menu(menus[i][0]) ;
            for(j = 1 ; j < menus[i].length; j++){
                m.add(new MenuItem(menus[i][j])) ;
            }
            menuBar.add(m) ;
        }

        setSize(400, 400) ;
        setVisible(true) ;

    }
    public static void main(String[] args){
        new Menus() ;
    }
}

Output:

Add a comment


Add your answer