请问我用如下方法显示菜单怎么不行?
JFrame frame=new JFrame();
frame.setSize(300,400);
JMenuBar jmb=new JMenuBar();
frame.setMenuBar(jmb);
frame.show();
JFrame frame=new JFrame();
frame.setSize(300,400);
JMenuBar jmb=new JMenuBar();
frame.setMenuBar(jmb);
frame.show();
import javax.swing.*;public class Menutest extends JApplet
{
public void init()
{
JMenuBar mb=new JMenuBar();
JMenu fileMenu=new JMenu("显示");
JMenu pullRightMenu=new JMenu("问好");
fileMenu.add("欢迎");
fileMenu.addSeparator();
fileMenu.add(pullRightMenu);
fileMenu.add("退出");
pullRightMenu.add(new JCheckBoxMenuItem("早上好"));
pullRightMenu.add(new JCheckBoxMenuItem("下午好"));
pullRightMenu.add(new JCheckBoxMenuItem("晚安,再见"));
mb.add(fileMenu);
setJMenuBar(mb);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;public class IntroExample extends JMenuBar {
String[] fileItems = new String[] {"New", "Open", "Save", "Exit"};
String[] editItems = new String[] {"Undo", "Cut", "Copy", "Paste"};
char[] fileShortcuts = {'N', 'O', 'S', 'X'};
char[] editShortcuts = {'Z', 'X', 'C', 'V'}; public IntroExample() {
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu otherMenu = new JMenu("Other");
JMenu subMenu = new JMenu("SubMenu");
JMenu subMenu2 = new JMenu("SubMenu2"); // Assemble the File menus with mnemonics.
ActionListener printListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("Menu item [" + event.getActionCommand()
+ "] was pressed.");
}
};
for (int i = 0; i < fileItems.length; i++) {
JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]);
item.addActionListener(printListener);
fileMenu.add(item);
} // Assemble the File menus with keyboard accelerators.
for (int i = 0; i < editItems.length; i++) {
JMenuItem item = new JMenuItem(editItems[i]);
item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i], Toolkit
.getDefaultToolkit().getMenuShortcutKeyMask(), false));
item.addActionListener(printListener);
editMenu.add(item);
} // Insert a separator in the Edit menu in Position 1 after "Undo".
editMenu.insertSeparator(1); // Assemble the submenus of the Other menu.
JMenuItem item;
subMenu2.add(item = new JMenuItem("Extra 2"));
item.addActionListener(printListener);
subMenu.add(item = new JMenuItem("Extra 1"));
item.addActionListener(printListener);
subMenu.add(subMenu2); // Assemble the Other menu itself.
otherMenu.add(subMenu);
otherMenu.add(item = new JCheckBoxMenuItem("Check Me"));
item.addActionListener(printListener);
otherMenu.addSeparator();
ButtonGroup buttonGroup = new ButtonGroup();
otherMenu.add(item = new JRadioButtonMenuItem("Radio 1"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.add(item = new JRadioButtonMenuItem("Radio 2"));
item.addActionListener(printListener);
buttonGroup.add(item);
otherMenu.addSeparator();
item = new JMenuItem("Potted Plant", new ImageIcon("resource/bye.gif"));
item.setHorizontalTextPosition(SwingConstants.LEFT);
otherMenu.add(item);
item.addActionListener(printListener); // Finally, add all the menus to the menu bar.
fileMenu.setMinimumSize(fileMenu.getPreferredSize());
editMenu.setMinimumSize(editMenu.getPreferredSize());
otherMenu.setMinimumSize(otherMenu.getPreferredSize());
add(fileMenu);
add(editMenu);
add(otherMenu);
add(Box.createHorizontalGlue());
add(new JLabel("ChangePWD"));
add(Box.createHorizontalStrut(8));
add(new JLabel("Logout"));
add(Box.createHorizontalStrut(8));
add(new JLabel("Exit"));
add(new JLabel(new ImageIcon("resource/bye.gif")));
add(Box.createHorizontalStrut(12));
} public static void main(String[] args) {
JFrame frame = new JFrame("Simple Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(new IntroExample());
frame.pack();
frame.setVisible(true);
}
}