📄 clabel.java
字号:
super.setToolTipText(appToolTipText); } // determine horizontal position int x = rect.x + hIndent; if (align == SWT.CENTER) { x = (rect.width-extent.x)/2; } if (align == SWT.RIGHT) { x = rect.width-extent.x - hIndent; } // draw a background image behind the text try { if (backgroundImage != null) { // draw a background image behind the text Rectangle imageRect = backgroundImage.getBounds(); // tile image to fill space gc.setBackground(getBackground()); gc.fillRectangle(rect); int xPos = 0; while (xPos < rect.width) { int yPos = 0; while (yPos < rect.height) { gc.drawImage(backgroundImage, xPos, yPos); yPos += imageRect.height; } xPos += imageRect.width; } } else if (gradientColors != null) { // draw a gradient behind the text final Color oldBackground = gc.getBackground(); if (gradientColors.length == 1) { if (gradientColors[0] != null) gc.setBackground(gradientColors[0]); gc.fillRectangle(0, 0, rect.width, rect.height); } else { final Color oldForeground = gc.getForeground(); Color lastColor = gradientColors[0]; if (lastColor == null) lastColor = oldBackground; int pos = 0; for (int i = 0; i < gradientPercents.length; ++i) { gc.setForeground(lastColor); lastColor = gradientColors[i + 1]; if (lastColor == null) lastColor = oldBackground; gc.setBackground(lastColor); if (gradientVertical) { final int gradientHeight = (gradientPercents[i] * rect.height / 100) - pos; gc.fillGradientRectangle(0, pos, rect.width, gradientHeight, true); pos += gradientHeight; } else { final int gradientWidth = (gradientPercents[i] * rect.width / 100) - pos; gc.fillGradientRectangle(pos, 0, gradientWidth, rect.height, false); pos += gradientWidth; } } if (gradientVertical && pos < rect.height) { gc.setBackground(getBackground()); gc.fillRectangle(0, pos, rect.width, rect.height - pos); } if (!gradientVertical && pos < rect.width) { gc.setBackground(getBackground()); gc.fillRectangle(pos, 0, rect.width - pos, rect.height); } gc.setForeground(oldForeground); } gc.setBackground(oldBackground); } else { if ((getStyle() & SWT.NO_BACKGROUND) != 0) { gc.setBackground(getBackground()); gc.fillRectangle(rect); } } } catch (SWTException e) { if ((getStyle() & SWT.NO_BACKGROUND) != 0) { gc.setBackground(getBackground()); gc.fillRectangle(rect); } } // draw border int style = getStyle(); if ((style & SWT.SHADOW_IN) != 0 || (style & SWT.SHADOW_OUT) != 0) { paintBorder(gc, rect); } // draw the image if (img != null) { Rectangle imageRect = img.getBounds(); gc.drawImage(img, 0, 0, imageRect.width, imageRect.height, x, (rect.height-imageRect.height)/2, imageRect.width, imageRect.height); x += imageRect.width + GAP; } // draw the text if (t != null) { int textHeight = gc.getFontMetrics().getHeight(); gc.setForeground(getForeground()); gc.drawText(t, x, rect.y + (rect.height-textHeight)/2, true); }}/** * Set the alignment of the CLabel. * Use the values LEFT, CENTER and RIGHT to align image and text within the available space. * * @param align the alignment style of LEFT, RIGHT or CENTER * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * <li>ERROR_INVALID_ARGUMENT - if the value of align is not one of SWT.LEFT, SWT.RIGHT or SWT.CENTER</li> * </ul> */public void setAlignment(int align) { checkWidget(); if (align != SWT.LEFT && align != SWT.RIGHT && align != SWT.CENTER) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } if (this.align != align) { this.align = align; redraw(); }}public void setBackground (Color color) { super.setBackground (color); // Are these settings the same as before? if (color != null && backgroundImage == null && gradientColors == null && gradientPercents == null) { Color background = getBackground(); if (color.equals(background)) { return; } } backgroundImage = null; gradientColors = null; gradientPercents = null; redraw ();}/** * Specify a gradient of colours to be drawn in the background of the CLabel. * <p>For example, to draw a gradient that varies from dark blue to blue and then to * white and stays white for the right half of the label, use the following call * to setBackground:</p> * <pre> * clabel.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE), * display.getSystemColor(SWT.COLOR_BLUE), * display.getSystemColor(SWT.COLOR_WHITE), * display.getSystemColor(SWT.COLOR_WHITE)}, * new int[] {25, 50, 100}); * </pre> * * @param colors an array of Color that specifies the colors to appear in the gradient * in order of appearance from left to right; The value <code>null</code> * clears the background gradient; the value <code>null</code> can be used * inside the array of Color to specify the background color. * @param percents an array of integers between 0 and 100 specifying the percent of the width * of the widget at which the color should change; the size of the percents * array must be one less than the size of the colors array. * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * <li>ERROR_INVALID_ARGUMENT - if the values of colors and percents are not consistant</li> * </ul> */public void setBackground(Color[] colors, int[] percents) { setBackground(colors, percents, false);}/** * Specify a gradient of colours to be drawn in the background of the CLabel. * <p>For example, to draw a gradient that varies from dark blue to white in the vertical, * direction use the following call * to setBackground:</p> * <pre> * clabel.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE), * display.getSystemColor(SWT.COLOR_WHITE)}, * new int[] {100}, true); * </pre> * * @param colors an array of Color that specifies the colors to appear in the gradient * in order of appearance from left/top to right/bottom; The value <code>null</code> * clears the background gradient; the value <code>null</code> can be used * inside the array of Color to specify the background color. * @param percents an array of integers between 0 and 100 specifying the percent of the width/height * of the widget at which the color should change; the size of the percents * array must be one less than the size of the colors array. * @param vertical indicate the direction of the gradient. True is vertical and false is horizontal. * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * <li>ERROR_INVALID_ARGUMENT - if the values of colors and percents are not consistant</li> * </ul> * * @since 3.0 */public void setBackground(Color[] colors, int[] percents, boolean vertical) { checkWidget(); if (colors != null) { if (percents == null || percents.length != colors.length - 1) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } if (getDisplay().getDepth() < 15) { // Don't use gradients on low color displays colors = new Color[] {colors[colors.length - 1]}; percents = new int[] { }; } for (int i = 0; i < percents.length; i++) { if (percents[i] < 0 || percents[i] > 100) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } if (i > 0 && percents[i] < percents[i-1]) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } } } // Are these settings the same as before? final Color background = getBackground(); if (backgroundImage == null) { if ((gradientColors != null) && (colors != null) && (gradientColors.length == colors.length)) { boolean same = false; for (int i = 0; i < gradientColors.length; i++) { same = (gradientColors[i] == colors[i]) || ((gradientColors[i] == null) && (colors[i] == background)) || ((gradientColors[i] == background) && (colors[i] == null)); if (!same) break; } if (same) { for (int i = 0; i < gradientPercents.length; i++) { same = gradientPercents[i] == percents[i]; if (!same) break; } } if (same && this.gradientVertical == vertical) return; } } else { backgroundImage = null; } // Store the new settings if (colors == null) { gradientColors = null; gradientPercents = null; gradientVertical = false; } else { gradientColors = new Color[colors.length]; for (int i = 0; i < colors.length; ++i) gradientColors[i] = (colors[i] != null) ? colors[i] : background; gradientPercents = new int[percents.length]; for (int i = 0; i < percents.length; ++i) gradientPercents[i] = percents[i]; gradientVertical = vertical; } // Refresh with the new settings redraw();}/** * Set the image to be drawn in the background of the label. * * @param image the image to be drawn in the background * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public void setBackground(Image image) { checkWidget(); if (image == backgroundImage) return; if (image != null) { gradientColors = null; gradientPercents = null; } backgroundImage = image; redraw(); }public void setFont(Font font) { super.setFont(font); redraw();}/** * Set the label's Image. * The value <code>null</code> clears it. * * @param image the image to be displayed in the label or null * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public void setImage(Image image) { checkWidget(); if (image != this.image) { this.image = image; redraw(); }}/** * Set the label's text. * The value <code>null</code> clears it. * * @param text the text to be displayed in the label or null * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public void setText(String text) { checkWidget(); if (text == null) text = ""; //$NON-NLS-1$ if (! text.equals(this.text)) { this.text = text; redraw(); }}/** * Shorten the given text <code>t</code> so that its length doesn't exceed * the given width. The default implementation replaces characters in the * center of the original string with an ellipsis ("..."). * Override if you need a different strategy. */protected String shortenText(GC gc, String t, int width) { if (t == null) return null; int w = gc.textExtent(ELLIPSIS).x; int l = t.length(); int pivot = l/2; int s = pivot; int e = pivot+1; while (s >= 0 && e < l) { String s1 = t.substring(0, s); String s2 = t.substring(e, l); int l1 = gc.textExtent(s1).x; int l2 = gc.textExtent(s2).x; if (l1+w+l2 < width) { t = s1 + ELLIPSIS + s2; break; } s--; e++; } return t;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -