JAVA反射包应用功能GUI程序举例
Java反射包(java.lang.reflect)为我们提供一个强大的功能,利用它可以查出一个未知类所有的:数据字段,方法,构造器。
下面这个程序就是一个利用reflect包写的一个简单的GUI程序,麦子分享给大家,在文本域里输入java标准类名(如:javax.swing.JButton),按执行按钮,即可查出这个类所有的信息。
001 | package com.ykmaiz.test; |
003 | import javax.swing.UIManager; |
006 | public class Reflect { |
007 | boolean packFrame = false ; |
011 | Frame1 frame = new Frame1(); |
020 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
021 | Dimension frameSize = frame.getSize(); |
022 | if (frameSize.height > screenSize.height) { |
023 | frameSize.height = screenSize.height; |
025 | if (frameSize.width > screenSize.width) { |
026 | frameSize.width = screenSize.width; |
028 | frame.setLocation((screenSize.width - frameSize.width) / 2 , |
029 | (screenSize.height - frameSize.height) / 2 ); |
030 | frame.setVisible( true ); |
033 | public static void main(String[] args) { |
035 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
044 | package com.ykmaiz.test; |
047 | import java.awt.event.*; |
048 | import javax.swing.*; |
049 | import com.borland.jbcl.layout.*; |
050 | import javax.swing.border.*; |
051 | import java.lang.reflect.*; |
053 | public class Frame1 extends JFrame { |
055 | JTextField jTextField1 = new JTextField(); |
056 | JButton jButton1 = new JButton(); |
057 | JLabel jLabel1 = new JLabel(); |
058 | JScrollPane jScrollPane1 = new JScrollPane(); |
059 | JTextArea jTextArea1 = new JTextArea(); |
060 | TitledBorder titledBorder1; |
061 | JLabel jLabel2 = new JLabel(); |
062 | JPanel jPanel1 = new JPanel(); |
063 | JPanel jPanel2 = new JPanel(); |
064 | BorderLayout borderLayout2 = new BorderLayout(); |
065 | BorderLayout borderLayout1 = new BorderLayout(); |
066 | XYLayout xYLayout1 = new XYLayout(); |
070 | enableEvents(AWTEvent.WINDOW_EVENT_MASK); |
077 | Toolkit tk=Toolkit.getDefaultToolkit(); |
078 | Image img=tk.getImage( "status.gif" ); |
079 | Cursor cu=tk.createCustomCursor(img, new Point( 10 , 10 ), "stick" ); |
083 | private void jbInit() throws Exception { |
084 | contentPane = (JPanel) this .getContentPane(); |
085 | titledBorder1 = new TitledBorder(BorderFactory. |
086 | createEtchedBorder(Color.white, new Color( 134 , 134 , 134 )), "结果" ); |
087 | jTextField1.setFont( new java.awt.Font( "Dialog" , 0 , 15 )); |
088 | jTextField1.setSelectedTextColor(Color.white); |
089 | jTextField1.setText( "" ); |
090 | contentPane.setLayout(borderLayout1); |
091 | this .setSize( new Dimension( 450 , 361 )); |
092 | this .setTitle( "Reflect" ); |
093 | this .addWindowListener( new Frame1_this_windowAdapter( this )); |
094 | jButton1.setText( "执行" ); |
095 | jButton1.addActionListener( new Frame1_jButton1_actionAdapter( this )); |
096 | jLabel1.setFont( new java.awt.Font( "Dialog" , 0 , 12 )); |
097 | jLabel1.setText( "类名:" ); |
098 | jTextArea1.setFont( new java.awt.Font( "Dialog" , 0 , 15 )); |
099 | jTextArea1.setEditable( false ); |
100 | jTextArea1.setText( "" ); |
101 | jScrollPane1.setBorder(titledBorder1); |
102 | jLabel2.setText( " " ); |
103 | jPanel1.setLayout(xYLayout1); |
104 | jPanel2.setLayout(borderLayout2); |
105 | jPanel1.add(jTextField1, new XYConstraints( 55 , 5 , 304 , - 1 )); |
106 | jPanel1.add(jLabel1, new XYConstraints( 16 , 8 , - 1 , - 1 )); |
107 | jPanel1.add(jButton1, new XYConstraints( 374 , 6 , - 1 , - 1 )); |
108 | jPanel2.add(jScrollPane1); |
109 | contentPane.add(jPanel1, BorderLayout.NORTH); |
110 | contentPane.add(jPanel2, BorderLayout.CENTER); |
111 | jScrollPane1.getViewport().add(jTextArea1, null ); |
112 | this .getRootPane().setDefaultButton(jButton1); |
115 | protected void processWindowEvent(WindowEvent e) { |
116 | super .processWindowEvent(e); |
117 | if (e.getID() == WindowEvent.WINDOW_CLOSING) { |
122 | void jButton1_actionPerformed(ActionEvent e) { |
123 | String className=jTextField1.getText(); |
124 | StringBuffer buf= new StringBuffer(); |
126 | Class c = Class.forName(className); |
127 | String superName=c.getSuperclass().getName(); |
128 | buf.append(className+ " extends " +superName+ "\n{\n" ); |
129 | buf.append( " \\* 字段 *\\\n" ); |
130 | buf.append(getFields(c)); |
131 | buf.append( " \\* 构造器 *\\\n" ); |
132 | buf.append(getConstructors(c)); |
133 | buf.append( " \\* 方法 *\\\n" ); |
134 | buf.append(getMethods(c)); |
136 | } catch (Exception et){ |
137 | JOptionPane.showMessageDialog( this , "没找到该类:" +et.getMessage());} |
138 | jTextArea1.setText(buf.toString()); |
140 | public String getFields(Class c){ |
142 | Field[] fields=c.getDeclaredFields(); |
144 | if (j==cla.length- 1 )str += cla[j].getName(); |
145 | else str += cla[j].getName() + ", " ; |
152 | public String getMethods(Class c){ |
154 | Method[] m=c.getMethods(); |
156 | if (j == clb.length - 1 )str += clb[j].getName(); |
157 | else str += clb[j].getName() + ", " ; |
166 | class Frame1_jButton1_actionAdapter |
167 | implements java.awt.event.ActionListener { |
170 | Frame1_jButton1_actionAdapter(Frame1 adaptee) { |
171 | this .adaptee = adaptee; |
173 | public void actionPerformed(ActionEvent e) { |
174 | adaptee.jButton1_actionPerformed(e); |
178 | class Frame1_this_windowAdapter |
179 | extends java.awt.event.WindowAdapter { |
182 | Frame1_this_windowAdapter(Frame1 adaptee) { |
183 | this .adaptee = adaptee; |
|
0Java中Serializable接口的作用。
不管任何类型只要实现了Serializable接口,就可以被保存到文件中,或者作为数据流通过网络发送到别的地方。
也可以用管道来传输到调节阀系统的其他程序中。
这样子极大的简化了类的设计。
只要设计一个保存一个读取功能就能解决上面说得所有问题。
实现serializable接口的作用是就是可以把对象存到字节流,然后可以恢复!
所以你想如果你的对象没实现序列化怎么才能进行网络传输呢,要网络传输就得转为字节流,所以在分布式应用中,你就得实现序列化,如果你不需要分布式应用,那就没那个必要实现序列化。