请问这程序为什么会出现这样的情况?
为什么我按了four后,不会输出four?
如果说菜单在菜单里就不行的话,那为什么preview和setting就可以 ? 难道多层就不可以了 ?
这个程序如何让four可以有像preview和setting他们这样的效果 ?
package tmp;
import java.awt.*;
import java.awt.event.*;
class TestMenuBar extends Frame{
MenuBar mb = new MenuBar();
Menu fileM = new Menu("File");
Menu editM = new Menu("Edit");
Menu toolsM = new Menu("Tools");
Menu helpM = new Menu("help");
MenuItem file1 = new MenuItem("New");
MenuItem file2 = new MenuItem("Open");
MenuItem file3 = new MenuItem("Save");
Menu file4 = new Menu("Print");
MenuItem file41 = new MenuItem("preview");
MenuItem file42 = new MenuItem("setting");
CheckboxMenuItem cm = new CheckboxMenuItem("Quit",true);
Menu one = new Menu("one");
Menu two = new Menu("Two");
Menu three = new Menu("Three");
MenuItem four = new MenuItem("Four");
public TestMenuBar(){
mb.add(fileM);
mb.add(editM);
mb.add(toolsM);
mb.add(helpM);
file4.add(file41);
file4.add(file42);
AL A = new AL();
file41.addActionListener(A);
file42.addActionListener(A);
four.addActionListener(A);
three.add(four);
two.add(three);
one.add(two);
fileM.add(file1);
fileM.add(file2);
fileM.add(file3);
fileM.add(file4);
fileM.addSeparator();
fileM.add(cm);
fileM.add(one);
this.setMenuBar(mb);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String arga[]){
TestMenuBar tmb = new TestMenuBar();
tmb.setSize(500,350);
tmb.setVisible(true);
}
}
class AL implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("preview")){
System.out.println("Preview");
} else if (e.getActionCommand().equals("setting")){
System.out.println("Setting");
} else if (e.getActionCommand().equals("four")){ // 这里将four改成Four也不行 ?
System.out.println("Four");
}
}
}