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

📄 24.txt

📁 电子工业出版社出版的《java2应用开发指南》配套光盘源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
	  if (debug) 
{
	    System.err.println("Button " + getLabel() + " pressed.");
	  }
	  Vector targets;
	  synchronized (this) 
{
	    targets = (Vector) pushListeners.clone();
	  }
	  ActionEvent actionEvt = new ActionEvent(this, 0, null);
	  for (int i = 0; i < targets.size(); i++) 
{
	    ActionListener target = (ActionListener)targets.elementAt(i);
	    target.actionPerformed(actionEvt);
	}

    }

    /** 
     * Enable debugging output.  Currently a message is printed each time
     * the button is clicked.  This is a bound property.
     * 
     * @see #getDebug
     * @see #addPropertyChangeListener
     */      
public void setDebug(boolean x) 
{
	  boolean old = debug;
	  debug = x;
	  changes.firePropertyChange("debug", new Boolean(old), new Boolean(x));
    }

    /** 
     * Returns true if debugging output is enabled.  
     * 
     * @see #setDebug
     */
public boolean getDebug() 
{
	  return debug;
    }

    /** 
     * Set the font size to 18 if true, 12 otherwise.  This property overrides
     * the value specified with setFontSize.  This is a bound property.
     * 
     * @see #isLargeFont
     * @see #addPropertyChangeListener
     */      
public void setLargeFont(boolean b) 
{
	  if (isLargeFont() == b) 
{
	    return;
	  }
	  int size = 12;
	if (b) 
{
	   size = 18;
	}
	Font old = getFont();
	setFont(new Font(old.getName(), old.getStyle(), size));
	changes.firePropertyChange("largeFont", new Boolean(!b), new Boolean(b));
    }

    /** 
     * Returns true if the font is "large" in the sense defined by setLargeFont.
     * 
     * @see #setLargeFont
     * @see #setFont
     */      
public boolean isLargeFont() 
{
        if (getFont().getSize() >= 18) 
{
	      return true;
	    }
else 
{
	      return false;
	    }
    }


   /** 
     * Set the point size of the current font.  This is a bound property.
     * 
     * @see #getFontSize
     * @see #setFont
     * @see #setLargeFont
     * @see #addPropertyChangeListener
     */      
public void setFontSize(int x) 
{
	  Font old = getFont();
	  setFont(new Font(old.getName(), old.getStyle(), x));
	changes.firePropertyChange("fontSize", new Integer(old.getSize()), new Integer(x));
    }

    /** 
     * Return the current font point size.
     * 
     * @see #setFontSize
     */
public int getFontSize() 
{
         return getFont().getSize();
    }

    /**
     * Set the current font and change its size to fit.  This is a 
     * bound property.
     *
     * @see #setFontSize
     * @see #setLargeFont
     */
public void setFont(Font f) 
{
	  Font old = getFont();
	  super.setFont(f);
	sizeToFit();
	changes.firePropertyChange("font", old, f);
	repaint();
    }

    /** 
     * Set the buttons label and change it's size to fit.  This is a 
     * bound property.
     * 
     * @see #getLabel
     */
public void setLabel(String newLabel) 
{
	 String oldLabel = label;
	 label = newLabel;
	 sizeToFit();
	 changes.firePropertyChange("label", oldLabel, newLabel);
    }

    /**
     * Returns the buttons label.
     * 
     * @see #setLabel
     */
public String getLabel() 
{
	  return label;
    }

public Dimension getPreferredSize() 
{
	  FontMetrics fm = getFontMetrics(getFont());
	  return new Dimension(fm.stringWidth(label) + TEXT_XPAD, 
			     fm.getMaxAscent() + fm.getMaxDescent() + TEXT_YPAD);
    }

    /**
     * @deprecated provided for backward compatibility with old layout managers.
     */
public Dimension preferredSize() 
{
	   return getPreferredSize();
    }

public Dimension getMinimumSize() 
{
	  return getPreferredSize();
    }

    /**
     * @deprecated provided for backward compatibility with old layout managers.
     */
public Dimension minimumSize() 
{
	   return getMinimumSize();
    }

private void sizeToFit() 
{
	  Dimension d = getSize();
	  Dimension pd = getPreferredSize();

	if (pd.width > d.width || pd.height > d.height) 
{
	    int width = d.width;
	    if (pd.width > width) 
{
		   width = pd.width;
	    }
	    int height = d.height;
	    if (pd.height > height) 
{
		height = pd.height;
	    }
	    setSize(width, height);

	    Component p = getParent();
	    if (p != null) 
{
	        p.invalidate();
	        p.validate();
	    }
	 }
  }

    /**
     * Set the color the buttons label is drawn with.  This is a bound property.
     */
public void setForeground(Color c) 
{
	Color old = getForeground();
	super.setForeground(c);
	changes.firePropertyChange("foreground", old, c);
	// This repaint shouldn't really be necessary.
	repaint();
    }


    /**
     * Set the color the buttons background is drawn with.  This is a bound property.
     */
public void setBackground(Color c) 
{
	Color old = getBackground();
	super.setBackground(c);
	changes.firePropertyChange("background", old, c);
	// This repaint shouldn't really be necessary.
	repaint();
    }


    private boolean debug;
    private PropertyChangeSupport changes = new PropertyChangeSupport(this);
    private Vector pushListeners = new Vector();
    private String label;
    private boolean down;
    private boolean sized;

    static final int TEXT_XPAD = 12;
    static final int TEXT_YPAD = 8;
}
例程24-3
package sunw.demo.buttons;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;

public class ExplicitButtonCustomizer extends Panel implements Customizer, KeyListener 
{

public ExplicitButtonCustomizer() 
{
	  setLayout(null);
    }

public void setObject(Object obj) 
{
	 target = (ExplicitButton) obj;

	 Label t1 = new Label("Caption:", Label.RIGHT);
	 add(t1);
	 t1.setBounds(10, 5, 60, 30);

	 labelField = new TextField(target.getLabel(), 20);
	 add(labelField);
	 labelField.setBounds(80, 5, 100, 30);

	labelField.addKeyListener(this);
    }

public Dimension getPreferredSize() 
{
	  return new Dimension(200, 40);
    }

    /**
     * @deprecated provided for backward compatibility with old layout managers.
     */
public Dimension preferredSize() 
{
	    return getPreferredSize();
    }

public void keyTyped(KeyEvent e) 
{
    }

public void keyPressed(KeyEvent e) 
{
    }

public void keyReleased(KeyEvent e) 
{
	   String txt = labelField.getText();
	   target.setLabel(txt);
	   support.firePropertyChange("", null, null);
    }

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

public void addPropertyChangeListener(PropertyChangeListener l) 
{
	  support.addPropertyChangeListener(l);
    }

public void removePropertyChangeListener(PropertyChangeListener l) 
{
	  support.removePropertyChangeListener(l);
    }

    private PropertyChangeSupport support = new PropertyChangeSupport(this);

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

    private ExplicitButton target;
    private TextField labelField;
}

⌨️ 快捷键说明

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