Menu bar : Différence entre versions

De Wiki
Aller à : navigation, rechercher
(Page créée avec « TBW ... Return to the introduction ↑ Go to the next page → »)
 
 
(5 révisions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
TBW ...
+
As for a “classical” <font color=#FF8C00 title="Grahical User Interface>GUI</font>, <font color=#556B2F>'''GENIUS'''</font> proposes to have a main bar menu with the [{{PathCurrentJavaDoc}}/fr/cnes/genius/highLevel/GMenuBar.html GMenuBar] class (on the same principle as the swing <font color=#FF8C00>JMenuBar</font>).
 +
 
 +
First, we will have to create a class that extends [{{PathCurrentJavaDoc}}/fr/cnes/genius/lowLevel/GPanel.html GPanel]
 +
 
 +
<syntaxhighlight lang="java">
 +
class myPanel extends GPanel {
 +
 
 +
  myPanel () {
 +
  }
 +
 
 +
}
 +
</syntaxhighlight>
 +
 
 +
In the constructor of this class, we will create the different items of the menu(s) using the [{{PathCurrentJavaDoc}}/fr/cnes/genius/highLevel/GMenuItem.html GMenuItem] class.
 +
 
 +
<syntaxhighlight lang="java">
 +
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("?");
 +
   
 +
  }
 +
 
 +
}
 +
</syntaxhighlight>
 +
 
 +
Then, we will create the different menu(s) present in the [{{PathCurrentJavaDoc}}/fr/cnes/genius/highLevel/GMenuBar.html GMenuBar] using the [{{PathCurrentJavaDoc}}/fr/cnes/genius/highLevel/GMenu.html GMenu] class and associate items to this menu(s) using the <font color=#4169E1>add()</font> method.
 +
 
 +
<syntaxhighlight lang="java">
 +
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);
 +
   
 +
  }
 +
 
 +
}
 +
</syntaxhighlight>
 +
 
 +
We will initialize the menubar using the [{{PathCurrentJavaDoc}}/fr/cnes/genius/highLevel/GMenuBar.html GMenuBar] class, associate menu(s) to the menubar using the <font color=#4169E1>add()</font> method and create a <font color=#4169E1>getMenuBar()</font> method that will return it.
 +
 
 +
<syntaxhighlight lang="java">
 +
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; }
 +
 
 +
}
 +
</syntaxhighlight>
 +
 
 +
The [{{PathCurrentJavaDoc}}/fr/cnes/genius/highLevel/GMenuBar.html GMenuBar] will be associated to the main frame as this:
 +
 
 +
<syntaxhighlight lang="java">
 +
myPanel pan = new MyPanel();
 +
GFrame frame = new GFrame("Test GChoice", pan, pan.getBar());
 +
</syntaxhighlight>
 +
 
 +
 
 +
[[File:MenuBar.jpg]]
 +
 
 +
 
 +
At last, we will check the action on this menu bar using the [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GListener.html GListener] interface:
 +
 
 +
<syntaxhighlight lang="java">
 +
public void after(GEvent e) throws Exception {
 +
 
 +
  if (e.contains(itemQuit) ){
 +
    System.exit(0);
 +
  }
 +
 
 +
}
 +
</syntaxhighlight>
 +
 
 +
Note that since the V1.2 version, it is now possible to get several menu levels.
 +
 
 +
<syntaxhighlight lang="java">
 +
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);
 +
</syntaxhighlight>
 +
 
 +
 
 +
[[File:MenuBarLevels.jpg]]
 +
 
  
 
[[WELCOME_TO_THE_GENIUS_WIKI|Return to the introduction ↑]]  
 
[[WELCOME_TO_THE_GENIUS_WIKI|Return to the introduction ↑]]  
 
[[Icons|Go to the next page →]]
 
[[Icons|Go to the next page →]]

Version actuelle en date du 10 juillet 2017 à 07:57

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 →