本文主要是介绍[java]窗口中的菜单项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码:
caidan.java
public class caidan
{public static void main(String args[]){WindowMenu win = new WindowMenu("带菜单的窗口",20,30,200,190);}
}
import javax.swing.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import static javax.swing.JFrame.*;
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){init(s);setLocation(x,y);setSize(w,h);setVisible(true);setDefaultCloseOperation(DISPOSE_ON_CLOSE);}
void init(String s){setTitle(s);menubar = new JMenuBar();menu = new JMenu("菜单");subMenu= new JMenu("软件项目");item1 = new JMenuItem("java话题",new ImageIcon("1.bmp"));item2 = new JMenuItem("动画话题",new ImageIcon("2.bmp"));item1.setAccelerator(KeyStroke.getKeyStroke('A'));item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menu.add(subMenu);menu.add(item1);menu.addSeparator();menu.add(item2);subMenu.add(new JMenuItem("汽车销售系统",new ImageIcon("33.jpg")));subMenu.add(new JMenuItem("农场信息系统",new ImageIcon("4.bmp")));menubar.add(menu);setJMenuBar(menubar);
}
}
ps:原图有点大了
运行截图:
这篇关于[java]窗口中的菜单项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!