JMenu



 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) {
        // TODO Auto-generated method stub
        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'));    // item 的快捷键 
        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);// 窗口添加 子菜单
    }
}

你可能感兴趣的:(java)