Menu bar

De Wiki
Aller à : navigation, rechercher

As for a “classical” GUI, GENIUS proposes to have a main bar menu with the GMenuBar class (on the same principle as the swing JMenuBar).

First, we will have to create a class that extends GPanel

class myPanel extends GPanel {
 
  myPanel () {
  }
 
}

In the constructor of this class, we will create the different items of the menu(s) using the GMenuItem class.

class myPanel extends GPanel {
 
  private GMenuItem itemLoad ;
  private GMenuItem itemSave;
  private GMenuItem itemQuit;
  private GMenuItem itemHelp;
 
  myPanel () {
 
    // We create menu items
    itemLoad = new GMenuItem("Load");
    itemSave = new GMenuItem("Save");
    itemQuit = new GMenuItem("Quit");
    itemHelp = new GMenuItem("?");
 
  }
 
}

Then, we will create the different menu(s) present in the GMenuBar using the GMenu class and associate items to this menu(s) using the add() method.

class myPanel extends GPanel {
 
  private GMenuItem itemLoad ;
  private GMenuItem itemSave;
  private GMenuItem itemQuit;
  private GMenuItem itemHelp;
 
  private GMenu fileMenu;
  private GMenu aboutMenu;
 
  myPanel () {
 
    // We create menu items
    itemLoad = new GMenuItem("Load");
    itemSave = new GMenuItem("Save");
    itemQuit = new GMenuItem("Quit");
    itemHelp = new GMenuItem("?");
 
    // We create the “File” and the "About" menu containing the previous items
    fileMenu= new GMenu("File");
    aboutMenu= new GMenu("About");
 
    // We link items to menus
    fileMenu.add(itemLoad);
    fileMenu.add(itemWrite);
    fileMenu.add(itemQuit);
    aboutMenu.add(itemHelp);
 
  }
 
}

We will initialize the menubar using the GMenuBar class, associate menu(s) to the menubar using the add() method and create a getMenuBar() method that will return it.

class myPanel extends GPanel {
 
  private GMenuItem itemLoad ;
  private GMenuItem itemSave;
  private GMenuItem itemQuit;
  private GMenuItem itemHelp;
 
  private GMenu fileMenu;
  private GMenu aboutMenu;
 
  private GMenuBar bar;
 
  myPanel () {
 
    // We create menu items
    itemLoad = new GMenuItem("Load");
    itemSave = new GMenuItem("Save");
    itemQuit = new GMenuItem("Quit");
    itemHelp = new GMenuItem("?");
 
    // We create the “File” and the "About" menu containing the previous items
    fileMenu= new GMenu("File");
    aboutMenu= new GMenu("About");
 
    // We link items to menus
    fileMenu.add(itemLoad);
    fileMenu.add(itemWrite);
    fileMenu.add(itemQuit);
    aboutMenu.add(itemHelp);
 
    // Menu Bar initialization
    bar = new GMenuBar(this);
    bar.add(fileMenu);
    bar.add(aboutMenu);
 
  }
 
  public GMenuBar getBar() { return bar; }
 
}

The GMenuBar will be associated to the main frame as this:

myPanel pan = new MyPanel();
GFrame frame = new GFrame("Test GChoice", pan, pan.getBar());


MenuBar.jpg


At last, we will check the action on this menu bar using the GListener interface:

public void after(GEvent e) throws Exception {
 
  if (e.contains(itemQuit) ){
    System.exit(0);
  }
 
}

Note that since the V1.2 version, it is now possible to get several menu levels.

level1Menu = new GMenu("level 1");
itemLevel11 = new GMenuItem("level 1-1");
itemLevel12 = new GMenuItem("level 1-2");
level1Menu.add(itemLevel11);
level1Menu.add(itemLevel12);
 
// FileMenu initialization
level0Menu = new GMenu("Level 0");
level0Menu.add(level1Menu);


MenuBarLevels.jpg


Return to the introduction ↑ Go to the next page →