真给分吗?呵呵,每个菜单以菜单名作为Action_Command,不能重复,作为查找的索引。
-------------------------------------------
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Test extends JFrame{
static JMenu target=null;
//添加Menu,包括快捷键
public static void makeMenu(JFrame frame,String parentMenuName,String menuString,int shortCut,Icon icon){
target=null;
if(parentMenuName==null){
frame.getJMenuBar().add(new MyMenu(new MyAction(menuString),menuString,shortCut,icon));
return;
}
iteratorMenu(frame.getJMenuBar(),parentMenuName);
if(target!=null)
((JMenu)target).add(new MyMenu(new MyAction(menuString),menuString,shortCut,icon));
}
//添加Menu
public static void makeMenu(JFrame frame,String parentMenuName,String menuString,Icon icon){
target=null;
if(parentMenuName==null){
frame.getJMenuBar().add(new MyMenu(new MyAction(menuString),menuString,icon));
return;
}
iteratorMenu(frame.getJMenuBar(),parentMenuName);
if(target!=null)
((JMenu)target).add(new MyMenu(new MyAction(menuString),menuString,icon));
}
//添加MenuItem
public static void makeMenuItem(JFrame frame,String parentMenuName,String menuItemString,KeyStroke shortCut,Icon icon){
target=null;
if(parentMenuName==null){
return;
}
iteratorMenu(frame.getJMenuBar(),parentMenuName);
if(target!=null)
((JMenu)target).add(new MyMenuItem(new MyAction(menuItemString),menuItemString,shortCut,icon));
}
//查找匹配Menu
public static void iteratorMenu(MenuElement menu,String menuName){
MenuElement[] menuElements=null;
if(menu instanceof JMenu&&((JMenu)menu).getActionCommand().equals(menuName)){
target=(JMenu)menu;
return;}
if((menuElements=menu.getSubElements()).length>0){
for(int i=0;i<menuElements.length;i++){
iteratorMenu(menuElements[i],menuName);}
}
}
Test(){
this.setJMenuBar(new JMenuBar());
makeMenu(this,null,"菜单1",KeyEvent.VK_1,null);
makeMenu(this,null,"菜单2",KeyEvent.VK_2,new ImageIcon("./LEAF.gif"));
makeMenu(this,null,"菜单3",KeyEvent.VK_3,null);
makeMenu(this,"菜单1","一级菜单1",null);
makeMenu(this,"菜单2","一级菜单2",new ImageIcon("./LEAF.gif"));
makeMenu(this,"一级菜单2","二级菜单2",null);
makeMenu(this,"二级菜单2","三级菜单1",null);
makeMenuItem(this,"菜单3","菜单项1",KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.ALT_MASK),new ImageIcon("./LEAF.gif"));
makeMenuItem(this,"三级菜单1","菜单项2",KeyStroke.getKeyStroke(KeyEvent.VK_Y,KeyEvent.ALT_MASK),null);
setSize(300,200); }
public static void main(String args[]){
new Test().show();
}
}
//扩展Action,处理菜单项事件
class MyAction extends AbstractAction{
public MyAction(String title){
super(title);
}
public void actionPerformed(ActionEvent e){
System.out.println(((JMenuItem)e.getSource()).getActionCommand());
}
}
class MyMenu extends JMenu{
MyMenu(Action e,String menuName,int shortCut,Icon icon){
super(e);
this.setActionCommand(menuName);
this.setMnemonic(shortCut);
this.setIcon(icon);
}
MyMenu(Action e,String menuName,Icon icon){
super(e);
this.setActionCommand(menuName);
this.setIcon(icon);
}}
class MyMenuItem extends JMenuItem{
MyMenuItem(Action e,String menuName,KeyStroke shortCut,Icon icon){
super(e);
this.setActionCommand(menuName);
this.setAccelerator(shortCut);
this.setIcon(icon); }
}
-------------------------------------------
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Test extends JFrame{
static JMenu target=null;
//添加Menu,包括快捷键
public static void makeMenu(JFrame frame,String parentMenuName,String menuString,int shortCut,Icon icon){
target=null;
if(parentMenuName==null){
frame.getJMenuBar().add(new MyMenu(new MyAction(menuString),menuString,shortCut,icon));
return;
}
iteratorMenu(frame.getJMenuBar(),parentMenuName);
if(target!=null)
((JMenu)target).add(new MyMenu(new MyAction(menuString),menuString,shortCut,icon));
}
//添加Menu
public static void makeMenu(JFrame frame,String parentMenuName,String menuString,Icon icon){
target=null;
if(parentMenuName==null){
frame.getJMenuBar().add(new MyMenu(new MyAction(menuString),menuString,icon));
return;
}
iteratorMenu(frame.getJMenuBar(),parentMenuName);
if(target!=null)
((JMenu)target).add(new MyMenu(new MyAction(menuString),menuString,icon));
}
//添加MenuItem
public static void makeMenuItem(JFrame frame,String parentMenuName,String menuItemString,KeyStroke shortCut,Icon icon){
target=null;
if(parentMenuName==null){
return;
}
iteratorMenu(frame.getJMenuBar(),parentMenuName);
if(target!=null)
((JMenu)target).add(new MyMenuItem(new MyAction(menuItemString),menuItemString,shortCut,icon));
}
//查找匹配Menu
public static void iteratorMenu(MenuElement menu,String menuName){
MenuElement[] menuElements=null;
if(menu instanceof JMenu&&((JMenu)menu).getActionCommand().equals(menuName)){
target=(JMenu)menu;
return;}
if((menuElements=menu.getSubElements()).length>0){
for(int i=0;i<menuElements.length;i++){
iteratorMenu(menuElements[i],menuName);}
}
}
Test(){
this.setJMenuBar(new JMenuBar());
makeMenu(this,null,"菜单1",KeyEvent.VK_1,null);
makeMenu(this,null,"菜单2",KeyEvent.VK_2,new ImageIcon("./LEAF.gif"));
makeMenu(this,null,"菜单3",KeyEvent.VK_3,null);
makeMenu(this,"菜单1","一级菜单1",null);
makeMenu(this,"菜单2","一级菜单2",new ImageIcon("./LEAF.gif"));
makeMenu(this,"一级菜单2","二级菜单2",null);
makeMenu(this,"二级菜单2","三级菜单1",null);
makeMenuItem(this,"菜单3","菜单项1",KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.ALT_MASK),new ImageIcon("./LEAF.gif"));
makeMenuItem(this,"三级菜单1","菜单项2",KeyStroke.getKeyStroke(KeyEvent.VK_Y,KeyEvent.ALT_MASK),null);
setSize(300,200); }
public static void main(String args[]){
new Test().show();
}
}
//扩展Action,处理菜单项事件
class MyAction extends AbstractAction{
public MyAction(String title){
super(title);
}
public void actionPerformed(ActionEvent e){
System.out.println(((JMenuItem)e.getSource()).getActionCommand());
}
}
class MyMenu extends JMenu{
MyMenu(Action e,String menuName,int shortCut,Icon icon){
super(e);
this.setActionCommand(menuName);
this.setMnemonic(shortCut);
this.setIcon(icon);
}
MyMenu(Action e,String menuName,Icon icon){
super(e);
this.setActionCommand(menuName);
this.setIcon(icon);
}}
class MyMenuItem extends JMenuItem{
MyMenuItem(Action e,String menuName,KeyStroke shortCut,Icon icon){
super(e);
this.setActionCommand(menuName);
this.setAccelerator(shortCut);
this.setIcon(icon); }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货