📄 basicscrollbarui.java
字号:
public void mouseReleased(MouseEvent e) { 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(); 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(); } 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(); startScrollTimerIfNecessary(); } } private void setValueFrom(MouseEvent e) { 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()); } } else { float valueMax = model.getMaximum() - model.getExtent(); float valueRange = valueMax - model.getMinimum(); float thumbValue = thumbPos - thumbMin; float thumbRange = thumbMax - thumbMin; int value; if (scrollbar.getOrientation() == JScrollBar.VERTICAL || scrollbar.getComponentOrientation().isLeftToRight()) { value = (int)(0.5 + ((thumbValue / thumbRange) * valueRange)); } else { value = (int)(0.5 + (((thumbMax - thumbPos) / thumbRange) * valueRange)); } scrollbar.setValue(value + model.getMinimum()); } } private void startScrollTimerIfNecessary() { if (scrollTimer.isRunning()) { return; } switch (scrollbar.getOrientation()) { case JScrollBar.VERTICAL: if (direction >0) { if (getThumbBounds().y + getThumbBounds().height < trackListener.currentMouseY) { scrollTimer.start(); } } else if (getThumbBounds().y > trackListener.currentMouseY) { scrollTimer.start(); } break; case JScrollBar.HORIZONTAL: if (direction >0) { if (getThumbBounds().x + getThumbBounds().width < trackListener.currentMouseX) { scrollTimer.start(); } } else if (getThumbBounds().x > trackListener.currentMouseX) { scrollTimer.start(); } break; } } public void mouseMoved(MouseEvent e) { } } /** * Listener for cursor keys. */ protected class ArrowButtonListener extends MouseAdapter { // Because we are handling both mousePressed and Actions // we need to make sure we don't fire under both conditions. // (keyfocus on scrollbars causes action without mousePress boolean handledEvent; public void mousePressed(MouseEvent e) { if(!scrollbar.isEnabled()) { return; } // not an unmodified left mouse button //if(e.getModifiers() != InputEvent.BUTTON1_MASK) {return; } if( ! SwingUtilities.isLeftMouseButton(e)) { return; } int direction = (e.getSource() == incrButton) ? 1 : -1; scrollByUnit(direction); scrollTimer.stop(); scrollListener.setDirection(direction); scrollListener.setScrollByBlock(false); scrollTimer.start(); handledEvent = true; if (!scrollbar.hasFocus() && scrollbar.isRequestFocusEnabled()) { scrollbar.requestFocus(); } } public void mouseReleased(MouseEvent e) { scrollTimer.stop(); handledEvent = false; scrollbar.setValueIsAdjusting(false); } } /** * Listener for scrolling events initiated in the * <code>ScrollPane</code>. */ protected class ScrollListener implements ActionListener { int direction = +1; boolean useBlockIncrement; public ScrollListener() { direction = +1; useBlockIncrement = false; } 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); // Stop scrolling if the thumb catches up with the mouse if(scrollbar.getOrientation() == JScrollBar.VERTICAL) { if(direction > 0) { if(getThumbBounds().y + getThumbBounds().height >= trackListener.currentMouseY) ((Timer)e.getSource()).stop(); } else if(getThumbBounds().y <= trackListener.currentMouseY) { ((Timer)e.getSource()).stop(); } } else { if(direction > 0) { if(getThumbBounds().x + getThumbBounds().width >= trackListener.currentMouseX) ((Timer)e.getSource()).stop(); } else if(getThumbBounds().x <= trackListener.currentMouseX) { ((Timer)e.getSource()).stop(); } } } else { scrollByUnit(direction); } if(direction > 0 && scrollbar.getValue()+scrollbar.getVisibleAmount() >= scrollbar.getMaximum()) ((Timer)e.getSource()).stop(); else if(direction < 0 && scrollbar.getValue() <= scrollbar.getMinimum()) ((Timer)e.getSource()).stop(); } } public class PropertyChangeHandler implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if ("model".equals(propertyName)) { BoundedRangeModel oldModel = (BoundedRangeModel)e.getOldValue(); BoundedRangeModel newModel = (BoundedRangeModel)e.getNewValue(); oldModel.removeChangeListener(modelListener); newModel.addChangeListener(modelListener); scrollbar.repaint(); scrollbar.revalidate(); } else if ("orientation".equals(propertyName)) { Integer orient = (Integer)e.getNewValue(); if (scrollbar.getComponentOrientation().isLeftToRight()) { if (incrButton instanceof BasicArrowButton) { ((BasicArrowButton)incrButton).setDirection(orient.intValue() == HORIZONTAL? EAST : SOUTH); } if (decrButton instanceof BasicArrowButton) { ((BasicArrowButton)decrButton).setDirection(orient.intValue() == HORIZONTAL? WEST : NORTH); } } else { if (incrButton instanceof BasicArrowButton) { ((BasicArrowButton)incrButton).setDirection(orient.intValue() == HORIZONTAL? WEST : SOUTH); } if (decrButton instanceof BasicArrowButton) { ((BasicArrowButton)decrButton).setDirection(orient.intValue() == HORIZONTAL? EAST : NORTH); } } } else if ("componentOrientation".equals(propertyName)) { ComponentOrientation co = scrollbar.getComponentOrientation(); incrButton.setComponentOrientation(co); decrButton.setComponentOrientation(co); if (scrollbar.getOrientation() == JScrollBar.HORIZONTAL) { if (co.isLeftToRight()) { if (incrButton instanceof BasicArrowButton) { ((BasicArrowButton)incrButton).setDirection(EAST); } if (decrButton instanceof BasicArrowButton) { ((BasicArrowButton)decrButton).setDirection(WEST); } } else { if (incrButton instanceof BasicArrowButton) { ((BasicArrowButton)incrButton).setDirection(WEST); } if (decrButton instanceof BasicArrowButton) { ((BasicArrowButton)decrButton).setDirection(EAST); } } } InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED); SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED, inputMap); } } } /** * Used for scrolling the scrollbar. */ private static class SharedActionScroller extends AbstractAction { private int dir; private boolean block; SharedActionScroller(int dir, boolean block) { this.dir = dir; this.block = block; } public void actionPerformed(ActionEvent e) { JScrollBar scrollBar = (JScrollBar)e.getSource(); if (dir == NEGATIVE_SCROLL || dir == POSITIVE_SCROLL) { int amount; // Don't use the BasicScrollBarUI.scrollByXXX methods as we // don't want to use an invokeLater to reset the trackHighlight // via an invokeLater if (block) { if (dir == NEGATIVE_SCROLL) { amount = -1 * scrollBar.getBlockIncrement(-1); } else { amount = scrollBar.getBlockIncrement(1); } } else { if (dir == NEGATIVE_SCROLL) { amount = -1 * scrollBar.getUnitIncrement(-1); } else { amount = scrollBar.getUnitIncrement(1); } } scrollBar.setValue(scrollBar.getValue() + amount); } else if (dir == MIN_SCROLL) { scrollBar.setValue(scrollBar.getMinimum()); } else if (dir == MAX_SCROLL) { scrollBar.setValue(scrollBar.getMaximum()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -