I have recently start learning Java as my next language and I am getting stuck on the menu structure. In Python the order the menu items are defined is the order the menu items appear however in Java AWT I am having problems with creating the sub-menu item as my 1st item.
Even though I .add()
the sub-menu items first they still appear as the last menu item. I have been looking but I am not finding any method of telling the menu to have the sub-menu be the first listed menu item.
Is this possible within AWT?
Here is the code I have showing an example of my problem. My class is names TestClass.java
.
import java.awt.*;
import java.awt.event.*;
public class TestClass extends Frame{
/**
*
*/
private static final long serialVersionUID = 1L;
public TestClass () {
setLayout(new FlowLayout());
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("New");
MenuItem i1=new MenuItem("Sub menu 1");
MenuItem i2=new MenuItem("Sub menu 2");
MenuItem i3=new MenuItem("Exit");
submenu.add(i1);
submenu.add(i2);
menu.add(i3);
menu.add(submenu);
mb.add(menu);
setMenuBar(mb);
setSize(100,100);
setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
);
}
public static void main(String[] args) {
new TestClass();
}
}