📄 basicscrollbarui.java
字号:
* @see #getMinimumSize */ public Dimension getPreferredSize(JComponent c) { return (scrollbar.getOrientation() == JScrollBar.VERTICAL) ? new Dimension(scrollBarWidth, 48) : new Dimension(48, scrollBarWidth); } /** * @param c The JScrollBar that's delegating this method to us. * @return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); * @see #getMinimumSize * @see #getPreferredSize */ public Dimension getMaximumSize(JComponent c) { return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); } protected JButton createDecreaseButton(int orientation) { return new BasicArrowButton(orientation, UIManager.getColor("ScrollBar.thumb"), UIManager.getColor("ScrollBar.thumbShadow"), UIManager.getColor("ScrollBar.thumbDarkShadow"), UIManager.getColor("ScrollBar.thumbHighlight")); } protected JButton createIncreaseButton(int orientation) { return new BasicArrowButton(orientation, UIManager.getColor("ScrollBar.thumb"), UIManager.getColor("ScrollBar.thumbShadow"), UIManager.getColor("ScrollBar.thumbDarkShadow"), UIManager.getColor("ScrollBar.thumbHighlight")); } protected void paintDecreaseHighlight(Graphics g) { Insets insets = scrollbar.getInsets(); Rectangle thumbR = getThumbBounds(); g.setColor(trackHighlightColor); if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { int x = insets.left; int y = decrButton.getY() + decrButton.getHeight(); int w = scrollbar.getWidth() - (insets.left + insets.right); int h = thumbR.y - y; g.fillRect(x, y, w, h); } else { int x, w; if (scrollbar.getComponentOrientation().isLeftToRight()) { x = decrButton.getX() + decrButton.getWidth(); w = thumbR.x - x; } else { x = thumbR.x + thumbR.width; w = decrButton.getX() - x; } int y = insets.top; int h = scrollbar.getHeight() - (insets.top + insets.bottom); g.fillRect(x, y, w, h); } } protected void paintIncreaseHighlight(Graphics g) { Insets insets = scrollbar.getInsets(); Rectangle thumbR = getThumbBounds(); g.setColor(trackHighlightColor); if (scrollbar.getOrientation() == JScrollBar.VERTICAL) { int x = insets.left; int y = thumbR.y + thumbR.height; int w = scrollbar.getWidth() - (insets.left + insets.right); int h = incrButton.getY() - y; g.fillRect(x, y, w, h); } else { int x, w; if (scrollbar.getComponentOrientation().isLeftToRight()) { x = thumbR.x + thumbR.width; w = incrButton.getX() - x; } else { x = incrButton.getX() + incrButton.getWidth(); w = thumbR.x - x; } int y = insets.top; int h = scrollbar.getHeight() - (insets.top + insets.bottom); g.fillRect(x, y, w, h); } } protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) { g.setColor(trackColor); g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height); if(trackHighlight == DECREASE_HIGHLIGHT) { paintDecreaseHighlight(g); } else if(trackHighlight == INCREASE_HIGHLIGHT) { paintIncreaseHighlight(g); } } protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) { if(thumbBounds.isEmpty() || !scrollbar.isEnabled()) { return; } int w = thumbBounds.width; int h = thumbBounds.height; g.translate(thumbBounds.x, thumbBounds.y); g.setColor(thumbDarkShadowColor); g.drawRect(0, 0, w-1, h-1); g.setColor(thumbColor); g.fillRect(0, 0, w-1, h-1); g.setColor(thumbHighlightColor); g.drawLine(1, 1, 1, h-2); g.drawLine(2, 1, w-3, 1); g.setColor(thumbLightShadowColor); g.drawLine(2, h-2, w-2, h-2); g.drawLine(w-2, 1, w-2, h-3); g.translate(-thumbBounds.x, -thumbBounds.y); } /** * Return the smallest acceptable size for the thumb. If the scrollbar * becomes so small that this size isn't available, the thumb will be * hidden. * <p> * <b>Warning </b>: the value returned by this method should not be * be modified, it's a shared static constant. * * @return The smallest acceptable size for the thumb. * @see #getMaximumThumbSize */ protected Dimension getMinimumThumbSize() { return minimumThumbSize; } /** * Return the largest acceptable size for the thumb. To create a fixed * size thumb one make this method and <code>getMinimumThumbSize</code> * return the same value. * <p> * <b>Warning </b>: the value returned by this method should not be * be modified, it's a shared static constant. * * @return The largest acceptable size for the thumb. * @see #getMinimumThumbSize */ protected Dimension getMaximumThumbSize() { return maximumThumbSize; } /* * LayoutManager Implementation */ public void addLayoutComponent(String name, Component child) {} public void removeLayoutComponent(Component child) {} public Dimension preferredLayoutSize(Container scrollbarContainer) { return getPreferredSize((JComponent)scrollbarContainer); } public Dimension minimumLayoutSize(Container scrollbarContainer) { return getMinimumSize((JComponent)scrollbarContainer); } private int getValue(JScrollBar sb) { return (useCachedValue) ? scrollBarValue : sb.getValue(); } protected void layoutVScrollbar(JScrollBar sb) { Dimension sbSize = sb.getSize(); Insets sbInsets = sb.getInsets(); /* * Width and left edge of the buttons and thumb. */ int itemW = sbSize.width - (sbInsets.left + sbInsets.right); int itemX = sbInsets.left; /* Nominal locations of the buttons, assuming their preferred * size will fit. */ boolean squareButtons = DefaultLookup.getBoolean( scrollbar, this, "ScrollBar.squareButtons", false); int decrButtonH = squareButtons ? itemW : decrButton.getPreferredSize().height; int decrButtonY = sbInsets.top; int incrButtonH = squareButtons ? itemW : incrButton.getPreferredSize().height; int incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH); /* The thumb must fit within the height left over after we * subtract the preferredSize of the buttons and the insets. */ int sbInsetsH = sbInsets.top + sbInsets.bottom; int sbButtonsH = decrButtonH + incrButtonH; float trackH = sbSize.height - (sbInsetsH + sbButtonsH); /* Compute the height and origin of the thumb. The case * where the thumb is at the bottom edge is handled specially * to avoid numerical problems in computing thumbY. Enforce * the thumbs min/max dimensions. If the thumb doesn't * fit in the track (trackH) we'll hide it later. */ float min = sb.getMinimum(); float extent = sb.getVisibleAmount(); float range = sb.getMaximum() - min; float value = getValue(sb); int thumbH = (range <= 0) ? getMaximumThumbSize().height : (int)(trackH * (extent / range)); thumbH = Math.max(thumbH, getMinimumThumbSize().height); thumbH = Math.min(thumbH, getMaximumThumbSize().height); int thumbY = incrButtonY - thumbH; if (value < (sb.getMaximum() - sb.getVisibleAmount())) { float thumbRange = trackH - thumbH; thumbY = (int)(0.5f + (thumbRange * ((value - min) / (range - extent)))); thumbY += decrButtonY + decrButtonH; } /* If the buttons don't fit, allocate half of the available * space to each and move the lower one (incrButton) down. */ int sbAvailButtonH = (sbSize.height - sbInsetsH); if (sbAvailButtonH < sbButtonsH) { incrButtonH = decrButtonH = sbAvailButtonH / 2; incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH); } decrButton.setBounds(itemX, decrButtonY, itemW, decrButtonH); incrButton.setBounds(itemX, incrButtonY, itemW, incrButtonH); /* Update the trackRect field. */ int itrackY = decrButtonY + decrButtonH; int itrackH = incrButtonY - itrackY; trackRect.setBounds(itemX, itrackY, itemW, itrackH); /* If the thumb isn't going to fit, zero it's bounds. Otherwise * make sure it fits between the buttons. Note that setting the * thumbs bounds will cause a repaint. */ if(thumbH >= (int)trackH) { setThumbBounds(0, 0, 0, 0); } else { if ((thumbY + thumbH) > incrButtonY) { thumbY = incrButtonY - thumbH; } if (thumbY < (decrButtonY + decrButtonH)) { thumbY = decrButtonY + decrButtonH + 1; } setThumbBounds(itemX, thumbY, itemW, thumbH); } } protected void layoutHScrollbar(JScrollBar sb) { Dimension sbSize = sb.getSize(); Insets sbInsets = sb.getInsets(); /* Height and top edge of the buttons and thumb. */ int itemH = sbSize.height - (sbInsets.top + sbInsets.bottom); int itemY = sbInsets.top; boolean ltr = sb.getComponentOrientation().isLeftToRight(); /* Nominal locations of the buttons, assuming their preferred * size will fit. */ boolean squareButtons = DefaultLookup.getBoolean( scrollbar, this, "ScrollBar.squareButtons", false); int leftButtonW = squareButtons ? itemH : decrButton.getPreferredSize().width; int rightButtonW = squareButtons ? itemH : incrButton.getPreferredSize().width; if (!ltr) { int temp = leftButtonW; leftButtonW = rightButtonW; rightButtonW = temp; } int leftButtonX = sbInsets.left; int rightButtonX = sbSize.width - (sbInsets.right + rightButtonW); /* The thumb must fit within the width left over after we * subtract the preferredSize of the buttons and the insets. */ int sbInsetsW = sbInsets.left + sbInsets.right; int sbButtonsW = leftButtonW + rightButtonW; float trackW = sbSize.width - (sbInsetsW + sbButtonsW); /* Compute the width and origin of the thumb. Enforce * the thumbs min/max dimensions. The case where the thumb * is at the right edge is handled specially to avoid numerical * problems in computing thumbX. If the thumb doesn't * fit in the track (trackH) we'll hide it later. */ float min = sb.getMinimum(); float max = sb.getMaximum(); float extent = sb.getVisibleAmount(); float range = max - min; float value = getValue(sb); int thumbW = (range <= 0) ? getMaximumThumbSize().width : (int)(trackW * (extent / range)); thumbW = Math.max(thumbW, getMinimumThumbSize().width); thumbW = Math.min(thumbW, getMaximumThumbSize().width); int thumbX = ltr ? rightButtonX - thumbW : leftButtonX + leftButtonW; if (value < (max - sb.getVisibleAmount())) { float thumbRange = trackW - thumbW; if( ltr ) { thumbX = (int)(0.5f + (thumbRange * ((value - min) / (range - extent)))); } else { thumbX = (int)(0.5f + (thumbRange * ((max - extent - value) / (range - extent)))); } thumbX += leftButtonX + leftButtonW; } /* If the buttons don't fit, allocate half of the available * space to each and move the right one over. */ int sbAvailButtonW = (sbSize.width - sbInsetsW); if (sbAvailButtonW < sbButtonsW) { rightButtonW = leftButtonW = sbAvailButtonW / 2; rightButtonX = sbSize.width - (sbInsets.right + rightButtonW); } (ltr ? decrButton : incrButton).setBounds(leftButtonX, itemY, leftButtonW, itemH); (ltr ? incrButton : decrButton).setBounds(rightButtonX, itemY, rightButtonW, itemH); /* Update the trackRect field. */ int itrackX = leftButtonX + leftButtonW; int itrackW = rightButtonX - itrackX; trackRect.setBounds(itrackX, itemY, itrackW, itemH); /* Make sure the thumb fits between the buttons. Note * that setting the thumbs bounds causes a repaint. */ if (thumbW >= (int)trackW) { setThumbBounds(0, 0, 0, 0); } else { if (thumbX + thumbW > rightButtonX) { thumbX = rightButtonX - thumbW; } if (thumbX < leftButtonX + leftButtonW) { thumbX = leftButtonX + leftButtonW + 1; } setThumbBounds(thumbX, itemY, thumbW, itemH); } } public void layoutContainer(Container scrollbarContainer) { /* If the user is dragging the value, we'll assume that the * scrollbars layout is OK modulo the thumb which is being * handled by the dragging code. */ if (isDragging) { return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -