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;
}
字串7
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;
字串3
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("");
字串4
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);
字串6
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());
![我要研发网[www.51dev.com]](/templets/images/toplogo.gif)
