basicsliderui.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 2,219 行 · 第 1/5 页

JAVA
2,219
字号
      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 = thumbHeight;    if (slider.getPaintTicks() && slider.getMajorTickSpacing() > 0        || slider.getMinorTickSpacing() > 0)      width += tickHeight;    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()  {    return getPreferredHorizontalSize();  }  /**   * 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()  {    return getPreferredVerticalSize();  }  /**   * 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 getPreferredHorizontalSize();    else      return getPreferredVerticalSize();  }  /**   * This method returns the maximum 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 a maximum size for.   *   * @return The dimensions of the maximum size.   */  public Dimension getMaximumSize(JComponent c)  {    if (slider.getOrientation() == JSlider.HORIZONTAL)      return getPreferredHorizontalSize();    else      return getPreferredVerticalSize();  }  /**   * 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()  {    if (slider.getOrientation() == JSlider.HORIZONTAL)      {	if (thumbWidth > contentRect.width)	  thumbRect.width = contentRect.width / 4;	else	  thumbRect.width = thumbWidth;	if (thumbHeight > contentRect.height)	  thumbRect.height = contentRect.height;	else	  thumbRect.height = thumbHeight;      }    else      {	// The thumb gets flipped when inverted, so thumbWidth 	// actually is the height and vice versa.	if (thumbWidth > contentRect.height)	  thumbRect.height = contentRect.height / 4;	else	  thumbRect.height = thumbWidth;	if (thumbHeight > contentRect.width)	  thumbRect.width = contentRect.width;	else	  thumbRect.width = thumbHeight;      }  }  /**   * 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 = contentRect.y;      }    else      {	thumbRect.x = contentRect.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;    else      trackBuffer = thumbRect.height;  }  /**   * This method returns the size of the thumbRect.   *   * @return The dimensions of the thumb.   */  protected Dimension getThumbSize()  {    // This is really just the bounds box for the thumb.    // The thumb will actually be pointed (like a rectangle + triangle at bottom)    return thumbRect.getSize();  }  /**   * 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;	trackRect.y = contentRect.y;	trackRect.width = contentRect.width - 2 * trackBuffer;	trackRect.height = thumbRect.height;      }    else      {	trackRect.x = contentRect.x;	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 tickHeight;  }  /**   * 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();)      {	Object comp = list.nextElement();	if (! (comp instanceof Component))	  continue;	label = (Component) comp;	pref = label.getPreferredSize();	if (pref != null && pref.height > tallest)	  tallest = pref.height;      }    return tallest;  }  /**   * This method returns the width of the label whose key has the highest   * value.   *   * @return The width of the high value label or 0 if no label table exists.   */  protected int getWidthOfHighValueLabel()  {    Component highValueLabel = getHighestValueLabel();    if (highValueLabel != null)      return highValueLabel.getWidth();    else      return 0;  }  /**   * This method returns the width of the label whose key has the lowest   * value.   *   * @return The width of the low value label or 0 if no label table exists.   */  protected int getWidthOfLowValueLabel()  {    Component lowValueLabel = getLowestValueLabel();    if (lowValueLabel != null)      return lowValueLabel.getWidth();    else      return 0;  }  /**   * This method returns the height of the label whose key has the highest   * value.   *   * @return The height of the high value label or 0 if no label table exists.   */  protected int getHeightOfHighValueLabel()  {    Component highValueLabel = getHighestValueLabel();    if (highValueLabel != null)      return highValueLabel.getHeight();    else      return 0;  }  /**   * This method returns the height of the label whose key has the lowest   * value.   *   * @return The height of the low value label or 0 if no label table exists.   */  protected int getHeightOfLowValueLabel()  {    Component lowValueLabel = getLowestValueLabel();    if (lowValueLabel != null)      return lowValueLabel.getHeight();    else      return 0;  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?