public class Exampel {
public static void main(String[] args) {
WindowMenu win= new WindowMenu("带菜单的窗口",20,30,200,190);
}
}
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
public class WindowMenu extends JFrame{
JMenuBar menubar;
JMenu menu,subMenu;
JMenuItem item1,item2;
public WindowMenu(){};
public WindowMenu(String s,int x,int y,int w,int h){
this.init(s);
this.setLocation(x,y);
this.setSize(w,h);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
void init(String s) {
setTitle(s);
JMenuBar menubar = new JMenuBar();
menu = new JMenu("菜单");
subMenu =new JMenu("子菜单");
item1 = new JMenuItem("菜单项1",new ImageIcon("a.gif"));
item2 = new JMenuItem("菜单项2",new ImageIcon("b.gif"));
item1.setAccelerator(KeyStroke.getKeyStroke('A'));
item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));
menu.add(item1);
menu.addSeparator();
menu.add(item2);
menu.add(subMenu);
subMenu.add(new JMenuItem("子菜单里的菜单项",new ImageIcon("c.gif")));
menubar.add(menu);
this.setJMenuBar(menubar);
}
}