📄 basicscrollbarui.java
字号:
JScrollBar scrollbar = (JScrollBar)scrollbarContainer; switch (scrollbar.getOrientation()) { case JScrollBar.VERTICAL: layoutVScrollbar(scrollbar); break; case JScrollBar.HORIZONTAL: layoutHScrollbar(scrollbar); break; } } /** * Set the bounds of the thumb and force a repaint that includes * the old thumbBounds and the new one. * * @see #getThumbBounds */ protected void setThumbBounds(int x, int y, int width, int height) { /* If the thumbs bounds haven't changed, we're done. */ if ((thumbRect.x == x) && (thumbRect.y == y) && (thumbRect.width == width) && (thumbRect.height == height)) { return; } /* Update thumbRect, and repaint the union of x,y,w,h and * the old thumbRect. */ int minX = Math.min(x, thumbRect.x); int minY = Math.min(y, thumbRect.y); int maxX = Math.max(x + width, thumbRect.x + thumbRect.width); int maxY = Math.max(y + height, thumbRect.y + thumbRect.height); thumbRect.setBounds(x, y, width, height); scrollbar.repaint(minX, minY, maxX - minX, maxY - minY); // Once there is API to determine the mouse location this will need // to be changed. setThumbRollover(false); } /** * Return the current size/location of the thumb. * <p> * <b>Warning </b>: the value returned by this method should not be * be modified, it's a reference to the actual rectangle, not a copy. * * @return The current size/location of the thumb. * @see #setThumbBounds */ protected Rectangle getThumbBounds() { return thumbRect; } /** * Returns the current bounds of the track, i.e. the space in between * the increment and decrement buttons, less the insets. The value * returned by this method is updated each time the scrollbar is * laid out (validated). * <p> * <b>Warning </b>: the value returned by this method should not be * be modified, it's a reference to the actual rectangle, not a copy. * * @return the current bounds of the scrollbar track * @see #layoutContainer */ protected Rectangle getTrackBounds() { return trackRect; } /* * Method for scrolling by a block increment. * Added for mouse wheel scrolling support, RFE 4202656. */ static void scrollByBlock(JScrollBar scrollbar, int direction) { // This method is called from BasicScrollPaneUI to implement wheel // scrolling, and also from scrollByBlock(). int oldValue = scrollbar.getValue(); int blockIncrement = scrollbar.getBlockIncrement(direction); int delta = blockIncrement * ((direction > 0) ? +1 : -1); int newValue = oldValue + delta; // Check for overflow. if (delta > 0 && newValue < oldValue) { newValue = scrollbar.getMaximum(); } else if (delta < 0 && newValue > oldValue) { newValue = scrollbar.getMinimum(); } scrollbar.setValue(newValue); } protected void scrollByBlock(int direction) { scrollByBlock(scrollbar, direction); trackHighlight = direction > 0 ? INCREASE_HIGHLIGHT : DECREASE_HIGHLIGHT; Rectangle dirtyRect = getTrackBounds(); scrollbar.repaint(dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height); } /* * Method for scrolling by a unit increment. * Added for mouse wheel scrolling support, RFE 4202656. * * If limitByBlock is set to true, the scrollbar will scroll at least 1 * unit increment, but will not scroll farther than the block increment. * See BasicScrollPaneUI.Handler.mouseWheelMoved(). */ static void scrollByUnits(JScrollBar scrollbar, int direction, int units, boolean limitToBlock) { // This method is called from BasicScrollPaneUI to implement wheel // scrolling, as well as from scrollByUnit(). int delta; int limit = -1; if (limitToBlock) { if (direction < 0) { limit = scrollbar.getValue() - scrollbar.getBlockIncrement(direction); } else { limit = scrollbar.getValue() + scrollbar.getBlockIncrement(direction); } } for (int i=0; i<units; i++) { if (direction > 0) { delta = scrollbar.getUnitIncrement(direction); } else { delta = -scrollbar.getUnitIncrement(direction); } int oldValue = scrollbar.getValue(); int newValue = oldValue + delta; // Check for overflow. if (delta > 0 && newValue < oldValue) { newValue = scrollbar.getMaximum(); } else if (delta < 0 && newValue > oldValue) { newValue = scrollbar.getMinimum(); } if (oldValue == newValue) { break; } if (limitToBlock && i > 0) { assert limit != -1; if ((direction < 0 && newValue < limit) || (direction > 0 && newValue > limit)) { break; } } scrollbar.setValue(newValue); } } protected void scrollByUnit(int direction) { scrollByUnits(scrollbar, direction, 1, false); } /** * Indicates whether the user can absolutely position the thumb with * a mouse gesture (usually the middle mouse button). * * @return true if a mouse gesture can absolutely position the thumb * @since 1.5 */ public boolean getSupportsAbsolutePositioning() { return supportsAbsolutePositioning; } /** * A listener to listen for model changes. * */ protected class ModelListener implements ChangeListener { public void stateChanged(ChangeEvent e) { if (!useCachedValue) { scrollBarValue = scrollbar.getValue(); } layoutContainer(scrollbar); useCachedValue = false; } } /** * Track mouse drags. */ protected class TrackListener extends MouseAdapter implements MouseMotionListener { protected transient int offset; protected transient int currentMouseX, currentMouseY; private transient int direction = +1; public void mouseReleased(MouseEvent e) { if (isDragging) { updateThumbState(e.getX(), e.getY()); } if (SwingUtilities.isRightMouseButton(e) || (!getSupportsAbsolutePositioning() && SwingUtilities.isMiddleMouseButton(e))) return; if(!scrollbar.isEnabled()) return; Rectangle r = getTrackBounds(); scrollbar.repaint(r.x, r.y, r.width, r.height); trackHighlight = NO_HIGHLIGHT; isDragging = false; offset = 0; scrollTimer.stop(); useCachedValue = true; scrollbar.setValueIsAdjusting(false); } /** * 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 (SwingUtilities.isRightMouseButton(e) || (!getSupportsAbsolutePositioning() && SwingUtilities.isMiddleMouseButton(e))) return; if(!scrollbar.isEnabled()) return; if (!scrollbar.hasFocus() && scrollbar.isRequestFocusEnabled()) { scrollbar.requestFocus(); } useCachedValue = true; scrollbar.setValueIsAdjusting(true); currentMouseX = e.getX(); currentMouseY = e.getY(); // Clicked in the Thumb area? if(getThumbBounds().contains(currentMouseX, currentMouseY)) { switch (scrollbar.getOrientation()) { case JScrollBar.VERTICAL: offset = currentMouseY - getThumbBounds().y; break; case JScrollBar.HORIZONTAL: offset = currentMouseX - getThumbBounds().x; break; } isDragging = true; return; } else if (getSupportsAbsolutePositioning() && SwingUtilities.isMiddleMouseButton(e)) { switch (scrollbar.getOrientation()) { case JScrollBar.VERTICAL: offset = getThumbBounds().height / 2; break; case JScrollBar.HORIZONTAL: offset = getThumbBounds().width / 2; break; } isDragging = true; setValueFrom(e); return; } isDragging = false; Dimension sbSize = scrollbar.getSize(); direction = +1; switch (scrollbar.getOrientation()) { case JScrollBar.VERTICAL: if (getThumbBounds().isEmpty()) { int scrollbarCenter = sbSize.height / 2; direction = (currentMouseY < scrollbarCenter) ? -1 : +1; } else { int thumbY = getThumbBounds().y; direction = (currentMouseY < thumbY) ? -1 : +1; } break; case JScrollBar.HORIZONTAL: if (getThumbBounds().isEmpty()) { int scrollbarCenter = sbSize.width / 2; direction = (currentMouseX < scrollbarCenter) ? -1 : +1; } else { int thumbX = getThumbBounds().x; direction = (currentMouseX < thumbX) ? -1 : +1; } if (!scrollbar.getComponentOrientation().isLeftToRight()) { direction = -direction; } break; } scrollByBlock(direction); scrollTimer.stop(); scrollListener.setDirection(direction); scrollListener.setScrollByBlock(true); startScrollTimerIfNecessary(); } /** * Set the models value to the position of the thumb's top of Vertical * scrollbar, or the left/right of Horizontal scrollbar in * left-to-right/right-to-left scrollbar relative to the origin of the * track. */ public void mouseDragged(MouseEvent e) { if (SwingUtilities.isRightMouseButton(e) || (!getSupportsAbsolutePositioning() && SwingUtilities.isMiddleMouseButton(e))) return; if(!scrollbar.isEnabled() || getThumbBounds().isEmpty()) { return; } if (isDragging) { setValueFrom(e); } else { currentMouseX = e.getX(); currentMouseY = e.getY(); updateThumbState(currentMouseX, currentMouseY); startScrollTimerIfNecessary(); } } private void setValueFrom(MouseEvent e) { boolean active = isThumbRollover(); BoundedRangeModel model = scrollbar.getModel(); Rectangle thumbR = getThumbBounds(); float trackLength; int thumbMin, thumbMax, thumbPos; if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { thumbMin = decrButton.getY() + decrButton.getHeight(); thumbMax = incrButton.getY() - thumbR.height; thumbPos = Math.min(thumbMax, Math.max(thumbMin, (e.getY() - offset))); setThumbBounds(thumbR.x, thumbPos, thumbR.width, thumbR.height); trackLength = getTrackBounds().height; } else { if (scrollbar.getComponentOrientation().isLeftToRight()) { thumbMin = decrButton.getX() + decrButton.getWidth(); thumbMax = incrButton.getX() - thumbR.width; } else { thumbMin = incrButton.getX() + incrButton.getWidth(); thumbMax = decrButton.getX() - thumbR.width; } thumbPos = Math.min(thumbMax, Math.max(thumbMin, (e.getX() - offset))); setThumbBounds(thumbPos, thumbR.y, thumbR.width, thumbR.height); trackLength = getTrackBounds().width; } /* Set the scrollbars value. If the thumb has reached the end of * the scrollbar, then just set the value to its maximum. Otherwise * compute the value as accurately as possible. */ if (thumbPos == thumbMax) { if (scrollbar.getOrientation() == JScrollBar.VERTICAL || scrollbar.getComponentOrientation().isLeftToRight()) { scrollbar.setValue(model.getMaximum() - model.getExtent()); } else { scrollbar.setValue(model.getMinimum()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -