⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 24.txt

📁 电子工业出版社出版的《java2应用开发指南》配套光盘源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
例程24-1
// ExplicitButtonBeanInfo.java
//包定义
package sunw.demo.buttons;
//引入相关类
import java.beans.*;

/**
 * BeanInfo for an ExplicitButton.
 * 
 * @see sunw.demo.buttons.ExplicitButton
 */
//创建BeanInfo类
public class ExplicitButtonBeanInfo extends SimpleBeanInfo 
{

    //获取属性描述符方法
public PropertyDescriptor[] getPropertyDescriptors() 
{
        try 
{
            PropertyDescriptor background =
			new PropertyDescriptor("background", beanClass);
	    PropertyDescriptor foreground =
			new PropertyDescriptor("foreground", beanClass);
	    PropertyDescriptor font =
			new PropertyDescriptor("font", beanClass);
            PropertyDescriptor label =
			new PropertyDescriptor("label", beanClass);

            background.setBound(true);
            foreground.setBound(true);
            font.setBound(true);
            label.setBound(true);

            PropertyDescriptor rv[] = {background, foreground, font, label};
            return rv;
        }
catch (IntrospectionException e) 
{
            throw new Error(e.toString());
        }
    }


    //取得默认属性的索引
public int getDefaultPropertyIndex() 
{
	// the index for the "label" property
        return 3; 
    }


   //取得事件描述符的方法
public EventSetDescriptor[] getEventSetDescriptors() 
{
        try 
{
            EventSetDescriptor push = new EventSetDescriptor(beanClass, 
	                "actionPerformed",
			java.awt.event.ActionListener.class,
			"actionPerformed");

            EventSetDescriptor changed = new EventSetDescriptor(beanClass,
			"propertyChange",
			java.beans.PropertyChangeListener.class,
			"propertyChange");

            push.setDisplayName("button push");
            changed.setDisplayName("bound property change");
	
            EventSetDescriptor[] rv = { push, changed};
            return rv;
        }
catch (IntrospectionException e) 
{
            throw new Error(e.toString());
        }
    }

    //取得Bean描述符的方法
public BeanDescriptor getBeanDescriptor() 
{
	  BeanDescriptor back = new BeanDescriptor(beanClass, customizerClass);
        back.setValue("hidden-state", Boolean.TRUE);
	  return back;
    }

   //获取与Bean相关联的图标
public java.awt.Image getIcon(int iconKind) 
{
	   if (iconKind == BeanInfo.ICON_MONO_16x16 ||
	      iconKind == BeanInfo.ICON_COLOR_16x16 ) 
{
	         java.awt.Image img = loadImage("ExplicitButtonIcon16.gif");
	         return img;
	      }
	      if (iconKind == BeanInfo.ICON_MONO_32x32 ||
	      iconKind == BeanInfo.ICON_COLOR_32x32 ) {
	      java.awt.Image img = loadImage("ExplicitButtonIcon32.gif");
	      return img;
	  }
	  return null;
    }

    private final static Class beanClass = ExplicitButton.class;
    private final static Class customizerClass = ExplicitButtonCustomizer.class;
}
例程24-2
// OurButton.java
package sunw.demo.buttons;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.Serializable;
import java.util.Vector;


/**
 * A simple Java Beans button.  OurButton is a "from-scratch" 
 * lightweight AWT component.  It's a good example of how to
 * implement bound properties and support for event listeners.
 *
 * Parts of the source are derived from sun.awt.tiny.TinyButtonPeer.
 */
