TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
通过注释给按钮添加动作监听器。来自一本教科书。
一、自定义注释
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ActionListenerFor
{
String source();
}
这个注释能用于方法,且编译器会将注释信息加入类文件中,运行时虚拟机将载入这些信息。
二、给方法添加注释信息- /**
- @author Cay Horstmann
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class ButtonTest
- {
- public static void main(String[] args)
- {
- ButtonFrame frame = new ButtonFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- }
- /**
- A frame with a button panel
- */
- class ButtonFrame extends JFrame
- {
- public ButtonFrame()
- {
- setTitle("ButtonTest");
- setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
- panel = new JPanel();
- add(panel);
- // create buttons
- yellowButton = new JButton("Yellow");
- blueButton = new JButton("Blue");
- redButton = new JButton("Red");
- // add buttons to panel
- panel.add(yellowButton);
- panel.add(blueButton);
- panel.add(redButton);
- ActionListenerInstaller.processAnnotations(this);
- }
- @ActionListenerFor(source="yellowButton")
- public void yellowBackground()
- {
- panel.setBackground(Color.YELLOW);
- }
- @ActionListenerFor(source="blueButton")
- public void blueBackground()
- {
- panel.setBackground(Color.BLUE);
- }
- @ActionListenerFor(source="redButton")
- public void redBackground()
- {
- panel.setBackground(Color.RED);
- }
- public static final int DEFAULT_WIDTH = 300;
- public static final int DEFAULT_HEIGHT = 200;
- private JPanel panel;
- private JButton yellowButton;
- private JButton blueButton;
- private JButton redButton;
- }
- 三、利用反射和动态代理处理注释。
- import java.awt.event.*;
- import java.lang.annotation.*;
- import java.lang.reflect.*;
- public class ActionListenerInstaller
- {
- /**
- Processes all ActionListenerFor annotations in the given object.
- @param obj an object whose methods may have ActionListenerFor annotations
- */
- public static void processAnnotations(Object obj)
- {
- try
- {
- Class cl = obj.getClass();
-
- for (Method m : cl.getDeclaredMethods())
- {
- System.out.println(m.getName());
- ActionListenerFor a = m.getAnnotation(ActionListenerFor.class);
- if (a != null)
- {
- Field f = cl.getDeclaredField(a.source());
- f.setAccessible(true);
- addListener(f.get(obj), obj, m);
- }
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- /**
- Adds an action listener that calls a given method.
- @param source the event source to which an action listener is added
- @param param the implicit parameter of the method that the listener calls
- @param m the method that the listener calls
- */
- public static void addListener(Object source, final Object param, final Method m)
- throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
- {
- InvocationHandler handler = new
- InvocationHandler()
- {
- public Object invoke(Object proxy, Method mm, Object[] args) throws Throwable
- {
- return m.invoke(param);
- }
- };
-
- Object listener = Proxy.newProxyInstance(null, new Class[] { java.awt.event.ActionListener.class },
- handler);
- Method adder = source.getClass().getMethod("addActionListener", ActionListener.class);
- adder.invoke(source, listener);
- }
- }
复制代码
源码下载:http://file.javaxxz.com/2014/10/29/235805906.zip |
|