提交 7b8e76d6 编写于 作者: M malenkov

4129681: Cannot get a title border to display its label as disabled

Reviewed-by: alexp, rupashka
上级 19f7df4c
/* /*
* Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -24,22 +24,20 @@ ...@@ -24,22 +24,20 @@
*/ */
package javax.swing.border; package javax.swing.border;
import sun.swing.SwingUtilities2;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Color; import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Component; import java.awt.Component;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.geom.Path2D;
import java.beans.ConstructorProperties; import java.beans.ConstructorProperties;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicHTML;
/** /**
* A class which implements an arbitrary border * A class which implements an arbitrary border
...@@ -78,7 +76,7 @@ public class TitledBorder extends AbstractBorder ...@@ -78,7 +76,7 @@ public class TitledBorder extends AbstractBorder
protected Font titleFont; protected Font titleFont;
protected Color titleColor; protected Color titleColor;
private Point textLoc = new Point(); private final JLabel label;
/** /**
* Use the default vertical orientation for the title text. * Use the default vertical orientation for the title text.
...@@ -219,6 +217,10 @@ public class TitledBorder extends AbstractBorder ...@@ -219,6 +217,10 @@ public class TitledBorder extends AbstractBorder
setTitleJustification(titleJustification); setTitleJustification(titleJustification);
setTitlePosition(titlePosition); setTitlePosition(titlePosition);
this.label = new JLabel();
this.label.setOpaque(false);
this.label.putClientProperty(BasicHTML.propertyKey, null);
} }
/** /**
...@@ -232,178 +234,111 @@ public class TitledBorder extends AbstractBorder ...@@ -232,178 +234,111 @@ public class TitledBorder extends AbstractBorder
* @param height the height of the painted border * @param height the height of the painted border
*/ */
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Border border = getBorderUI();
Border border = getBorder(); String title = getTitle();
if ((title != null) && !title.isEmpty()) {
if (getTitle() == null || getTitle().equals("")) { int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
if (border != null) { JLabel label = getLabel(c);
border.paintBorder(c, g, x, y, width, height); Dimension size = label.getPreferredSize();
} Insets insets = (border != null)
return; ? border.getBorderInsets(c)
} : new Insets(0, 0, 0, 0);
Rectangle grooveRect = new Rectangle(x + EDGE_SPACING, y + EDGE_SPACING, int borderX = x + edge;
width - (EDGE_SPACING * 2), int borderY = y + edge;
height - (EDGE_SPACING * 2)); int borderW = width - edge - edge;
Font font = g.getFont(); int borderH = height - edge - edge;
Color color = g.getColor();
int labelY = y;
g.setFont(getFont(c)); int labelH = size.height;
int position = getPosition();
JComponent jc = (c instanceof JComponent) ? (JComponent)c : null; switch (position) {
FontMetrics fm = SwingUtilities2.getFontMetrics(jc, g);
int fontHeight = fm.getHeight();
int descent = fm.getDescent();
int ascent = fm.getAscent();
int diff;
int stringWidth = SwingUtilities2.stringWidth(jc, fm,
getTitle());
Insets insets;
if (border != null) {
insets = border.getBorderInsets(c);
} else {
insets = new Insets(0, 0, 0, 0);
}
int titlePos = getTitlePosition();
switch (titlePos) {
case ABOVE_TOP: case ABOVE_TOP:
diff = ascent + descent + (Math.max(EDGE_SPACING, insets.left = 0;
TEXT_SPACING*2) - EDGE_SPACING); insets.right = 0;
grooveRect.y += diff; borderY += labelH - edge;
grooveRect.height -= diff; borderH -= labelH - edge;
textLoc.y = grooveRect.y - (descent + TEXT_SPACING);
break; break;
case TOP: case TOP:
case DEFAULT_POSITION: insets.top = edge + insets.top/2 - labelH/2;
diff = Math.max(0, ((ascent/2) + TEXT_SPACING) - EDGE_SPACING); if (insets.top < edge) {
grooveRect.y += diff; borderY -= insets.top;
grooveRect.height -= diff; borderH += insets.top;
textLoc.y = (grooveRect.y - descent) + }
(insets.top + ascent + descent)/2; else {
labelY += insets.top;
}
break; break;
case BELOW_TOP: case BELOW_TOP:
textLoc.y = grooveRect.y + insets.top + ascent + TEXT_SPACING; labelY += insets.top + edge;
break; break;
case ABOVE_BOTTOM: case ABOVE_BOTTOM:
textLoc.y = (grooveRect.y + grooveRect.height) - labelY += height - labelH - insets.bottom - edge;
(insets.bottom + descent + TEXT_SPACING);
break; break;
case BOTTOM: case BOTTOM:
grooveRect.height -= fontHeight/2; labelY += height - labelH;
textLoc.y = ((grooveRect.y + grooveRect.height) - descent) + insets.bottom = edge + (insets.bottom - labelH) / 2;
((ascent + descent) - insets.bottom)/2; if (insets.bottom < edge) {
borderH += insets.bottom;
}
else {
labelY -= insets.bottom;
}
break; break;
case BELOW_BOTTOM: case BELOW_BOTTOM:
grooveRect.height -= fontHeight; insets.left = 0;
textLoc.y = grooveRect.y + grooveRect.height + ascent + insets.right = 0;
TEXT_SPACING; labelY += height - labelH;
borderH -= labelH - edge;
break; break;
} }
insets.left += edge + TEXT_INSET_H;
insets.right += edge + TEXT_INSET_H;
int justification = getTitleJustification(); int labelX = x;
if(isLeftToRight(c)) { int labelW = width - insets.left - insets.right;
if(justification==LEADING || if (labelW > size.width) {
justification==DEFAULT_JUSTIFICATION) { labelW = size.width;
justification = LEFT;
}
else if(justification==TRAILING) {
justification = RIGHT;
} }
} switch (getJustification(c)) {
else {
if(justification==LEADING ||
justification==DEFAULT_JUSTIFICATION) {
justification = RIGHT;
}
else if(justification==TRAILING) {
justification = LEFT;
}
}
switch (justification) {
case LEFT: case LEFT:
textLoc.x = grooveRect.x + TEXT_INSET_H + insets.left; labelX += insets.left;
break; break;
case RIGHT: case RIGHT:
textLoc.x = (grooveRect.x + grooveRect.width) - labelX += width - insets.right - labelW;
(stringWidth + TEXT_INSET_H + insets.right);
break; break;
case CENTER: case CENTER:
textLoc.x = grooveRect.x + labelX += (width - labelW) / 2;
((grooveRect.width - stringWidth) / 2);
break; break;
} }
// If title is positioned in middle of border AND its fontsize
// is greater than the border's thickness, we'll need to paint
// the border in sections to leave space for the component's background
// to show through the title.
//
if (border != null) { if (border != null) {
if (((titlePos == TOP || titlePos == DEFAULT_POSITION) && if ((position != TOP) && (position != BOTTOM)) {
(grooveRect.y > textLoc.y - ascent)) || border.paintBorder(c, g, borderX, borderY, borderW, borderH);
(titlePos == BOTTOM &&
(grooveRect.y + grooveRect.height < textLoc.y + descent))) {
Rectangle clipRect = new Rectangle();
// save original clip
Rectangle saveClip = g.getClipBounds();
// paint strip left of text
clipRect.setBounds(saveClip);
if (computeIntersection(clipRect, x, y, textLoc.x-1-x, height)) {
g.setClip(clipRect);
border.paintBorder(c, g, grooveRect.x, grooveRect.y,
grooveRect.width, grooveRect.height);
}
// paint strip right of text
clipRect.setBounds(saveClip);
if (computeIntersection(clipRect, textLoc.x+stringWidth+1, y,
x+width-(textLoc.x+stringWidth+1), height)) {
g.setClip(clipRect);
border.paintBorder(c, g, grooveRect.x, grooveRect.y,
grooveRect.width, grooveRect.height);
}
if (titlePos == TOP || titlePos == DEFAULT_POSITION) {
// paint strip below text
clipRect.setBounds(saveClip);
if (computeIntersection(clipRect, textLoc.x-1, textLoc.y+descent,
stringWidth+2, y+height-textLoc.y-descent)) {
g.setClip(clipRect);
border.paintBorder(c, g, grooveRect.x, grooveRect.y,
grooveRect.width, grooveRect.height);
}
} else { // titlePos == BOTTOM
// paint strip above text
clipRect.setBounds(saveClip);
if (computeIntersection(clipRect, textLoc.x-1, y,
stringWidth+2, textLoc.y - ascent - y)) {
g.setClip(clipRect);
border.paintBorder(c, g, grooveRect.x, grooveRect.y,
grooveRect.width, grooveRect.height);
}
}
// restore clip
g.setClip(saveClip);
} else {
border.paintBorder(c, g, grooveRect.x, grooveRect.y,
grooveRect.width, grooveRect.height);
} }
else {
Graphics g2 = g.create();
if (g2 instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D) g2;
Path2D path = new Path2D.Float();
path.append(new Rectangle(borderX, borderY, borderW, labelY - borderY), false);
path.append(new Rectangle(borderX, labelY, labelX - borderX - TEXT_SPACING, labelH), false);
path.append(new Rectangle(labelX + labelW + TEXT_SPACING, labelY, borderX - labelX + borderW - labelW - TEXT_SPACING, labelH), false);
path.append(new Rectangle(borderX, labelY + labelH, borderW, borderY - labelY + borderH - labelH), false);
g2d.clip(path);
}
border.paintBorder(c, g2, borderX, borderY, borderW, borderH);
g2.dispose();
}
}
g.translate(labelX, labelY);
label.setSize(labelW, labelH);
label.paint(g);
g.translate(-labelX, -labelY);
}
else if (border != null) {
border.paintBorder(c, g, x, y, width, height);
} }
g.setColor(getTitleColor());
SwingUtilities2.drawString(jc, g, getTitle(), textLoc.x, textLoc.y);
g.setFont(font);
g.setColor(color);
} }
/** /**
...@@ -412,70 +347,55 @@ public class TitledBorder extends AbstractBorder ...@@ -412,70 +347,55 @@ public class TitledBorder extends AbstractBorder
* @param insets the object to be reinitialized * @param insets the object to be reinitialized
*/ */
public Insets getBorderInsets(Component c, Insets insets) { public Insets getBorderInsets(Component c, Insets insets) {
FontMetrics fm; Border border = getBorderUI();
int descent = 0; if (border == null) {
int ascent = 16; insets.set(0, 0, 0, 0);
int height = 16;
Border border = getBorder();
if (border != null) {
if (border instanceof AbstractBorder) {
((AbstractBorder)border).getBorderInsets(c, insets);
} else {
// Can't reuse border insets because the Border interface
// can't be enhanced.
Insets i = border.getBorderInsets(c);
insets.top = i.top;
insets.right = i.right;
insets.bottom = i.bottom;
insets.left = i.left;
} }
} else { else if (border instanceof AbstractBorder) {
insets.left = insets.top = insets.right = insets.bottom = 0; AbstractBorder ab = (AbstractBorder) border;
} insets = ab.getBorderInsets(c, insets);
insets.left += EDGE_SPACING + TEXT_SPACING;
insets.right += EDGE_SPACING + TEXT_SPACING;
insets.top += EDGE_SPACING + TEXT_SPACING;
insets.bottom += EDGE_SPACING + TEXT_SPACING;
if(c == null || getTitle() == null || getTitle().equals("")) {
return insets;
} }
else {
Font font = getFont(c); Insets i = border.getBorderInsets(c);
insets.set(i.top, i.left, i.bottom, i.right);
fm = c.getFontMetrics(font);
if(fm != null) {
descent = fm.getDescent();
ascent = fm.getAscent();
height = fm.getHeight();
} }
String title = getTitle();
if ((title != null) && !title.isEmpty()) {
int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
JLabel label = getLabel(c);
Dimension size = label.getPreferredSize();
switch (getTitlePosition()) { switch (getPosition()) {
case ABOVE_TOP: case ABOVE_TOP:
insets.top += ascent + descent insets.top += size.height - edge;
+ (Math.max(EDGE_SPACING, TEXT_SPACING*2)
- EDGE_SPACING);
break; break;
case TOP: case TOP: {
case DEFAULT_POSITION: if (insets.top < size.height) {
insets.top += ascent + descent; insets.top = size.height - edge;
}
break; break;
}
case BELOW_TOP: case BELOW_TOP:
insets.top += ascent + descent + TEXT_SPACING; insets.top += size.height;
break; break;
case ABOVE_BOTTOM: case ABOVE_BOTTOM:
insets.bottom += ascent + descent + TEXT_SPACING; insets.bottom += size.height;
break; break;
case BOTTOM: case BOTTOM: {
insets.bottom += ascent + descent; if (insets.bottom < size.height) {
insets.bottom = size.height - edge;
}
break; break;
}
case BELOW_BOTTOM: case BELOW_BOTTOM:
insets.bottom += height; insets.bottom += size.height - edge;
break; break;
} }
insets.top += edge + TEXT_SPACING;
insets.left += edge + TEXT_SPACING;
insets.right += edge + TEXT_SPACING;
insets.bottom += edge + TEXT_SPACING;
}
return insets; return insets;
} }
...@@ -493,40 +413,13 @@ public class TitledBorder extends AbstractBorder ...@@ -493,40 +413,13 @@ public class TitledBorder extends AbstractBorder
* Returns the border of the titled border. * Returns the border of the titled border.
*/ */
public Border getBorder() { public Border getBorder() {
Border b = border; return border;
if (b == null)
b = UIManager.getBorder("TitledBorder.border");
return b;
} }
/** /**
* Returns the title-position of the titled border. * Returns the title-position of the titled border.
*/ */
public int getTitlePosition() { public int getTitlePosition() {
if (titlePosition == DEFAULT_POSITION) {
Object value = UIManager.get("TitledBorder.position");
if (value instanceof String) {
String s = (String)value;
if ("ABOVE_TOP".equalsIgnoreCase(s)) {
return ABOVE_TOP;
} else if ("TOP".equalsIgnoreCase(s)) {
return TOP;
} else if ("BELOW_TOP".equalsIgnoreCase(s)) {
return BELOW_TOP;
} else if ("ABOVE_BOTTOM".equalsIgnoreCase(s)) {
return ABOVE_BOTTOM;
} else if ("BOTTOM".equalsIgnoreCase(s)) {
return BOTTOM;
} else if ("BELOW_BOTTOM".equalsIgnoreCase(s)) {
return BELOW_BOTTOM;
}
} else if (value instanceof Integer) {
int i = (Integer)value;
if (i >= 0 && i <= 6) {
return i;
}
}
}
return titlePosition; return titlePosition;
} }
...@@ -539,20 +432,14 @@ public class TitledBorder extends AbstractBorder ...@@ -539,20 +432,14 @@ public class TitledBorder extends AbstractBorder
* Returns the title-font of the titled border. * Returns the title-font of the titled border.
*/ */
public Font getTitleFont() { public Font getTitleFont() {
Font f = titleFont; return titleFont;
if (f == null)
f = UIManager.getFont("TitledBorder.font");
return f;
} }
/** /**
* Returns the title-color of the titled border. * Returns the title-color of the titled border.
*/ */
public Color getTitleColor() { public Color getTitleColor() {
Color c = titleColor; return titleColor;
if (c == null)
c = UIManager.getColor("TitledBorder.titleColor");
return c;
} }
...@@ -636,22 +523,18 @@ public class TitledBorder extends AbstractBorder ...@@ -636,22 +523,18 @@ public class TitledBorder extends AbstractBorder
Insets insets = getBorderInsets(c); Insets insets = getBorderInsets(c);
Dimension minSize = new Dimension(insets.right+insets.left, Dimension minSize = new Dimension(insets.right+insets.left,
insets.top+insets.bottom); insets.top+insets.bottom);
Font font = getFont(c); String title = getTitle();
FontMetrics fm = c.getFontMetrics(font); if ((title != null) && !title.isEmpty()) {
JComponent jc = (c instanceof JComponent) ? (JComponent)c : null; JLabel label = getLabel(c);
switch (getTitlePosition()) { Dimension size = label.getPreferredSize();
case ABOVE_TOP:
case BELOW_BOTTOM: int position = getPosition();
minSize.width = Math.max(SwingUtilities2.stringWidth(jc, fm, if ((position != ABOVE_TOP) && (position != BELOW_BOTTOM)) {
getTitle()), minSize.width); minSize.width += size.width;
break; }
case BELOW_TOP: else if (minSize.width < size.width) {
case ABOVE_BOTTOM: minSize.width += size.width;
case TOP: }
case BOTTOM:
case DEFAULT_POSITION:
default:
minSize.width += SwingUtilities2.stringWidth(jc, fm, getTitle());
} }
return minSize; return minSize;
} }
...@@ -674,48 +557,36 @@ public class TitledBorder extends AbstractBorder ...@@ -674,48 +557,36 @@ public class TitledBorder extends AbstractBorder
if (height < 0) { if (height < 0) {
throw new IllegalArgumentException("Height must be >= 0"); throw new IllegalArgumentException("Height must be >= 0");
} }
Border border = getBorderUI();
String title = getTitle(); String title = getTitle();
if (title != null && !"".equals(title)) { if ((title != null) && !title.isEmpty()) {
Font font = getFont(c); int edge = (border instanceof TitledBorder) ? 0 : EDGE_SPACING;
Border border2 = getBorder(); JLabel label = getLabel(c);
Insets borderInsets; Dimension size = label.getPreferredSize();
if (border2 != null) { Insets insets = (border != null)
borderInsets = border2.getBorderInsets(c); ? border.getBorderInsets(c)
} : new Insets(0, 0, 0, 0);
else {
borderInsets = new Insets(0, 0, 0, 0); int baseline = label.getBaseline(size.width, size.height);
} switch (getPosition()) {
FontMetrics fm = c.getFontMetrics(font);
int fontHeight = fm.getHeight();
int descent = fm.getDescent();
int ascent = fm.getAscent();
int y = EDGE_SPACING;
int h = height - EDGE_SPACING * 2;
int diff;
switch (getTitlePosition()) {
case ABOVE_TOP: case ABOVE_TOP:
diff = ascent + descent + (Math.max(EDGE_SPACING, return baseline;
TEXT_SPACING * 2) -
EDGE_SPACING);
return y + diff - (descent + TEXT_SPACING);
case TOP: case TOP:
case DEFAULT_POSITION: insets.top = edge + (insets.top - size.height) / 2;
diff = Math.max(0, ((ascent/2) + TEXT_SPACING) - return (insets.top < edge)
EDGE_SPACING); ? baseline
return (y + diff - descent) + : baseline + insets.top;
(borderInsets.top + ascent + descent)/2;
case BELOW_TOP: case BELOW_TOP:
return y + borderInsets.top + ascent + TEXT_SPACING; return baseline + insets.top + edge;
case ABOVE_BOTTOM: case ABOVE_BOTTOM:
return (y + h) - (borderInsets.bottom + descent + return baseline + height - size.height - insets.bottom - edge;
TEXT_SPACING);
case BOTTOM: case BOTTOM:
h -= fontHeight / 2; insets.bottom = edge + (insets.bottom - size.height) / 2;
return ((y + h) - descent) + return (insets.bottom < edge)
((ascent + descent) - borderInsets.bottom)/2; ? baseline + height - size.height
: baseline + height - size.height + insets.bottom;
case BELOW_BOTTOM: case BELOW_BOTTOM:
h -= fontHeight; return baseline + height - size.height;
return y + h + ascent + TEXT_SPACING;
} }
} }
return -1; return -1;
...@@ -732,10 +603,9 @@ public class TitledBorder extends AbstractBorder ...@@ -732,10 +603,9 @@ public class TitledBorder extends AbstractBorder
public Component.BaselineResizeBehavior getBaselineResizeBehavior( public Component.BaselineResizeBehavior getBaselineResizeBehavior(
Component c) { Component c) {
super.getBaselineResizeBehavior(c); super.getBaselineResizeBehavior(c);
switch(getTitlePosition()) { switch (getPosition()) {
case TitledBorder.ABOVE_TOP: case TitledBorder.ABOVE_TOP:
case TitledBorder.TOP: case TitledBorder.TOP:
case TitledBorder.DEFAULT_POSITION:
case TitledBorder.BELOW_TOP: case TitledBorder.BELOW_TOP:
return Component.BaselineResizeBehavior.CONSTANT_ASCENT; return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
case TitledBorder.ABOVE_BOTTOM: case TitledBorder.ABOVE_BOTTOM:
...@@ -746,30 +616,98 @@ public class TitledBorder extends AbstractBorder ...@@ -746,30 +616,98 @@ public class TitledBorder extends AbstractBorder
return Component.BaselineResizeBehavior.OTHER; return Component.BaselineResizeBehavior.OTHER;
} }
private Border getBorderUI() {
Border border = getBorder();
return border != null
? border
: UIManager.getBorder("TitledBorder.border");
}
private int getPosition() {
int position = getTitlePosition();
if (position != DEFAULT_POSITION) {
return position;
}
Object value = UIManager.get("TitledBorder.position");
if (value instanceof Integer) {
int i = (Integer) value;
if ((0 < i) && (i <= 6)) {
return i;
}
}
else if (value instanceof String) {
String s = (String) value;
if (s.equalsIgnoreCase("ABOVE_TOP")) {
return ABOVE_TOP;
}
if (s.equalsIgnoreCase("TOP")) {
return TOP;
}
if (s.equalsIgnoreCase("BELOW_TOP")) {
return BELOW_TOP;
}
if (s.equalsIgnoreCase("ABOVE_BOTTOM")) {
return ABOVE_BOTTOM;
}
if (s.equalsIgnoreCase("BOTTOM")) {
return BOTTOM;
}
if (s.equalsIgnoreCase("BELOW_BOTTOM")) {
return BELOW_BOTTOM;
}
}
return TOP;
}
private int getJustification(Component c) {
int justification = getTitleJustification();
if ((justification == LEADING) || (justification == DEFAULT_JUSTIFICATION)) {
return c.getComponentOrientation().isLeftToRight() ? LEFT : RIGHT;
}
if (justification == TRAILING) {
return c.getComponentOrientation().isLeftToRight() ? RIGHT : LEFT;
}
return justification;
}
protected Font getFont(Component c) { protected Font getFont(Component c) {
Font font; Font font = getTitleFont();
if ((font = getTitleFont()) != null) { if (font != null) {
return font; return font;
} else if (c != null && (font = c.getFont()) != null) { }
font = UIManager.getFont("TitledBorder.font");
if (font != null) {
return font; return font;
} }
if (c != null) {
font = c.getFont();
if (font != null) {
return font;
}
}
return new Font(Font.DIALOG, Font.PLAIN, 12); return new Font(Font.DIALOG, Font.PLAIN, 12);
} }
private static boolean computeIntersection(Rectangle dest, private Color getColor(Component c) {
int rx, int ry, int rw, int rh) { Color color = getTitleColor();
int x1 = Math.max(rx, dest.x); if (color != null) {
int x2 = Math.min(rx + rw, dest.x + dest.width); return color;
int y1 = Math.max(ry, dest.y); }
int y2 = Math.min(ry + rh, dest.y + dest.height); color = UIManager.getColor("TitledBorder.titleColor");
dest.x = x1; if (color != null) {
dest.y = y1; return color;
dest.width = x2 - x1;
dest.height = y2 - y1;
if (dest.width <= 0 || dest.height <= 0) {
return false;
} }
return true; return (c != null)
? c.getForeground()
: null;
}
private JLabel getLabel(Component c) {
this.label.setText(getTitle());
this.label.setFont(getFont(c));
this.label.setForeground(getColor(c));
this.label.setComponentOrientation(c.getComponentOrientation());
this.label.setEnabled(c.isEnabled());
return this.label;
} }
} }
<html>
<body>
When applet starts, you'll see a checkbox and a label with a titled border.
Turn on the checkbox to disable the label.
The test passes if the title of the border is disabled as well as the label.
<applet width="300" height="200" code="Test4129681.class">
</applet>
</body>
</html>
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4129681
* @summary Tests enabling/disabling of titled border's caption
* @author Sergey Malenkov
* @run applet/manual=yesno Test4129681.html
*/
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
public class Test4129681 extends JApplet implements ItemListener {
private JLabel label;
@Override
public void init() {
JCheckBox check = new JCheckBox("disable");
check.addItemListener(this);
this.label = new JLabel("message");
this.label.setBorder(BorderFactory.createTitledBorder("label"));
this.label.setEnabled(!check.isSelected());
add(BorderLayout.NORTH, check);
add(BorderLayout.CENTER, this.label);
}
public void itemStateChanged(ItemEvent event) {
this.label.setEnabled(ItemEvent.DESELECTED == event.getStateChange());
}
}
<html>
<body>
When applet starts, you'll see a panel with a compound titled border.
If one of its titles is overstriken with the border's line then test fails.
Otherwise test passes.
<applet width="600" height="300" code="Test4760089.class">
</applet>
</body>
</html>
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4760089
* @summary Tests that titled border do not paint inner titled border over its caption
* @author Sergey Malenkov
* @run applet/manual=yesno Test4760089.html
*/
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class Test4760089 extends JApplet {
@Override
public void init() {
Border border = new EtchedBorder();
border = new TitledBorder(border, "LEFT", TitledBorder.LEFT, TitledBorder.TOP);
border = new TitledBorder(border, "RIGHT", TitledBorder.RIGHT, TitledBorder.TOP);
JPanel panel = new JPanel();
panel.setBorder(border);
getContentPane().add(panel);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册