游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1350|回复: 0

带java反射的动态菜单生成类 wxh zt

[复制链接]

1367

主题

1993

帖子

2118

积分

金牌会员

Rank: 6Rank: 6

积分
2118
发表于 2005-8-7 19:13:00 | 显示全部楼层 |阅读模式
带java反射的动态菜单生成类  
简介 package src;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.lang.reflect.*;
/**
*
* @author ForTest
*/
public class createObjectMenu {

/** Creates a new instance of createObjectMenu */
public createObjectMenu() {
}

public JMenuBar createFunction(String user) throws SQLException{
Connection con=cc.getConn();
String sql="select mname,paren,mtype,mjb from Menu where user='"+user+"'and mtype='Menu';";
try{
Statement stmt= con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
if(rs.getInt("mjb")==1){
MenuBarTest.add(getUserMenu(user,rs.getString("mname")));
}
}
}catch(SQLException e){
System.err.println("createFunction:"+e.getMessage());
}
return MenuBarTest;
}

public JMenu getUserMenu(String user,String paren) throws SQLException{ //得到MENU子菜单
Connection con=cc.getConn();
String sql="select mname,mtype from Menu where user='"+user+"'and paren='"+paren+"';";
JMenu tmpMenu=new JMenu(paren);
try{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
if(rs.getString("mtype").equals("Item")){
tmpMenu.add(getUserItem(rs.getString("mname")));
}else{
tmpMenu.add(getUserMenu(user,rs.getString("mname")));
}
}
}catch(SQLException e){
System.err.println("getUserMenu:"+e.getMessage());
}
return tmpMenu;
}

public JMenuItem getUserItem(String mname){
JMenuItem tmpItem =new JMenuItem(mname);
tmpItem.addActionListener(tmpListener); //添加监听
return tmpItem;
}

ActionListener tmpListener = new ActionListener() { //创建一个监听器
public void actionPerformed(ActionEvent event) {
JMenuItem tmpItem=(JMenuItem)event.getSource();
String addAction=tmpItem.getText()+"Action";
getListener(addAction);
}
};

public void getListener(String methListener){ //java反射应用,通过函数名称来动态调用函数
try{
Class cls=Class.forName("src.MenuItemListener"); //得到要在其中查找方法的类
Class partypes[] = new Class[]{}; //函数调用的参数类型,这里为空。
Method moth=cls.getMethod(methListener, partypes); //得到方法
MenuItemListener ml=new MenuItemListener(); //以MenuItemListener创造一个新的对象
Object[] obj=new Object[]{}; //生成函数要调用的数据,这里为空。
Object r=moth.invoke(ml, obj); //调用得到的函数。
}catch(Throwable e){
System.err.println(e); //抛出异常。
}
}

JMenuBar MenuBarTest=new JMenuBar();
ConnClass cc=new ConnClass();
}
/*
本类同过Access数据库中一个Menu表建立菜单,表结构如下
mid:菜单的id号,自动增加
manme:菜单名称
mtype:菜单类型
paren:上一级菜单名称
mjb:菜单级别

ConnClass 类为一个只用来做数据库连接的类,方法getConn()返回一个连接
getListener(String methListener)是一个java反射的应用,用来通过一个给定的字符串查找一个方法并执行
主要参照的是下面的网址:
http://herald.seu.edu.cn/blog/bigseal/articles/16758.aspx
*
本类只是将我心里面的一点想法表达了出来,方法很笨,但是我希望能有人和我一起学习探讨
本类用NetBean IDE3.6编译
*/
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-26 18:03

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表