当前位置: 技术问答>java相关
这样的方法怎么写?送500分
来源: 互联网 发布时间:2015-04-25
本文导语: 各位: 我想写着这样的两个方法,请问怎么能实现: public static void makeTopMenu(菜单名称,菜单字符串,菜单快捷键) public static void makesubMenu(上级菜单名,本菜单名,菜单字符串,菜单快捷键,菜...
各位:
我想写着这样的两个方法,请问怎么能实现:
public static void makeTopMenu(菜单名称,菜单字符串,菜单快捷键)
public static void makesubMenu(上级菜单名,本菜单名,菜单字符串,菜单快捷键,菜单图标)
比如:makeTopMenu(filemenu,"File",null)
makesubMenu(filemenu,newfile,"New",null,null)
我想在Frame中直接调用这两个方法就能实现菜单的建立
请问各位高手,怎么实现这样一个方法?
我想写着这样的两个方法,请问怎么能实现:
public static void makeTopMenu(菜单名称,菜单字符串,菜单快捷键)
public static void makesubMenu(上级菜单名,本菜单名,菜单字符串,菜单快捷键,菜单图标)
比如:makeTopMenu(filemenu,"File",null)
makesubMenu(filemenu,newfile,"New",null,null)
我想在Frame中直接调用这两个方法就能实现菜单的建立
请问各位高手,怎么实现这样一个方法?
|
真给分吗?呵呵,每个菜单以菜单名作为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
-------------------------------------------
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