📄 jslider.java
字号:
/* JSlider.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing;import java.awt.Dimension;import java.awt.MenuContainer;import java.awt.image.ImageObserver;import java.beans.PropertyChangeEvent;import java.io.Serializable;import java.util.Dictionary;import java.util.Enumeration;import java.util.Hashtable;import javax.accessibility.Accessible;import javax.accessibility.AccessibleContext;import javax.accessibility.AccessibleRole;import javax.accessibility.AccessibleStateSet;import javax.accessibility.AccessibleValue;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.plaf.SliderUI;/** * The JSlider is a Swing component that allows selection of a value within a * range by adjusting a thumb in a track. The values for the minimum, * maximum, extent and value are stored in a {@link * DefaultBoundedRangeModel}. * * <p> * JSliders have the following properties: * </p> * * <table> * <tr><th> Property </th><th> Stored in </th><th> Bound? </th></tr> * <tr><td> extent </td><td> model </td><td> no </td></tr> * <tr><td> inverted </td><td> slider </td><td> yes </td></tr> * <tr><td> labelTable </td><td> slider </td><td> yes </td></tr> * <tr><td> majorTickSpacing </td><td> slider </td><td> yes </td></tr> * <tr><td> maximum </td><td> model </td><td> no </td></tr> * <tr><td> minimum </td><td> model </td><td> no </td></tr> * <tr><td> minorTickSpacing </td><td> slider </td><td> yes </td></tr> * <tr><td> model </td><td> slider </td><td> yes </td></tr> * <tr><td> orientation </td><td> slider </td><td> yes </td></tr> * <tr><td> paintLabels </td><td> slider </td><td> yes </td></tr> * <tr><td> paintTicks </td><td> slider </td><td> yes </td></tr> * <tr><td> snapToTicks </td><td> slider </td><td> no </td></tr> * <tr><td> value </td><td> model </td><td> no </td></tr> * <tr><td> valueIsAdjusting </td><td> model </td><td> no </td></tr> * </table> * * <p> * The various behavioral aspects of these properties follows: * </p> * * <ul> * <li> * When non-bound properties stored in the slider change, the slider fires * ChangeEvents to its ChangeListeners. * </li> * <li> * When bound properties stored in the slider change, the slider fires * PropertyChangeEvents to its PropertyChangeListeners * </li> * <li> * If any of the model's properties change, it fires a ChangeEvent to its * ChangeListeners, which include the slider. * </li> * <li> * If the slider receives a ChangeEvent from its model, it will propagate the * ChangeEvent to its ChangeListeners, with the ChangeEvent's "source" * property set to refer to the slider, rather than the model. * </li> * </ul> */public class JSlider extends JComponent implements SwingConstants, Accessible, ImageObserver, MenuContainer, Serializable{ /** DOCUMENT ME! */ private static final long serialVersionUID = -1441275936141218479L; /** * DOCUMENT ME! */ // FIXME: This inner class is a complete stub and needs to be implemented // properly. protected class AccessibleJSlider extends JComponent.AccessibleJComponent implements AccessibleValue { private static final long serialVersionUID = -6301740148041106789L; /** * Creates a new AccessibleJSlider object. */ protected AccessibleJSlider() { // Nothing to do here. } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public AccessibleStateSet getAccessibleStateSet() { return null; } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public AccessibleRole getAccessibleRole() { return null; } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public AccessibleValue getAccessibleValue() { return null; } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public Number getCurrentAccessibleValue() { return null; } /** * setCurrentAccessibleValue * * @param value0 TODO * * @return boolean */ public boolean setCurrentAccessibleValue(Number value0) { return false; } /** * getMinimumAccessibleValue * * @return Number */ public Number getMinimumAccessibleValue() { return null; } /** * getMaximumAccessibleValue * * @return Number */ public Number getMaximumAccessibleValue() { return null; } } /** Whether or not this slider paints its ticks. */ private transient boolean paintTicks = false; /** Whether or not this slider paints its track. */ private transient boolean paintTrack = true; /** Whether or not this slider paints its labels. */ private transient boolean paintLabels = false; /** * A dictionary of (Integer, Component) pairs where each Component is a * JLabel and the Integer determines where the label will be painted. */ private transient Dictionary labelTable; /** The model used to describe the slider. */ protected BoundedRangeModel sliderModel; /** The space between major ticks. */ protected int majorTickSpacing; /** The space between minor ticks. */ protected int minorTickSpacing; /** Whether the slider snaps its values to ticks. */ protected boolean snapToTicks = false; /** The orientation of the slider. */ protected int orientation = HORIZONTAL; /** Whether the slider is inverted. */ private transient boolean isInverted; /** The ChangeListener that listens to the model. */ protected ChangeListener changeListener; /** The ChangeEvent that is passed to all listeners of this slider. */ protected transient ChangeEvent changeEvent; /** * Creates a new horizontal JSlider object with a minimum of 0, a maximum of * 100, and a value of 50. */ public JSlider() { this(HORIZONTAL, 0, 100, 50); } /** * Creates a new JSlider object with the given orientation and a minimum of * 0, a maximum of 100, and a value of 50. * * @param orientation The orientation of the slider ({@link #HORIZONTAL} or * {@link #VERTICAL}). * * @throws IllegalArgumentException if <code>orientation</code> is not one of * the specified values. */ public JSlider(int orientation) { this(orientation, 0, 100, 50); } /** * Creates a new horizontal JSlider object with the given maximum and * minimum and a value that is halfway between the minimum and the * maximum. * * @param minimum The minimum value of the JSlider. * @param maximum The maximum value of the JSlider. */ public JSlider(int minimum, int maximum) { this(HORIZONTAL, minimum, maximum, (maximum + minimum) / 2); } /** * Creates a new horizontal JSlider object with the given minimum, maximum, * and value. * * @param minimum The minimum value of the JSlider. * @param maximum The maximum value of the JSlider. * @param value The initial value of the JSlider. */ public JSlider(int minimum, int maximum, int value) { this(HORIZONTAL, minimum, maximum, value); } /** * Creates a new JSlider object with the given orientation, minimum, * maximum, and value. * * @param orientation The orientation of the slider ({@link #HORIZONTAL} or * {@link #VERTICAL}). * @param minimum The minimum value of the JSlider. * @param maximum The maximum value of the JSlider. * @param value The initial value of the JSlider. * * @throws IllegalArgumentException if <code>orientation</code> is not one of * the specified values. */ public JSlider(int orientation, int minimum, int maximum, int value) { sliderModel = new DefaultBoundedRangeModel(value, 0, minimum, maximum); if (orientation != HORIZONTAL && orientation != VERTICAL) throw new IllegalArgumentException(orientation + " is not a legal orientation"); this.orientation = orientation; changeListener = createChangeListener(); sliderModel.addChangeListener(changeListener); updateUI(); } /** * Creates a new horizontal JSlider object with the given model. * * @param model The model (<code>null</code> not permitted). * * @throws NullPointerException if <code>model</code> is <code>null</code>. */ public JSlider(BoundedRangeModel model) { sliderModel = model; changeListener = createChangeListener(); sliderModel.addChangeListener(changeListener); updateUI(); } /** * This method returns the current value of the slider. * * @return The value of the slider stored in the model. */ public int getValue() { return sliderModel.getValue(); } /** * This method sets the value of the slider. * * @param value The slider's new value. */ public void setValue(int value) { sliderModel.setValue(value); } /** * This method returns the slider's UI delegate. * * @return The slider's UI delegate. */ public SliderUI getUI() { return (SliderUI) ui; } /** * This method sets the slider's UI delegate. * * @param ui A SliderUI object to use with this slider. */ public void setUI(SliderUI ui) { super.setUI(ui); } /** * This method sets this slider's UI to the UIManager's default for the * current look and feel. */ public void updateUI() { setUI((SliderUI) UIManager.getUI(this)); } /** * This method returns a name to identify which look and feel class will be * the UI delegate for the slider. * * @return The Look and Feel classID. "SliderUI" */ public String getUIClassID() { return "SliderUI"; } /** * Creates a ChangeListener for this Slider. * * @return A new ChangeListener. */ protected ChangeListener createChangeListener() { return new ChangeListener() { public void stateChanged(ChangeEvent ce) { // No need to trigger a repaint since the UI listens to the model // as well. All we need to do is pass on the stateChanged event // to our listeners. fireStateChanged(); } }; } /** * This method registers a listener to this slider. The listener will be * informed of new ChangeEvents. * * @param listener The listener to register. */ public void addChangeListener(ChangeListener listener) { listenerList.add(ChangeListener.class, listener); } /** * This method removes a listener from this slider. * * @param listener The listener to remove. */ public void removeChangeListener(ChangeListener listener) { listenerList.remove(ChangeListener.class, listener); } /** * This method is called whenever the model fires a ChangeEvent. It should * propagate the ChangeEvent to its listeners with a new ChangeEvent that * identifies the slider as the source. */ protected void fireStateChanged() { Object[] changeListeners = listenerList.getListenerList(); if (changeEvent == null) changeEvent = new ChangeEvent(this); for (int i = changeListeners.length - 2; i >= 0; i -= 2) { if (changeListeners[i] == ChangeListener.class) ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent); } } /** * This method returns an array of all ChangeListeners listening to this * slider. * * @return An array of ChangeListeners listening to this slider. */ public ChangeListener[] getChangeListeners() { return (ChangeListener[]) listenerList.getListeners(ChangeListener.class); } /** * This method returns the model of the slider. * * @return The slider's model. */ public BoundedRangeModel getModel()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -