// // BarChart Bean现在接收输入 // public class BarChartBean extends Canvas implements Serializable, PropertyChangeListener { // ... // List of percent listeners. private Vector percentListeners_;
// ... a whole lotta methods...
// Set/Get methods for percent public void setPercent(int ipercent) { // Set new percent, and only if necessary repaint() // This is the only place that iPercent´s range is controlled if (iPercent <= 100 && iPercent>= 0) { // Save old value, set new value FIRST int prevPercent = iPercent_; iPercent_ = iPercent;
// Notify property listeners of change to "percent" property pcs_.firePropertyChange("percent", new Integer(prevPercent), new Integer(iPercent_));
// Notify all listeners for "percent" change notifyPercentChanged(prevPercent, iPercent);
// Repaint only if necessary. if (prevPercent != iPercent_) { repaint(); } } }
// ...
// // These methods are for handling "PercentListeners" //
// Add a new percent listener public synchronized void addPercentListener(PercentListener listener) { percentListeners_.addElement(listener); }
// Remove a percent listener public synchronized void removePercentListener(PercentListener listener) { percentListeners_.removeElement(listener); }
// Notify all listeners that "percent" changed protected void notifyPercentChanged(int oldPct, int newPct) { Vector thisList = new Vector(); PercentEvent thisEvent = new PercentEvent(this, oldPct, newPct);
// Make a copy of the list so potential changes to it by // other threads won´t affect traversal. synchronized (this) { thisList = (Vector)percentListeners_.clone(); }
// Send a "PercentEvent" to every listener. for (int elem = 0; elem
public class Example extends Panel implements Serializable { private PercentLabel pl_ = new PercentLabel(); private BarChartBean bcb_ = new BarChartBean();
public interface MouseListener extends EventListener { public void mouseClicked(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseExited(MouseEvent e); public void mousePressed(MouseEvent e); public void mouseReleased(MouseEvent e); };
public interface MouseMotionListener extends EventListener { public void mouseDragged(MouseEvent e); public void mouseMoved(MouseEvent e); }
// count right mouse clicks public class ClickCounter extends Panel implements MouseListener { private int iClicks_ = 0;
// Listen for events on self ClickCounter() { addMouseListener(this); }
public void mouseClicked(MouseEvent e) { iClicks_++; repaint();} public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void paint() { // paint the # of clicks on the panel } }