[Original] Code Example: Customize Components and Custom Events

xiaoxiao2021-03-06  70

[Original] Code Example: Customize Components and Custom Events

Custom event: / ** * @ (#) odtbuttonevent.java * * Copyright 2004 OpenSource Develop Team. All Rights Reserved. * /

// packagepackage com.opensource.event;

// Import classesimport java.awt.awtevent;

/ ** * Custom button event. * * @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; // mouse enters BUTTON public static final int EXITED = 2; // mouse exit Button public static final int proped = 3; // Mouse Press public static final int product = 4; // mouse release

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; // Returns the listened object}

Public Object GetItem () {Return Item;}

Public int getButtonState () {return buttonstate; // Return button status}

Public string paramstring () {string s = null; switch (buttonstate) {copy: s = "entered"; break; case evted: s = "exited"; break; case pressed: s = "pressed"; break; casse Released: s = "released"; Break; default: s = "unknown button";

Return super.ParamString () "[Button =" "]";}} Custom listener interface: / ** * @ (#) OdtbuttonListener.java * * Copyright 2004 OpenSource Develop Team. All Rights Reserved. * /

// packagepackage com.opensource.event;

// Import Classesimport java.util.EventListener;

/ ** * Interface listener ODTButtonEvent * * @author ODT * @version 1.0 06/05/2004 * @see EventListener * @since JDK 1.3 * / public interface ODTButtonListener extends EventListener {void buttonStateChanged (ODTButtonEvent event); //} trigger event

Custom component registration listener interface: / ** * @ (#) odtbuttonable.java * * Copyright 2004 OpenSource Develop Team. All rights reserved. * /

// packagepackage com.opensource.event;

// Import Classes

/ ** * component implements this interface, can extend the expansion event of the listener registration * * @Author odt * @version 1.0 06/05/2004 * @since jdk 1.3 * / public interface odtbuttonable {public void AddButtonListener (OdtbuttonListener L ); // Register the listener public void RemoveButtonListener (ODTBUTTONLISTENER L); // cancel listener}

The above is a description of the custom event mechanism, and below is a custom component.

Custom components:

/ ** * @ (#) odtbutton.java * * Copyright 2004 OpenSource Develop Team. All rights reserved. * /

// packagepackage com.opensource.odt;

// Import classesimport java.util. *; import java.awt. *; import java.awt.event. *;

Import com.opensource.event. *;

/ * ** implement interface member 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 listener = 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; repaint ();

Public void mouseexited (mouseEvent evenet) {stated = odtbuttonevent.exited; meaningd; repaint ();}});

Public void entered (int Stated) {ODTButtonevent E = New Odtbuttonevent (this, this, stated); processButtonevent (e); // Trigger events using ProcessButtonEvent (e)

Public void pressed (int State) {ODTButtonevent E = New Odtbuttonevent (this, this, stated); processButtonevent (e);}

Public void exited (int State) {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) {listener.addeElement (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); // trigger event}} // button border public void DrawBorder (G.SetColor (New Color (174, 23, 145) ); G.drawroundRect (0, 0, size.width-1, size.height-1, 5);} // change button color 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);}}

Test custom components and events:

/ ** * @ (#) odtbuttontest.java * * Copyright 2004 OpenSource Develop Team. All rights reserved. * /

// packagepackage com.opensource.odt;

// Import classesimport java.awt. *; import java.awt.event. *; import javax.swing. *; import javax.swing.event. *;

Import com.opensource.event. *;

/ ** * test components and custom event code Examples * * @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); // register listener getContentPane () setLayout (new FlowLayout ());.. getContentPane () add (button); getContentPane () setBackground. (Color.WHITE); setSize (400, 500); setVisible (true);} // event triggered public void buttonStateChanged (ODTButtonEvent event) {ODTButtonable ie = event.getODTButtonable (); System.out.println (event. Paramstring ());} public static void main (string [] args) {new odtbuttontest ();}}

转载请注明原文地址:https://www.9cbs.com/read-121078.html

New Post(0)