📄 basicsliderui.java
字号:
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) { 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 class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class ScrollListener implements ActionListener { // changed this class to public to avoid bogus IllegalAccessException // bug in 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 class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class ComponentHandler extends ComponentAdapter { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void componentResized(ComponentEvent e) { getHandler().componentResized(e); } }; /** * Focus-change listener. * <p> * This class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class FocusHandler implements FocusListener { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Handler. If you need to add // new functionality add it to the Handler, but make sure this // class calls into the Handler. public void focusGained(FocusEvent e) { getHandler().focusGained(e); } public void focusLost(FocusEvent e) { getHandler().focusLost(e); } } /** * 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 class should be treated as a "protected" inner class. * Instantiate it only within subclasses of <Foo>. */ public class ActionScroller extends AbstractAction { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Actions. If you need to add // new functionality add it to the Actions, but make sure this // class calls into the Actions. 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) { SHARED_ACTION.scroll(slider, BasicSliderUI.this, dir, block); } 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 { // NOTE: This class exists only for backward compatability. All // its functionality has been moved into Actions. If you need to add // new functionality add it to the Actions, but make sure this // class calls into the Actions. int dir; boolean block; public SharedActionScroller(int dir, boolean block) { this.dir = dir; this.block = block; } public void actionPerformed(ActionEvent evt) { JSlider slider = (JSlider)evt.getSource(); BasicSliderUI ui = (BasicSliderUI)BasicLookAndFeel.getUIOfType( slider.getUI(), BasicSliderUI.class); if (ui == null) { return; } SHARED_ACTION.scroll(slider, ui, dir, block); } } private static class Actions extends UIAction { public static final String POSITIVE_UNIT_INCREMENT = "positiveUnitIncrement"; public static final String POSITIVE_BLOCK_INCREMENT = "positiveBlockIncrement"; public static final String NEGATIVE_UNIT_INCREMENT = "negativeUnitIncrement"; public static final String NEGATIVE_BLOCK_INCREMENT = "negativeBlockIncrement"; public static final String MIN_SCROLL_INCREMENT = "minScroll"; public static final String MAX_SCROLL_INCREMENT = "maxScroll"; Actions() { super(null); } public Actions(String name) { super(name); } public void actionPerformed(ActionEvent evt) { JSlider slider = (JSlider)evt.getSource(); BasicSliderUI ui = (BasicSliderUI)BasicLookAndFeel.getUIOfType( slider.getUI(), BasicSliderUI.class); String name = getName(); if (ui == null) { return; } if (POSITIVE_UNIT_INCREMENT == name) { scroll(slider, ui, POSITIVE_SCROLL, false); } else if (NEGATIVE_UNIT_INCREMENT == name) { scroll(slider, ui, NEGATIVE_SCROLL, false); } else if (POSITIVE_BLOCK_INCREMENT == name) { scroll(slider, ui, POSITIVE_SCROLL, true); } else if (NEGATIVE_BLOCK_INCREMENT == name) { scroll(slider, ui, NEGATIVE_SCROLL, true); } else if (MIN_SCROLL_INCREMENT == name) { scroll(slider, ui, MIN_SCROLL, false); } else if (MAX_SCROLL_INCREMENT == name) { scroll(slider, ui, MAX_SCROLL, false); } } private void scroll(JSlider slider, BasicSliderUI ui, int direction, boolean isBlock) { boolean invert = slider.getInverted(); if (direction == NEGATIVE_SCROLL || direction == POSITIVE_SCROLL) { if (invert) { direction = (direction == POSITIVE_SCROLL) ? NEGATIVE_SCROLL : POSITIVE_SCROLL; } if (isBlock) { ui.scrollByBlock(direction); } else { ui.scrollByUnit(direction); } } else { // MIN or MAX if (invert) { direction = (direction == MIN_SCROLL) ? MAX_SCROLL : MIN_SCROLL; } slider.setValue((direction == MIN_SCROLL) ? slider.getMinimum() : slider.getMaximum()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -