📄 basicscrollbarui.java
字号:
/** The dimensions of the maximum thumb size. */ protected Dimension maximumThumbSize; /** The dimensions of the minimum thumb size. */ protected Dimension minimumThumbSize; /** The color of the thumb. */ protected Color thumbColor; /** The outer shadow of the thumb. */ protected Color thumbDarkShadowColor; /** The top and left edge color for the thumb. */ protected Color thumbHighlightColor; /** The outer light shadow for the thumb. */ protected Color thumbLightShadowColor; /** The color that is used when the mouse press occurs in the track. */ protected Color trackHighlightColor; /** The color of the track. */ protected Color trackColor; /** The size and position of the track. */ protected Rectangle trackRect; /** The size and position of the thumb. */ protected Rectangle thumbRect; /** Indicates that the decrease highlight should be painted. */ protected static final int DECREASE_HIGHLIGHT = 1; /** Indicates that the increase highlight should be painted. */ protected static final int INCREASE_HIGHLIGHT = 2; /** Indicates that no highlight should be painted. */ protected static final int NO_HIGHLIGHT = 0; /** Indicates that the scrolling direction is positive. */ private static final int POSITIVE_SCROLL = 1; /** Indicates that the scrolling direction is negative. */ private static final int NEGATIVE_SCROLL = -1; /** The cached preferred size for the scrollbar. */ private transient Dimension preferredSize; /** The current highlight status. */ protected int trackHighlight; /** FIXME: Use this for something (presumably mouseDragged) */ protected boolean isDragging; /** The timer used to move the thumb when the mouse is held. */ protected Timer scrollTimer; /** The scrollbar this UI is acting for. */ protected JScrollBar scrollbar; /** * This method adds a component to the layout. * * @param name The name to associate with the component that is added. * @param child The Component to add. */ public void addLayoutComponent(String name, Component child) { // You should not be adding stuff to this component. // The contents are fixed. } /** * This method configures the scrollbar's colors. This can be done by * looking up the standard colors from the Look and Feel defaults. */ protected void configureScrollBarColors() { trackColor = UIManager.getColor("ScrollBar.track"); trackHighlightColor = UIManager.getColor("ScrollBar.trackHighlight"); thumbColor = UIManager.getColor("ScrollBar.thumb"); thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight"); thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow"); thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbShadow"); } /** * This method creates an ArrowButtonListener. * * @return A new ArrowButtonListener. */ protected ArrowButtonListener createArrowButtonListener() { return new ArrowButtonListener(); } /** * This method creates a new JButton with the appropriate icon for the * orientation. * * @param orientation The orientation this JButton uses. * * @return The increase JButton. */ protected JButton createIncreaseButton(int orientation) { return new BasicArrowButton(orientation); } /** * This method creates a new JButton with the appropriate icon for the * orientation. * * @param orientation The orientation this JButton uses. * * @return The decrease JButton. */ protected JButton createDecreaseButton(int orientation) { return new BasicArrowButton(orientation); } /** * This method creates a new ModelListener. * * @return A new ModelListener. */ protected ModelListener createModelListener() { return new ModelListener(); } /** * This method creates a new PropertyChangeListener. * * @return A new PropertyChangeListener. */ protected PropertyChangeListener createPropertyChangeListener() { return new PropertyChangeHandler(); } /** * This method creates a new ScrollListener. * * @return A new ScrollListener. */ protected ScrollListener createScrollListener() { return new ScrollListener(); } /** * This method creates a new TrackListener. * * @return A new TrackListener. */ protected TrackListener createTrackListener() { return new TrackListener(); } /** * This method returns a new BasicScrollBarUI. * * @param c The JComponent to create a UI for. * * @return A new BasicScrollBarUI. */ public static ComponentUI createUI(JComponent c) { return new BasicScrollBarUI(); } /** * This method returns the maximum size for this JComponent. * * @param c The JComponent to measure the maximum size for. * * @return The maximum size for the component. */ public Dimension getMaximumSize(JComponent c) { return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); } /** * This method returns the maximum thumb size. * * @return The maximum thumb size. */ protected Dimension getMaximumThumbSize() { return maximumThumbSize; } /** * This method returns the minimum size for this JComponent. * * @param c The JComponent to measure the minimum size for. * * @return The minimum size for the component. */ public Dimension getMinimumSize(JComponent c) { return getPreferredSize(c); } /** * This method returns the minimum thumb size. * * @return The minimum thumb size. */ protected Dimension getMinimumThumbSize() { return minimumThumbSize; } /** * This method calculates the preferred size since calling * getPreferredSize() returns a cached value. * This is package-private to avoid an accessor method. */ void calculatePreferredSize() { int height; int width; height = width = 0; if (scrollbar.getOrientation() == SwingConstants.HORIZONTAL) { width += incrButton.getPreferredSize().getWidth(); width += decrButton.getPreferredSize().getWidth(); width += (scrollbar.getMaximum() - scrollbar.getMinimum()); height = UIManager.getInt("ScrollBar.width"); } else { height += incrButton.getPreferredSize().getHeight(); height += decrButton.getPreferredSize().getHeight(); height += (scrollbar.getMaximum() - scrollbar.getMinimum()); width = UIManager.getInt("ScrollBar.width"); } Insets insets = scrollbar.getInsets(); height += insets.top + insets.bottom; width += insets.left + insets.right; preferredSize = new Dimension(width, height); } /** * This method returns a cached value of the preferredSize. The only * restrictions are: If the scrollbar is horizontal, the height should be * the maximum of the height of the JButtons and the minimum width of the * thumb. For vertical scrollbars, the calculation is similar (swap width * for height and vice versa). * * @param c The JComponent to measure. * * @return The preferredSize. */ public Dimension getPreferredSize(JComponent c) { calculatePreferredSize(); return preferredSize; } /** * This method returns the thumb's bounds based on the current value of the * scrollbar. This method updates the cached value and returns that. * * @return The thumb bounds. */ protected Rectangle getThumbBounds() { return thumbRect; } /** * This method calculates the bounds of the track. This method updates the * cached value and returns it. * * @return The track's bounds. */ protected Rectangle getTrackBounds() { return trackRect; } /** * This method installs any addition Components that are a part of or * related to this scrollbar. */ protected void installComponents() { if (incrButton != null) scrollbar.add(incrButton); if (decrButton != null) scrollbar.add(decrButton); } /** * This method installs the defaults for the scrollbar specified by the * Basic Look and Feel. */ protected void installDefaults() { int orientation = scrollbar.getOrientation(); switch (orientation) { case (JScrollBar.HORIZONTAL): incrButton = createIncreaseButton(EAST); decrButton = createDecreaseButton(WEST); break; default: incrButton = createIncreaseButton(SOUTH); decrButton = createDecreaseButton(NORTH); break; } LookAndFeel.installColors(scrollbar, "ScrollBar.background", "ScrollBar.foreground"); LookAndFeel.installBorder(scrollbar, "ScrollBar.border"); scrollbar.setOpaque(true); scrollbar.setLayout(this); thumbColor = UIManager.getColor("ScrollBar.thumb"); thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow"); thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight"); thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbShadow"); maximumThumbSize = UIManager.getDimension("ScrollBar.maximumThumbSize"); minimumThumbSize = UIManager.getDimension("ScrollBar.minimumThumbSize"); } /** * This method installs the keyboard actions for the scrollbar. */ protected void installKeyboardActions() { // FIXME: implement. } /** * This method installs any listeners for the scrollbar. This method also * installs listeners for things such as the JButtons and the timer. */ protected void installListeners() { scrollListener = createScrollListener(); trackListener = createTrackListener(); buttonListener = createArrowButtonListener(); modelListener = createModelListener(); propertyChangeListener = createPropertyChangeListener(); scrollbar.addMouseMotionListener(trackListener); scrollbar.addMouseListener(trackListener); incrButton.addMouseListener(buttonListener); decrButton.addMouseListener(buttonListener); scrollbar.addPropertyChangeListener(propertyChangeListener); scrollbar.getModel().addChangeListener(modelListener); scrollTimer.addActionListener(scrollListener); } /** * This method installs the UI for the component. This can include setting * up listeners, defaults, and components. This also includes initializing * any data objects. * * @param c The JComponent to install. */ public void installUI(JComponent c) { super.installUI(c); if (c instanceof JScrollBar) { scrollbar = (JScrollBar) c; trackRect = new Rectangle(); thumbRect = new Rectangle(); scrollTimer = new Timer(300, null); installDefaults(); installComponents(); configureScrollBarColors(); installListeners(); calculatePreferredSize(); } } /** * 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) { Rectangle vr = new Rectangle(); SwingUtilities.calculateInnerArea(scrollbar, vr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -