📄 basicsliderui.java
字号:
public Dimension getPreferredHorizontalSize() { Insets insets = slider.getInsets(); // The width should cover all the labels (which are usually the // deciding factor of the width) int width = getWidthOfWidestLabel() * (slider.getLabelTable() == null ? 0 : slider.getLabelTable() .size()); // If there are not enough labels. // This number is pretty much arbitrary, but it looks nice. if (width < 200) width = 200; // We can only draw inside of the focusRectangle, so we have to // pad it with insets. width += insets.left + insets.right + focusInsets.left + focusInsets.right; // Height is determined by the thumb, the ticks and the labels. int height = getThumbSize().height; if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0) height += getTickLength(); if (slider.getPaintLabels()) height += getHeightOfTallestLabel(); height += insets.top + insets.bottom + focusInsets.top + focusInsets.bottom; return new Dimension(width, height); } /** * This method returns the preferred size when the slider is vertically * oriented. * * @return The dimensions of the preferred vertical size. */ public Dimension getPreferredVerticalSize() { Insets insets = slider.getInsets(); int height = getHeightOfTallestLabel() * (slider.getLabelTable() == null ? 0 : slider.getLabelTable() .size()); if (height < 200) height = 200; height += insets.top + insets.bottom + focusInsets.top + focusInsets.bottom; int width = getThumbSize().width; if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0) width += getTickLength(); if (slider.getPaintLabels()) width += getWidthOfWidestLabel(); width += insets.left + insets.right + focusInsets.left + focusInsets.right; return new Dimension(width, height); } /** * This method returns the minimum size when the slider is horizontally * oriented. * * @return The dimensions of the minimum horizontal size. */ public Dimension getMinimumHorizontalSize() { Insets insets = slider.getInsets(); // Height is determined by the thumb, the ticks and the labels. int height = getThumbSize().height; if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0) height += getTickLength(); if (slider.getPaintLabels()) height += getHeightOfTallestLabel(); height += insets.top + insets.bottom + focusInsets.top + focusInsets.bottom; return new Dimension(36, height); } /** * This method returns the minimum size of the slider when it is vertically * oriented. * * @return The dimensions of the minimum vertical size. */ public Dimension getMinimumVerticalSize() { Insets insets = slider.getInsets(); int width = getThumbSize().width; if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0) width += getTickLength(); if (slider.getPaintLabels()) width += getWidthOfWidestLabel(); width += insets.left + insets.right + focusInsets.left + focusInsets.right; return new Dimension(width, 36); } /** * This method returns the preferred size of the component. If it returns * null, then it is up to the Layout Manager to give the {@link JComponent} * a size. * * @param c The {@link JComponent} to find the preferred size for. * * @return The dimensions of the preferred size. */ public Dimension getPreferredSize(JComponent c) { if (slider.getOrientation() == JSlider.HORIZONTAL) return getPreferredHorizontalSize(); else return getPreferredVerticalSize(); } /** * This method returns the minimum size for this {@link JSlider} for this * look and feel. If it returns null, then it is up to the Layout Manager * to give the {@link JComponent} a size. * * @param c The {@link JComponent} to find the minimum size for. * * @return The dimensions of the minimum size. */ public Dimension getMinimumSize(JComponent c) { if (slider.getOrientation() == JSlider.HORIZONTAL) return getMinimumHorizontalSize(); else return getMinimumVerticalSize(); } /** * This method returns the maximum size for this {@link JSlider} for this * look and feel. * * @param c The {@link JComponent} to find a maximum size for. * * @return The dimensions of the maximum size. */ public Dimension getMaximumSize(JComponent c) { Insets insets = slider.getInsets(); if (slider.getOrientation() == JSlider.HORIZONTAL) { // Height is determined by the thumb, the ticks and the labels. int height = getThumbSize().height; if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0) height += getTickLength(); if (slider.getPaintLabels()) height += getHeightOfTallestLabel(); height += insets.top + insets.bottom + focusInsets.top + focusInsets.bottom; return new Dimension(32767, height); } else { int width = getThumbSize().width; if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0) width += getTickLength(); if (slider.getPaintLabels()) width += getWidthOfWidestLabel(); width += insets.left + insets.right + focusInsets.left + focusInsets.right; return new Dimension(width, 32767); } } /** * This method calculates all the sizes of the rectangles by delegating to * the helper methods calculateXXXRect. */ protected void calculateGeometry() { calculateFocusRect(); calculateContentRect(); calculateThumbSize(); calculateTrackBuffer(); calculateTrackRect(); calculateTickRect(); calculateLabelRect(); calculateThumbLocation(); } /** * This method calculates the size and position of the focusRect. This * method does not need to be called if the orientation changes. */ protected void calculateFocusRect() { insetCache = slider.getInsets(); focusRect = SwingUtilities.calculateInnerArea(slider, focusRect); if (focusRect.width < 0) focusRect.width = 0; if (focusRect.height < 0) focusRect.height = 0; } /** * This method calculates the size but not the position of the thumbRect. It * must take into account the orientation of the slider. */ protected void calculateThumbSize() { Dimension d = getThumbSize(); thumbRect.width = d.width; thumbRect.height = d.height; if (slider.getOrientation() == JSlider.HORIZONTAL) thumbRect.y = trackRect.y; else thumbRect.x = trackRect.x; } /** * This method calculates the size and position of the contentRect. This * method does not need to be called if the orientation changes. */ protected void calculateContentRect() { contentRect.x = focusRect.x + focusInsets.left; contentRect.y = focusRect.y + focusInsets.top; contentRect.width = focusRect.width - focusInsets.left - focusInsets.right; contentRect.height = focusRect.height - focusInsets.top - focusInsets.bottom; if (contentRect.width < 0) contentRect.width = 0; if (contentRect.height < 0) contentRect.height = 0; } /** * Calculates the position of the thumbRect based on the current value of * the slider. It must take into account the orientation of the slider. */ protected void calculateThumbLocation() { int value = slider.getValue(); if (slider.getOrientation() == JSlider.HORIZONTAL) { thumbRect.x = xPositionForValue(value) - thumbRect.width / 2; thumbRect.y = trackRect.y; } else { thumbRect.x = trackRect.x; thumbRect.y = yPositionForValue(value) - thumbRect.height / 2; } } /** * Calculates the gap size between the left edge of the contentRect and the * left edge of the trackRect. */ protected void calculateTrackBuffer() { if (slider.getOrientation() == JSlider.HORIZONTAL) trackBuffer = thumbRect.width / 2; else trackBuffer = thumbRect.height / 2; } /** * This method returns the size of the thumbRect. * * @return The dimensions of the thumb. */ protected Dimension getThumbSize() { // TODO: shouldn't create new objects every time if (slider.getOrientation() == JSlider.HORIZONTAL) return new Dimension(11, 20); else return new Dimension(20, 11); } /** * Calculates the size and position of the trackRect. It must take into * account the orientation of the slider. */ protected void calculateTrackRect() { if (slider.getOrientation() == JSlider.HORIZONTAL) { trackRect.x = contentRect.x + trackBuffer; int h = getThumbSize().height; if (slider.getPaintTicks() && (slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0)) h += getTickLength(); trackRect.y = contentRect.y + (contentRect.height - h) / 2 - 1; trackRect.width = contentRect.width - 2 * trackBuffer; trackRect.height = thumbRect.height; } else { int w = getThumbSize().width; if (slider.getPaintTicks() && (slider.getMajorTickSpacing() > 0 || slider.getMinorTickSpacing() > 0)) w += getTickLength(); trackRect.x = contentRect.x + (contentRect.width - w) / 2 - 1; trackRect.y = contentRect.y + trackBuffer; trackRect.width = thumbRect.width; trackRect.height = contentRect.height - 2 * trackBuffer; } } /** * This method returns the height of the tick area box if the slider is * horizontal and the width of the tick area box is the slider is vertical. * It not necessarily how long the ticks will be. If a gap between the edge * of tick box and the actual tick is desired, then that will need to be * handled in the tick painting methods. * * @return The height (or width if the slider is vertical) of the tick * rectangle. */ protected int getTickLength() { return 8; } /** * This method calculates the size and position of the tickRect. It must * take into account the orientation of the slider. */ protected void calculateTickRect() { if (slider.getOrientation() == JSlider.HORIZONTAL) { tickRect.x = trackRect.x; tickRect.y = trackRect.y + trackRect.height; tickRect.width = trackRect.width; tickRect.height = getTickLength(); if (tickRect.y + tickRect.height > contentRect.y + contentRect.height) tickRect.height = contentRect.y + contentRect.height - tickRect.y; } else { tickRect.x = trackRect.x + trackRect.width; tickRect.y = trackRect.y; tickRect.width = getTickLength(); tickRect.height = trackRect.height; if (tickRect.x + tickRect.width > contentRect.x + contentRect.width) tickRect.width = contentRect.x + contentRect.width - tickRect.x; } } /** * This method calculates the size and position of the labelRect. It must * take into account the orientation of the slider. */ protected void calculateLabelRect() { if (slider.getOrientation() == JSlider.HORIZONTAL) { labelRect.x = contentRect.x; labelRect.y = tickRect.y + tickRect.height; labelRect.width = contentRect.width; labelRect.height = contentRect.height - labelRect.y; } else { labelRect.x = tickRect.x + tickRect.width; labelRect.y = contentRect.y; labelRect.width = contentRect.width - labelRect.x; labelRect.height = contentRect.height; } } /** * This method returns the width of the widest label in the slider's label * table. * * @return The width of the widest label or 0 if no label table exists. */ protected int getWidthOfWidestLabel() { int widest = 0; Component label; if (slider.getLabelTable() == null) return 0; Dimension pref; for (Enumeration list = slider.getLabelTable().elements(); list.hasMoreElements();) { Object comp = list.nextElement(); if (! (comp instanceof Component)) continue; label = (Component) comp; pref = label.getPreferredSize(); if (pref != null && pref.width > widest) widest = pref.width; } return widest; } /** * This method returns the height of the tallest label in the slider's label * table. * * @return The height of the tallest label or 0 if no label table exists. */ protected int getHeightOfTallestLabel() { int tallest = 0; Component label; if (slider.getLabelTable() == null) return 0; Dimension pref; for (Enumeration list = slider.getLabelTable().elements(); list.hasMoreElements();)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -