basicscrollbarui.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,300 行 · 第 1/3 页
JAVA
1,300 行
scrollTimer.setRepeats(true); installComponents(); installListeners(); installDefaults(); configureScrollBarColors(); calculatePreferredSize(); layoutContainer(scrollbar); } } /** * This method lays out the scrollbar. * * @param scrollbarContainer The Container to layout. */ public void layoutContainer(Container scrollbarContainer) { if (scrollbarContainer instanceof JScrollBar) { if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL) layoutHScrollbar((JScrollBar) scrollbarContainer); else layoutVScrollbar((JScrollBar) scrollbarContainer); } } /** * This method lays out the scrollbar horizontally. * * @param sb The JScrollBar to layout. */ protected void layoutHScrollbar(JScrollBar sb) { // All we have to do is layout the 2 buttons? Rectangle vr = new Rectangle(); SwingUtilities.calculateInnerArea(scrollbar, vr); // Update the rectangles. getTrackBounds(); getThumbBounds(); Dimension incrDims = incrButton.getPreferredSize(); Dimension decrDims = decrButton.getPreferredSize(); 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); // Update rectangles getTrackBounds(); getThumbBounds(); Dimension incrDims = incrButton.getPreferredSize(); Dimension decrDims = decrButton.getPreferredSize(); decrButton.setBounds(vr.x, vr.y, trackRect.width, decrDims.height); incrButton.setBounds(vr.x, trackRect.y + trackRect.height, trackRect.width, incrDims.height); } /** * 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) { layoutContainer(scrollbar); 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) { Color saved = g.getColor(); Point x; Point y; Point z; Polygon lines; g.setColor(thumbHighlightColor); x = new Point(thumbBounds.x + 1, thumbBounds.y + 1); y = new Point(x); y.translate(thumbBounds.width - 2, 0); z = new Point(x); z.translate(0, thumbBounds.height - 2); lines = new Polygon(new int[] { x.x, y.x, z.x }, new int[] { x.y, y.y, z.y }, 3); g.drawPolygon(lines); g.setColor(thumbLightShadowColor); x = new Point(thumbBounds.x + thumbBounds.width - 1, thumbBounds.y + thumbBounds.height - 1); y = new Point(x); y.translate(-(thumbBounds.width - 2), 0); z = new Point(x); z.translate(0, -(thumbBounds.height - 2)); lines = new Polygon(new int[] { x.x, y.x, z.x }, new int[] { x.y, y.y, z.y }, 3); g.drawPolygon(lines); g.setColor(thumbDarkShadowColor); x = new Point(thumbBounds.x + thumbBounds.width, thumbBounds.y + thumbBounds.height); y = new Point(x); y.translate(-thumbBounds.width, 0); z = new Point(x); z.translate(0, -thumbBounds.height); lines = new Polygon(new int[] { x.x, y.x, z.x }, new int[] { x.y, y.y, z.y }, 3); g.drawPolygon(lines); g.setColor(thumbColor); g.fillRect(thumbBounds.x, thumbBounds.y, thumbBounds.width, thumbBounds.height); g.setColor(saved); } /** * 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() { scrollbar.remove(incrButton); scrollbar.remove(decrButton); incrButton = null; decrButton = null; } /** * 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); scrollbar.setBorder(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() { scrollTimer.removeActionListener(scrollListener); scrollbar.getModel().removeChangeListener(modelListener); scrollbar.removePropertyChangeListener(propertyChangeListener); decrButton.removeMouseListener(buttonListener); incrButton.removeMouseListener(buttonListener); scrollbar.removeMouseListener(trackListener); scrollbar.removeMouseMotionListener(trackListener); 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) { uninstallDefaults(); uninstallListeners(); 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. * * @param yPos The y coordinate to calculate a value for. * * @return The value for the y coordinate. */ private 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. * * @param xPos The x coordinate to calculate a value for. * * @return The value for the x coordinate. */ private 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 + =
减小字号Ctrl + -
显示快捷键?