[原创]代码范例:自定义组件和自定义事件

类别:Java 点击:0 评论:0 推荐:

[原创]代码范例:自定义组件和自定义事件

自定义事件:
/**
 * @(#) ODTButtonEvent.java
 *
 * Copyright 2004 Opensource Develop Team. All rights reserved.
 */

// package
package com.opensource.event;

// import classes
import java.awt.AWTEvent;

/**
 * 自定义按钮事件。
 *
 * @author ODT
 * @version 1.0 06/05/2004
 * @see AWTEvent
 * @since JDK 1.3
 */
public class ODTButtonEvent extends AWTEvent
{
     public static final int ENTERED = 1; // 鼠标进入BUTTON
     public static final int EXITED = 2; // 鼠标退出BUTTON
     public static final int PRESSED = 3; // 鼠标按下
     public static final int RELEASED = 4; // 鼠标释放

     Object item;
     int buttonState;

     public ODTButtonEvent(ODTButtonable source, Object item, int buttonState)
     {
           super(source, -1);
           this.item = item;
           this.buttonState = buttonState;
     }

     public ODTButtonable getODTButtonable()
     {
           return (ODTButtonable) source; // 返回被监听的对象
     }

     public Object getItem()
     {
           return item;
     }

     public int getButtonState()
     {
           return buttonState; // 返回按钮状态
     }

     public String paramString()
     {
           String s = null;
           switch (buttonState)
           {
                 case ENTERED:
                       s = "ENTERED";
                       break;
                 case EXITED:
                       s = "EXITED";
                       break;
                 case PRESSED:
                       s = "PRESSED";
                       break;
                 case RELEASED:
                       s = "RELEASED";
                       break;
                 default:
                       s = "unknown button state";
           }

           return super.paramString() + " [button=" + s + "]";
     }
}


自定义监听接口:
/**
 * @(#) ODTButtonListener.java
 *
 * Copyright 2004 Opensource Develop Team. All rights reserved.
 */

// package
package com.opensource.event;

// import classes
import java.util.EventListener;

/**
 * 监听ODTButtonEvent接口
 *
 * @author ODT
 * @version 1.0 06/05/2004
 * @see EventListener
 * @since JDK 1.3
 */
public interface ODTButtonListener extends EventListener
{
     void buttonStateChanged(ODTButtonEvent event); // 触发事件
}


自定义构件注册监听器接口:
/**
 * @(#) ODTButtonable.java
 *
 * Copyright 2004 Opensource Develop Team. All rights reserved.
 */

// package
package com.opensource.event;

// import classes

/**
 * 构件实现此接口,可以扩展监听者注册感兴趣的扩展事件
 *
 * @author ODT
 * @version 1.0 06/05/2004
 * @since JDK 1.3
 */
public interface ODTButtonable
{
     public void addButtonListener(ODTButtonListener l); // 注册监听器
     public void removeButtonListener(ODTButtonListener l); // 取消监听器
}


以上是对自定义事件机制的描述,下面是自定义组件。

自定义组件:

/**
 * @(#) ODTButton.java
 *
 * Copyright 2004 Opensource Develop Team. All rights reserved.
 */

// package
package com.opensource.odt;

// import classes
import java.util.*;
import java.awt.*;
import java.awt.event.*;

import com.opensource.event.*;

/**
 * 实现ItemExpandable接口的构件
 *
 * @author ODT
 * @version 1.0 06/05/2004
 * @see Component
 * @see ODTButtonable
 * @since JDK 1.3
 */
public class ODTButton extends Component implements ODTButtonable
{
     private int stated;
     private Vector listeners = new Vector();

     public ODTButton()
     {
           addMouseListener(new MouseAdapter()
           {
                 public void mouseEntered(MouseEvent event)
                 {
                       stated = ODTButtonEvent.ENTERED;
                       entered(stated);
                       repaint();
                 }

                 public void mousePressed(MouseEvent event)
                 {
                       stated = ODTButtonEvent.PRESSED;
                       pressed(stated);
                       repaint();
                 }

                 public void mouseExited(MouseEvent event)
                 {
                       stated = ODTButtonEvent.EXITED;
                       exited(stated);
                       repaint();
                 }
           });
     }

     public void entered(int stated)
     {
           ODTButtonEvent e = new ODTButtonEvent(this, this, stated);
           processButtonEvent(e); // 利用processButtonEvent(e)来触发事件
     }

     public void pressed(int stated)
     {
           ODTButtonEvent e = new ODTButtonEvent(this, this, stated);
           processButtonEvent(e);
     }

     public void exited(int stated)
     {
           ODTButtonEvent e = new ODTButtonEvent(this, this, stated);
           processButtonEvent(e);
     }

     public void paint(Graphics g)
     {
           drawBorder(g);
           drawContent(g);
     }

     public Dimension getPreferredSize()
     {
           return new Dimension(50, 20);
     }

     public void addButtonListener(ODTButtonListener l)
     {
           listeners.addElement(l);
     }

     public void removeButtonListener(ODTButtonListener l)
     {
           listeners.removeElement(l);
     }
    
     public synchronized void processButtonEvent(ODTButtonEvent event)
     {
           Enumeration e = listeners.elements();
           while (e.hasMoreElements())
           {
                 ODTButtonListener l = (ODTButtonListener) e.nextElement();
                 l.buttonStateChanged(event); // 触发事件
           }
     }

     // 按钮边框
     public void drawBorder(Graphics g)
     {
           Dimension size = getSize();
           g.setColor(new Color(174, 23, 145));
           g.drawRoundRect(0, 0, size.width-1, size.height-1, 5, 5);
     }
    
     // 改变按钮颜色
     public void drawContent(Graphics g)
     {
           Dimension size = getSize();
           if (stated == ODTButtonEvent.ENTERED)
           {
                 g.setColor(new Color(248, 199, 238));
           }
           else
                 g.setColor(new Color(236, 111, 210));


           g.fillRoundRect(1, 1, size.width-2, size.height-2, 5, 5);
     }
}
 


测试自定义组件和事件:

/**
 * @(#) ODTButtonTest.java
 *
 * Copyright 2004 Opensource Develop Team. All rights reserved.
 */

// package
package com.opensource.odt;

// import classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import com.opensource.event.*;


/**
 * 测试自定义组件和事件代码范例
 *
 * @author ODT
 * @version 1.0 06/05/2004
 * @see JFrame
 * @see ODTButtonListener
 * @since JDK 1.3
 */
public class ODTButtonTest extends JFrame implements ODTButtonListener
{
     public ODTButtonTest()
     {
           ODTButton button = new ODTButton();
           button.addButtonListener(this); // 注册监听器
           getContentPane().setLayout(new FlowLayout());
           getContentPane().add(button);
           getContentPane().setBackground(Color.WHITE);
           setSize(400, 500);
           setVisible(true);
     }
    
     // 所触发的事件
     public void buttonStateChanged(ODTButtonEvent event)
     {
           ODTButtonable ie = event.getODTButtonable();
           System.out.println(event.paramString());
     }

     public static void main(String[] args)
     {
           new ODTButtonTest();
     }
}

本文地址:http://com.8s8s.com/it/it15775.htm