//Bean对象类的定义
public class OurButton extends Component implements Serializable,
				MouseListener, MouseMotionListener 
{
    /**
     * Constructs a Button with the a default label.
*/
    //构造方法
public OurButton() 
{
	  this("press");
    }

    /**
     * Constructs a Button with the specified label.
     * @param label the label of the button
*/
    //构造方法
public OurButton(String label) 
{
	 super();
	 this.label = label;
	 setFont(new Font("Dialog", Font.PLAIN, 12));
	 setBackground(Color.lightGray);
	 addMouseListener(this);
	 addMouseMotionListener(this);
    }

    //----------------------------------------------------------------------

    /**
     * Paint the button: the label is centered in both dimensions.
     * 
*/
    //Bean重绘方法
public synchronized void paint(Graphics g) 
{
	int width = getSize().width;
	int height = getSize().height;

	g.setColor(getBackground());
	g.fill3DRect(0, 0, width - 1, height - 1, !down);

	g.setColor(getForeground());
	g.setFont(getFont());

	g.drawRect(2, 2, width - 4, height - 4);

	FontMetrics fm = g.getFontMetrics();
	g.drawString(label, (width - fm.stringWidth(label)) / 2, 
		          (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2);
    }	

    //----------------------------------------------------------------------

    // Mouse listener methods.
    //鼠标事件
public void mouseClicked(MouseEvent evt) 
{
    }

public void mousePressed(MouseEvent evt) 
{
	  if (!isEnabled()) 
{
	    return;
	  }
	  down = true;
	  repaint();
    }

public void mouseReleased(MouseEvent evt) 
{
	  if (!isEnabled()) 
{
	    return;
	  }
	  if (down) 
{
	    fireAction();
	    down = false;
	    repaint();
	  }
    }

public void mouseEntered(MouseEvent evt) 
{
    }

public void mouseExited(MouseEvent evt) 
{
    }

public void mouseDragged(MouseEvent evt) 
{
	if (!isEnabled()) 
{
	    return;
	}
	// Has the mouse been dragged outside the button?
	int x = evt.getX();
	int y = evt.getY();
	int width = getSize().width;
	int height = getSize().height;
	if (x < 0 || x > width || y < 0 || y > height) 
{
	    // Yes, we should deactivate any pending click.
	    if (down) 
{
		  down = false;
		  repaint();
	    }
	} 
else if (!down) 
{
	    down = true;
	    repaint();
	}
    }

public void mouseMoved(MouseEvent evt) 
{
    }

    //----------------------------------------------------------------------

    // Methods for registering/deregistering event listeners

    /**
     * The specified ActionListeners <b>actionPerformed</b> method will 
     * be called each time the button is clicked.  The ActionListener
     * object is added to a list of ActionListeners managed by 
     * this button, it can be removed with removeActionListener.
     * Note: the JavaBeans specification does not require ActionListeners
     * to run in any particular order.
     * 
     * @see #removeActionListener
     * @param l the ActionListener
     */      

public synchronized void addActionListener(ActionListener l) 
{
	  pushListeners.addElement(l);
    }

    /** 
     * Remove this ActionListener from the buttons internal list.  If the
     * ActionListener isn't on the list, silently do nothing.
     * 
     * @see #addActionListener
     * @param l the ActionListener
     */      
public synchronized void removeActionListener(ActionListener l) 
{
	  pushListeners.removeElement(l);
    }

    /**
     * The specified PropertyChangeListeners <b>propertyChange</b> method will
     * be called each time the value of any bound property is changed.
     * The PropertyListener object is addded to a list of PropertyChangeListeners
     * managed by this button, it can be removed with removePropertyChangeListener.
     * Note: the JavaBeans specification does not require PropertyChangeListeners
     * to run in any particular order.
     *
     * @see #removePropertyChangeListener
     * @param l the PropertyChangeListener
     */      
public void addPropertyChangeListener(PropertyChangeListener l) 
{
	  changes.addPropertyChangeListener(l);
    }

    /** 
     * Remove this PropertyChangeListener from the buttons internal list.  
     * If the PropertyChangeListener isn't on the list, silently do nothing.
     * 
     * @see #addPropertyChangeListener
     * @param l the PropertyChangeListener
     */      
public void removePropertyChangeListener(PropertyChangeListener l) 
{
	    changes.removePropertyChangeListener(l);
    }

    //----------------------------------------------------------------------


    /** 
     * This method has the same effect as pressing the button.
     * 
     * @see #addActionListener
     */      
public void fireAction() 
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -