我来我网
https://5come5.cn
 
您尚未 登录  注册 | 菠菜 | 软件站 | 音乐站 | 邮箱1 | 邮箱2 | 风格选择 | 更多 » 
 

2004



性别: 帅哥 状态: 该用户目前不在线
等级: 品行端正
发贴: 222
威望: 0
浮云: 1107
在线等级:
注册时间: 2006-09-25
最后登陆: 2011-10-25

5come5帮你背单词 [ medal /'medl/ n. 奖章,励章,纪念章 ]


Java反射功能

java反射包(java.lang.reflect)为我们提供一个强大的功能,利用它可以查出一个未知类所有的:数据字段,方法,构造器。下面这个程序就是一个利用reflect包写的一个简单的GUI程序,在文本域里输入java标准类名(如:javax.swing.JButton),按执行按钮,即可查出这个类所有的信息。

package reflectframe;

import javax.swing.UIManager;
import java.awt.*;

public class Reflect { /*main()类
boolean packFrame = false;

//Construct the application
public Reflect() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
/*Pack frames that have useful preferred size info, e.g. from their layout*/
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Reflect();
}
}
/*主界面类*/
package reflectframe;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;
import java.lang.reflect.*;

public class Frame1 extends JFrame {
JPanel contentPane;
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
TitledBorder titledBorder1;
JLabel jLabel2 = new JLabel();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
BorderLayout borderLayout1 = new BorderLayout();
XYLayout xYLayout1 = new XYLayout();

//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
Toolkit tk=Toolkit.getDefaultToolkit();
Image img=tk.getImage("status.gif");
Cursor cu=tk.createCustomCursor(img,new Point(10,10),"stick");
this.setCursor(cu);
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134)),"结果");
jTextField1.setFont(new java.awt.Font("Dialog", 0, 15));
jTextField1.setSelectedTextColor(Color.white);
jTextField1.setText("");
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(450, 361));
this.setTitle("Reflect");
this.addWindowListener(new Frame1_this_windowAdapter(this));
jButton1.setText("执行");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel1.setText("类名:");
jTextArea1.setFont(new java.awt.Font("Dialog", 0, 15));
jTextArea1.setEditable(false);
jTextArea1.setText("");
jScrollPane1.setBorder(titledBorder1);
jLabel2.setText(" ");
jPanel1.setLayout(xYLayout1);
jPanel2.setLayout(borderLayout2);
jPanel1.add(jTextField1, new XYConstraints(55, 5, 304, -1));
jPanel1.add(jLabel1, new XYConstraints(16, 8, -1, -1));
jPanel1.add(jButton1, new XYConstraints(374, 6, -1, -1));
jPanel2.add(jScrollPane1);
contentPane.add(jPanel1, BorderLayout.NORTH);
contentPane.add(jPanel2, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1, null);
this.getRootPane().setDefaultButton(jButton1);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void jButton1_actionPerformed(ActionEvent e) {
String className=jTextField1.getText();
StringBuffer buf=new StringBuffer();
try{
Class c = Class.forName(className);
String superName=c.getSuperclass().getName(); /*得到该类的超类*/
buf.append(className+" extends "+superName+"\n{\n");
buf.append(" \\* 字段 *\\\n");
buf.append(getFields(c));
buf.append(" \\* 构造器 *\\\n");
buf.append(getConstructors(c));
buf.append(" \\* 方法 *\\\n");
buf.append(getMethods(c));
buf.append("}\n");
}catch(Exception et){
JOptionPane.showMessageDialog(this,"没找到该类:"+et.getMessage());}
jTextArea1.setText(buf.toString());
}
public String getFields(Class c){ /*得到该类的所有数据字段*/
String str="";
Field[] fields=c.getDeclaredFields();
for(int i=0;i0){
if(j==cla.length-1)str += cla[j].getName();
else str += cla[j].getName() + ", ";
}
}
str+=");\n";
}
return str;
}
public String getMethods(Class c){ /*得到类里所有方法*/
String str="";
Method[] m=c.getMethods();
for(int i=0;i0){
if (j == clb.length - 1)str += clb[j].getName();
else str += clb[j].getName() + ", ";
}
}
str+=");\n";
}
return str;
}
}

class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
Frame1 adaptee;

Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

class Frame1_this_windowAdapter extends java.awt.event.WindowAdapter {
Frame1 adaptee;

Frame1_this_windowAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
}
反射功能在javabeans中得到最为充分的利用,对beans的能力进行查询。

顶端 Posted: 2007-05-02 23:19 | [楼 主]
我来我网·5come5 Forum » 程序员之家

Total 0.007795(s) query 4, Time now is:11-27 09:03, Gzip enabled
Powered by PHPWind v5.3, Localized by 5come5 Tech Team, 黔ICP备16009856号