📄 basicscrollbarui.java
字号:
Dimension incrDims = incrButton.getPreferredSize(); Dimension decrDims = decrButton.getPreferredSize(); // calculate and update the track bounds SwingUtilities.calculateInnerArea(scrollbar, trackRect); trackRect.width -= incrDims.getWidth(); trackRect.width -= decrDims.getWidth(); trackRect.x += decrDims.getWidth(); updateThumbRect(); decrButton.setBounds(vr.x, vr.y, decrDims.width, trackRect.height); incrButton.setBounds(trackRect.x + trackRect.width, vr.y, incrDims.width, trackRect.height); } /** * This method lays out the scrollbar vertically. * * @param sb The JScrollBar to layout. */ protected void layoutVScrollbar(JScrollBar sb) { Rectangle vr = new Rectangle(); SwingUtilities.calculateInnerArea(scrollbar, vr); Dimension incrDims = incrButton.getPreferredSize(); Dimension decrDims = decrButton.getPreferredSize(); // Update rectangles SwingUtilities.calculateInnerArea(scrollbar, trackRect); trackRect.height -= incrDims.getHeight(); trackRect.height -= decrDims.getHeight(); trackRect.y += decrDims.getHeight(); updateThumbRect(); decrButton.setBounds(vr.x, vr.y, trackRect.width, decrDims.height); incrButton.setBounds(vr.x, trackRect.y + trackRect.height, trackRect.width, incrDims.height); } /** * Updates the thumb rect. */ void updateThumbRect() { int max = scrollbar.getMaximum(); int min = scrollbar.getMinimum(); int value = scrollbar.getValue(); int extent = scrollbar.getVisibleAmount(); if (max - extent <= min) { if (scrollbar.getOrientation() == JScrollBar.HORIZONTAL) { thumbRect.x = trackRect.x; thumbRect.y = trackRect.y; thumbRect.width = getMinimumThumbSize().width; thumbRect.height = trackRect.height; } else { thumbRect.x = trackRect.x; thumbRect.y = trackRect.y; thumbRect.width = trackRect.width; thumbRect.height = getMinimumThumbSize().height; } } else { if (scrollbar.getOrientation() == JScrollBar.HORIZONTAL) { thumbRect.x = trackRect.x; thumbRect.width = Math.max(extent * trackRect.width / (max - min), getMinimumThumbSize().width); int availableWidth = trackRect.width - thumbRect.width; thumbRect.x += (value - min) * availableWidth / (max - min - extent); thumbRect.y = trackRect.y; thumbRect.height = trackRect.height; } else { thumbRect.x = trackRect.x; thumbRect.height = Math.max(extent * trackRect.height / (max - min), getMinimumThumbSize().height); int availableHeight = trackRect.height - thumbRect.height; thumbRect.y = trackRect.y + (value - min) * availableHeight / (max - min - extent); thumbRect.width = trackRect.width; } } } /** * This method returns the minimum size required for the layout. * * @param scrollbarContainer The Container that is laid out. * * @return The minimum size. */ public Dimension minimumLayoutSize(Container scrollbarContainer) { return preferredLayoutSize(scrollbarContainer); } /** * This method is called when the component is painted. * * @param g The Graphics object to paint with. * @param c The JComponent to paint. */ public void paint(Graphics g, JComponent c) { paintTrack(g, c, getTrackBounds()); paintThumb(g, c, getThumbBounds()); if (trackHighlight == INCREASE_HIGHLIGHT) paintIncreaseHighlight(g); else if (trackHighlight == DECREASE_HIGHLIGHT) paintDecreaseHighlight(g); } /** * This method is called when repainting and the mouse is pressed in the * track. It paints the track below the thumb with the trackHighlight * color. * * @param g The Graphics object to paint with. */ protected void paintDecreaseHighlight(Graphics g) { Color saved = g.getColor(); g.setColor(trackHighlightColor); if (scrollbar.getOrientation() == HORIZONTAL) g.fillRect(trackRect.x, trackRect.y, thumbRect.x - trackRect.x, trackRect.height); else g.fillRect(trackRect.x, trackRect.y, trackRect.width, thumbRect.y - trackRect.y); g.setColor(saved); } /** * This method is called when repainting and the mouse is pressed in the * track. It paints the track above the thumb with the trackHighlight * color. * * @param g The Graphics objet to paint with. */ protected void paintIncreaseHighlight(Graphics g) { Color saved = g.getColor(); g.setColor(trackHighlightColor); if (scrollbar.getOrientation() == HORIZONTAL) g.fillRect(thumbRect.x + thumbRect.width, trackRect.y, trackRect.x + trackRect.width - thumbRect.x - thumbRect.width, trackRect.height); else g.fillRect(trackRect.x, thumbRect.y + thumbRect.height, trackRect.width, trackRect.y + trackRect.height - thumbRect.y - thumbRect.height); g.setColor(saved); } /** * This method paints the thumb. * * @param g The Graphics object to paint with. * @param c The Component that is being painted. * @param thumbBounds The thumb bounds. */ protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) { g.setColor(thumbColor); g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height); BasicGraphicsUtils.drawBezel(g, thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height, false, false, thumbDarkShadowColor, thumbDarkShadowColor, thumbHighlightColor, thumbHighlightColor); } /** * This method paints the track. * * @param g The Graphics object to paint with. * @param c The JComponent being painted. * @param trackBounds The track's bounds. */ protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) { Color saved = g.getColor(); g.setColor(trackColor); g.fill3DRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height, false); g.setColor(saved); } /** * This method returns the preferred size for the layout. * * @param scrollbarContainer The Container to find a size for. * * @return The preferred size for the layout. */ public Dimension preferredLayoutSize(Container scrollbarContainer) { if (scrollbarContainer instanceof JComponent) return getPreferredSize((JComponent) scrollbarContainer); else return null; } /** * This method removes a child component from the layout. * * @param child The child to remove. */ public void removeLayoutComponent(Component child) { // You should not be removing stuff from this component. } /** * The method scrolls the thumb by a block in the direction specified. * * @param direction The direction to scroll. */ protected void scrollByBlock(int direction) { scrollbar.setValue(scrollbar.getValue() + scrollbar.getBlockIncrement(direction)); } /** * The method scrolls the thumb by a unit in the direction specified. * * @param direction The direction to scroll. */ protected void scrollByUnit(int direction) { scrollbar.setValue(scrollbar.getValue() + scrollbar.getUnitIncrement(direction)); } /** * This method sets the thumb's bounds. * * @param x The X position of the thumb. * @param y The Y position of the thumb. * @param width The width of the thumb. * @param height The height of the thumb. */ protected void setThumbBounds(int x, int y, int width, int height) { thumbRect.x = x; thumbRect.y = y; thumbRect.width = width; thumbRect.height = height; } /** * This method uninstalls any components that are a part of or related to * this scrollbar. */ protected void uninstallComponents() { if (incrButton != null) scrollbar.remove(incrButton); if (decrButton != null) scrollbar.remove(decrButton); } /** * This method uninstalls any defaults that this scrollbar acquired from the * Basic Look and Feel defaults. */ protected void uninstallDefaults() { scrollbar.setForeground(null); scrollbar.setBackground(null); LookAndFeel.uninstallBorder(scrollbar); incrButton = null; decrButton = null; } /** * This method uninstalls any keyboard actions this scrollbar acquired * during install. */ protected void uninstallKeyboardActions() { // FIXME: implement. } /** * This method uninstalls any listeners that were registered during install. */ protected void uninstallListeners() { if (scrollTimer != null) scrollTimer.removeActionListener(scrollListener); if (scrollbar != null) { scrollbar.getModel().removeChangeListener(modelListener); scrollbar.removePropertyChangeListener(propertyChangeListener); scrollbar.removeMouseListener(trackListener); scrollbar.removeMouseMotionListener(trackListener); } if (decrButton != null) decrButton.removeMouseListener(buttonListener); if (incrButton != null) incrButton.removeMouseListener(buttonListener); propertyChangeListener = null; modelListener = null; buttonListener = null; trackListener = null; scrollListener = null; } /** * This method uninstalls the UI. This includes removing any defaults, * listeners, and components that this UI may have initialized. It also * nulls any instance data. * * @param c The Component to uninstall for. */ public void uninstallUI(JComponent c) { uninstallListeners(); uninstallDefaults(); uninstallComponents(); scrollTimer = null; thumbRect = null; trackRect = null; trackColor = null; trackHighlightColor = null; thumbColor = null; thumbHighlightColor = null; thumbDarkShadowColor = null; thumbLightShadowColor = null; scrollbar = null; } /** * This method returns the value in the scrollbar's range given the y * coordinate. If the value is out of range, it will return the closest * legal value. * This is package-private to avoid an accessor method. * * @param yPos The y coordinate to calculate a value for. * * @return The value for the y coordinate. */ int valueForYPosition(int yPos) { int min = scrollbar.getMinimum(); int max = scrollbar.getMaximum(); int len = trackRect.height; int value; // If the length is 0, you shouldn't be able to even see where the thumb is. // This really shouldn't ever happen, but just in case, we'll return the middle. if (len == 0) return ((max - min) / 2); value = ((yPos - trackRect.y) * (max - min) / len + min); // If this isn't a legal value, then we'll have to move to one now. if (value > max) value = max; else if (value < min) value = min; return value; } /** * This method returns the value in the scrollbar's range given the x * coordinate. If the value is out of range, it will return the closest * legal value. * This is package-private to avoid an accessor method. * * @param xPos The x coordinate to calculate a value for. * * @return The value for the x coordinate. */ int valueForXPosition(int xPos) { int min = scrollbar.getMinimum(); int max = scrollbar.getMaximum(); int len = trackRect.width; int value; // If the length is 0, you shouldn't be able to even see where the slider is. // This really shouldn't ever happen, but just in case, we'll return the middle. if (len == 0) return ((max - min) / 2); value = ((xPos - trackRect.x) * (max - min) / len + min); // If this isn't a legal value, then we'll have to move to one now. if (value > max) value = max; else if (value < min) value = min; return value; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -