📄 basicscrollbarui.java
字号:
} 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)); } useCachedValue = true; scrollBarValue = value + model.getMinimum(); scrollbar.setValue(adjustValueIfNecessary(scrollBarValue)); } setThumbRollover(active); } private int adjustValueIfNecessary(int value) { if (scrollbar.getParent() instanceof JScrollPane) { JScrollPane scrollpane = (JScrollPane)scrollbar.getParent(); JViewport viewport = scrollpane.getViewport(); Component view = viewport.getView(); if (view instanceof JList) { JList list = (JList)view; if (DefaultLookup.getBoolean(list, list.getUI(), "List.lockToPositionOnScroll", false)) { int adjustedValue = value; int mode = list.getLayoutOrientation(); int orientation = scrollbar.getOrientation(); if (orientation == JScrollBar.VERTICAL && mode == JList.VERTICAL) { int index = list.locationToIndex(new Point(0, value)); Rectangle rect = list.getCellBounds(index, index); if (rect != null) { adjustedValue = rect.y; } } if (orientation == JScrollBar.HORIZONTAL && (mode == JList.VERTICAL_WRAP || mode == JList.HORIZONTAL_WRAP)) { if (scrollpane.getComponentOrientation().isLeftToRight()) { int index = list.locationToIndex(new Point(value, 0)); Rectangle rect = list.getCellBounds(index, index); if (rect != null) { adjustedValue = rect.x; } } else { Point loc = new Point(value, 0); int extent = viewport.getExtentSize().width; loc.x += extent - 1; int index = list.locationToIndex(loc); Rectangle rect = list.getCellBounds(index, index); if (rect != null) { adjustedValue = rect.x + rect.width - extent; } } } value = adjustedValue; } } } return value; } private void startScrollTimerIfNecessary() { if (scrollTimer.isRunning()) { return; } Rectangle tb = getThumbBounds(); switch (scrollbar.getOrientation()) { case JScrollBar.VERTICAL: if (direction > 0) { if (tb.y + tb.height < trackListener.currentMouseY) { scrollTimer.start(); } } else if (tb.y > trackListener.currentMouseY) { scrollTimer.start(); } break; case JScrollBar.HORIZONTAL: if ((direction > 0 && isMouseAfterThumb()) || (direction < 0 && isMouseBeforeThumb())) { scrollTimer.start(); } break; } } public void mouseMoved(MouseEvent e) { if (!isDragging) { updateThumbState(e.getX(), e.getY()); } } /** * Invoked when the mouse exits the scrollbar. * * @param e MouseEvent further describing the event * @since 1.5 */ public void mouseExited(MouseEvent e) { if (!isDragging) { setThumbRollover(false); } } } /** * 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 && !isMouseAfterThumb()) || (direction < 0 && !isMouseBeforeThumb())) { ((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(); } } private boolean isMouseLeftOfThumb() { return trackListener.currentMouseX < getThumbBounds().x; } private boolean isMouseRightOfThumb() { Rectangle tb = getThumbBounds(); return trackListener.currentMouseX > tb.x + tb.width; } private boolean isMouseBeforeThumb() { return scrollbar.getComponentOrientation().isLeftToRight() ? isMouseLeftOfThumb() : isMouseRightOfThumb(); } private boolean isMouseAfterThumb() { return scrollbar.getComponentOrientation().isLeftToRight() ? isMouseRightOfThumb() : isMouseLeftOfThumb(); } private void updateButtonDirections() { int orient = scrollbar.getOrientation(); if (scrollbar.getComponentOrientation().isLeftToRight()) { if (incrButton instanceof BasicArrowButton) { ((BasicArrowButton)incrButton).setDirection( orient == HORIZONTAL? EAST : SOUTH); } if (decrButton instanceof BasicArrowButton) { ((BasicArrowButton)decrButton).setDirection( orient == HORIZONTAL? WEST : NORTH); } } else { if (incrButton instanceof BasicArrowButton) { ((BasicArrowButton)incrButton).setDirection( orient == HORIZONTAL? WEST : SOUTH); } if (decrButton instanceof BasicArrowButton) { ((BasicArrowButton)decrButton).setDirection( orient == HORIZONTAL ? EAST : NORTH); } } } public class PropertyChangeHandler implements PropertyChangeListener { // 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 propertyChange(PropertyChangeEvent e) { getHandler().propertyChange(e); } } /** * Used for scrolling the scrollbar. */ private static class Actions extends UIAction { private static final String POSITIVE_UNIT_INCREMENT = "positiveUnitIncrement"; private static final String POSITIVE_BLOCK_INCREMENT = "positiveBlockIncrement"; private static final String NEGATIVE_UNIT_INCREMENT = "negativeUnitIncrement"; private static final String NEGATIVE_BLOCK_INCREMENT = "negativeBlockIncrement"; private static final String MIN_SCROLL = "minScroll"; private static final String MAX_SCROLL = "maxScroll"; Actions(String name) { super(name); } public void actionPerformed(ActionEvent e) { JScrollBar scrollBar = (JScrollBar)e.getSource(); String key = getName(); if (key == POSITIVE_UNIT_INCREMENT) { scroll(scrollBar, POSITIVE_SCROLL, false); } else if (key == POSITIVE_BLOCK_INCREMENT) { scroll(scrollBar, POSITIVE_SCROLL, true); } else if (key == NEGATIVE_UNIT_INCREMENT) { scroll(scrollBar, NEGATIVE_SCROLL, false); } else if (key == NEGATIVE_BLOCK_INCREMENT) { scroll(scrollBar, NEGATIVE_SCROLL, true); } else if (key == MIN_SCROLL) { scroll(scrollBar, BasicScrollBarUI.MIN_SCROLL, true); } else if (key == MAX_SCROLL) { scroll(scrollBar, BasicScrollBarUI.MAX_SCROLL, true); } } private void scroll(JScrollBar scrollBar, int dir, boolean block) { 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 == BasicScrollBarUI.MIN_SCROLL) { scrollBar.setValue(scrollBar.getMinimum()); } else if (dir == BasicScrollBarUI.MAX_SCROLL) { scrollBar.setValue(scrollBar.getMaximum()); } } } // // EventHandler // private class Handler implements FocusListener, PropertyChangeListener { // // FocusListener // public void focusGained(FocusEvent e) { scrollbar.repaint(); } public void focusLost(FocusEvent e) { scrollbar.repaint(); } // // PropertyChangeListener // public void propertyChange(PropertyChangeEvent e) { String propertyName = e.getPropertyName(); if ("model" == propertyName) { BoundedRangeModel oldModel = (BoundedRangeModel)e.getOldValue(); BoundedRangeModel newModel = (BoundedRangeModel)e.getNewValue(); oldModel.removeChangeListener(modelListener); newModel.addChangeListener(modelListener); scrollbar.repaint(); scrollbar.revalidate(); } else if ("orientation" == propertyName) { updateButtonDirections(); } else if ("componentOrientation" == propertyName) { updateButtonDirections(); InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED); SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED, inputMap); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -