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

📄 basicsliderui.java

📁 java jdk 1.4的源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * Instantiate it only within subclasses of <Foo>.     */    public class ChangeHandler implements ChangeListener {        public void stateChanged(ChangeEvent e)                {	    if ( !isDragging ) {	        calculateThumbLocation();		slider.repaint();	    }        }    }    /////////////////////////////////////////////////////////////////////////    /// Track Listener Class    /////////////////////////////////////////////////////////////////////////            /**     * Track mouse movements.     *     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    public class TrackListener extends MouseInputAdapter {        protected transient int offset;        protected transient int currentMouseX, currentMouseY;        public void mouseReleased(MouseEvent e)                {            if ( !slider.isEnabled() )                return;            offset = 0;            scrollTimer.stop();            // This is the way we have to determine snap-to-ticks.  It's hard to explain            // but since ChangeEvents don't give us any idea what has changed we don't            // have a way to stop the thumb bounds from being recalculated.  Recalculating            // the thumb bounds moves the thumb over the current value (i.e., snapping            // to the ticks).            if ( slider.getSnapToTicks() /*|| slider.getSnapToValue()*/ ) {                isDragging = false;                slider.setValueIsAdjusting(false);            }            else {                slider.setValueIsAdjusting(false);                isDragging = false;            }	    	    slider.repaint();        }        /**        * If the mouse is pressed above the "thumb" component        * then reduce the scrollbars value by one page ("page up"),         * otherwise increase it by one page.  If there is no         * thumb then page up if the mouse is in the upper half        * of the track.        */        public void mousePressed(MouseEvent e)                {            if ( !slider.isEnabled() )                return;            currentMouseX = e.getX();            currentMouseY = e.getY();            if (slider.isRequestFocusEnabled()) {                slider.requestFocus();            }            // Clicked in the Thumb area?            if ( thumbRect.contains(currentMouseX, currentMouseY) ) {                switch ( slider.getOrientation() ) {                case JSlider.VERTICAL:                    offset = currentMouseY - thumbRect.y;                    break;                case JSlider.HORIZONTAL:                    offset = currentMouseX - thumbRect.x;                    break;                }                isDragging = true;                return;            }            isDragging = false;            slider.setValueIsAdjusting(true);            Dimension sbSize = slider.getSize();            int direction = POSITIVE_SCROLL;            switch ( slider.getOrientation() ) {            case JSlider.VERTICAL:                if ( thumbRect.isEmpty() ) {                    int scrollbarCenter = sbSize.height / 2;                    if ( !drawInverted() ) {                        direction = (currentMouseY < scrollbarCenter) ? POSITIVE_SCROLL : NEGATIVE_SCROLL;                    }                    else {                        direction = (currentMouseY < scrollbarCenter) ? NEGATIVE_SCROLL : POSITIVE_SCROLL;                    }                }                else {                    int thumbY = thumbRect.y;                    if ( !drawInverted() ) {                        direction = (currentMouseY < thumbY) ? POSITIVE_SCROLL : NEGATIVE_SCROLL;                    }                    else {                        direction = (currentMouseY < thumbY) ? NEGATIVE_SCROLL : POSITIVE_SCROLL;                    }                }                break;                                case JSlider.HORIZONTAL:                if ( thumbRect.isEmpty() ) {                    int scrollbarCenter = sbSize.width / 2;                    if ( !drawInverted() ) {                        direction = (currentMouseX < scrollbarCenter) ? NEGATIVE_SCROLL : POSITIVE_SCROLL;                    }                    else {                        direction = (currentMouseX < scrollbarCenter) ? POSITIVE_SCROLL : NEGATIVE_SCROLL;                    }                }                else {                    int thumbX = thumbRect.x;                    if ( !drawInverted() ) {                        direction = (currentMouseX < thumbX) ? NEGATIVE_SCROLL : POSITIVE_SCROLL;                    }                    else {                        direction = (currentMouseX < thumbX) ? POSITIVE_SCROLL : NEGATIVE_SCROLL;                    }                }                break;            }            scrollDueToClickInTrack(direction);            Rectangle r = thumbRect;            if ( !r.contains(currentMouseX, currentMouseY) ) {                if ( shouldScroll(direction) ) {                    scrollTimer.stop();                    scrollListener.setDirection(direction);                    scrollTimer.start();                }            }        }        public boolean shouldScroll(int direction) {            Rectangle r = thumbRect;            if ( slider.getOrientation() == JSlider.VERTICAL ) {                if ( drawInverted() ? direction < 0 : direction > 0 ) {                    if ( r.y + r.height  <= currentMouseY ) {                        return false;                    }                }                else if ( r.y >= currentMouseY ) {                    return false;                }            }            else {                if ( drawInverted() ? direction < 0 : direction > 0 ) {                    if ( r.x + r.width  >= currentMouseX ) {                        return false;                    }                }                else if ( r.x <= currentMouseX ) {                    return false;                }            }            if ( direction > 0 && slider.getValue() + slider.getExtent() >= slider.getMaximum() ) {                return false;            }            else if ( direction < 0 && slider.getValue() <= slider.getMinimum() ) {                return false;            }            return true;        }        /**         * Set the models value to the position of the top/left        * of the thumb relative to the origin of the track.        */        public void mouseDragged( MouseEvent e ) {                                BasicScrollBarUI ui;            int thumbMiddle = 0;            if ( !slider.isEnabled() )                return;            currentMouseX = e.getX();            currentMouseY = e.getY();            if ( !isDragging )                return;            slider.setValueIsAdjusting(true);            switch ( slider.getOrientation() ) {            case JSlider.VERTICAL:                      int halfThumbHeight = thumbRect.height / 2;                int thumbTop = e.getY() - offset;                int trackTop = trackRect.y;                int trackBottom = trackRect.y + (trackRect.height - 1);                int vMax = yPositionForValue(slider.getMaximum() -                                            slider.getExtent());                if (drawInverted()) {                    trackBottom = vMax;                }                else {                    trackTop = vMax;                }                thumbTop = Math.max( thumbTop, trackTop - halfThumbHeight );                thumbTop = Math.min( thumbTop, trackBottom - halfThumbHeight );                setThumbLocation(thumbRect.x, thumbTop);                thumbMiddle = thumbTop + halfThumbHeight;                slider.setValue( valueForYPosition( thumbMiddle ) );                break;            case JSlider.HORIZONTAL:                int halfThumbWidth = thumbRect.width / 2;                int thumbLeft = e.getX() - offset;                int trackLeft = trackRect.x;                int trackRight = trackRect.x + (trackRect.width - 1);                int hMax = xPositionForValue(slider.getMaximum() -                                            slider.getExtent());                if (drawInverted()) {                    trackLeft = hMax;                }                else {                    trackRight = hMax;                }                thumbLeft = Math.max( thumbLeft, trackLeft - halfThumbWidth );                thumbLeft = Math.min( thumbLeft, trackRight - halfThumbWidth );                setThumbLocation( thumbLeft, thumbRect.y);                thumbMiddle = thumbLeft + halfThumbWidth;                slider.setValue( valueForXPosition( thumbMiddle ) );                break;            default:                return;            }        }        public void mouseMoved(MouseEvent e)    {}         }    /**     * Scroll-event listener.     *     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    public class ScrollListener implements ActionListener {        // changed this class to public to avoid bogus IllegalAccessException bug i        // InternetExplorer browser.  It was protected.  Work around for 4109432        int direction = POSITIVE_SCROLL;        boolean useBlockIncrement;        public ScrollListener() {            direction = POSITIVE_SCROLL;            useBlockIncrement = true;        }        public ScrollListener(int dir, boolean block)   {            direction = dir;            useBlockIncrement = block;        }        public void setDirection(int direction) { this.direction = direction;}        public void setScrollByBlock(boolean block) { this.useBlockIncrement = block;}        public void actionPerformed(ActionEvent e) {            if ( useBlockIncrement ) {                scrollByBlock(direction);            }            else {                scrollByUnit(direction);            }            if ( !trackListener.shouldScroll(direction) ) {                ((Timer)e.getSource()).stop();            }        }    };     /**     * Listener for resizing events.     * <p>     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    public class ComponentHandler extends ComponentAdapter {        public void componentResized(ComponentEvent e)  {	    calculateGeometry();	    slider.repaint();        }    };      /**     * Focus-change listener.     * <p>     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    public class FocusHandler implements FocusListener {        public void focusGained(FocusEvent e) { slider.repaint();}         public void focusLost(FocusEvent e) { slider.repaint();}    };    /**     * As of Java 2 platform v1.3 this undocumented class is no longer used.     * The recommended approach to creating bindings is to use a     * combination of an <code>ActionMap</code>, to contain the action,     * and an <code>InputMap</code> to contain the mapping from KeyStroke     * to action description. The InputMap is is usually described in the     * LookAndFeel tables.     * <p>     * Please refer to the key bindings specification for further details.     * <p>     * This inner class is marked &quot;public&quot; due to a compiler bug.     * This class should be treated as a &quot;protected&quot; inner class.     * Instantiate it only within subclasses of <Foo>.     */    public class ActionScroller extends AbstractAction {        int dir;        boolean block;        JSlider slider;        public ActionScroller( JSlider slider, int dir, boolean block) {            this.dir = dir;            this.block = block;            this.slider = slider;        }        public void actionPerformed(ActionEvent e) {	    if ( dir == NEGATIVE_SCROLL || dir == POSITIVE_SCROLL ) {		int realDir = dir;		if ( drawInverted() ) {		    realDir = dir == NEGATIVE_SCROLL ? POSITIVE_SCROLL : NEGATIVE_SCROLL;		}				if ( block )		    scrollByBlock(realDir);		else		    scrollByUnit(realDir);	    }	    else {		if ( drawInverted() ) {		    if ( dir == MIN_SCROLL )			slider.setValue(slider.getMaximum());		    else if ( dir == MAX_SCROLL )			slider.setValue(slider.getMinimum());		}		else {		    if ( dir == MIN_SCROLL )			slider.setValue(slider.getMinimum());		    else if ( dir == MAX_SCROLL )			slider.setValue(slider.getMaximum());		}       	    }	}	public boolean isEnabled() { 	    boolean b = true;	    if (slider != null) {		b = slider.isEnabled();	    }	    return b;	}    };    /**     * A static version of the above.     */    static class SharedActionScroller extends AbstractAction {        int dir;        boolean block;        public SharedActionScroller(int dir, boolean block) {            this.dir = dir;            this.block = block;        }        public void actionPerformed(ActionEvent e) {	    JSlider slider = (JSlider)e.getSource();	    if ( dir == NEGATIVE_SCROLL || dir == POSITIVE_SCROLL ) {		int realDir = dir;		BasicSliderUI ui = (BasicSliderUI)slider.getUI();		if ( slider.getInverted() ) {		    realDir = dir == NEGATIVE_SCROLL ? POSITIVE_SCROLL : NEGATIVE_SCROLL;		}				if ( block )		    ui.scrollByBlock(realDir);		else		    ui.scrollByUnit(realDir);	    }	    else {		if ( slider.getInverted() ) {		    if ( dir == MIN_SCROLL )			slider.setValue(slider.getMaximum());		    else if ( dir == MAX_SCROLL )			slider.setValue(slider.getMinimum());		}		else {		    if ( dir == MIN_SCROLL )			slider.setValue(slider.getMinimum());		    else if ( dir == MAX_SCROLL )			slider.setValue(slider.getMaximum());		}       	    }	}    }}

⌨️ 快捷键说明

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