📄 titledborder.java
字号:
* <code>"TitledBorder.font"</code>. * * @see javax.swing.UIManager#getFont(Object) */ public Font getTitleFont() { if (titleFont != null) return titleFont; return UIManager.getFont("TitledBorder.font"); } /** * Retrieves the color for displaying the title text. If no color has * been set, or if it has been set to<code>null</code>, the current * {@link javax.swing.LookAndFeel} will be asked for a color * using the key <code>"TitledBorder.titleColor"</code>. * * @return a color, or <code>null</code> if the current LookAndFeel * does not provide a color for the key * <code>"TitledBorder.titleColor"</code>. * * @see javax.swing.UIManager#getColor(Object) */ public Color getTitleColor() { if (titleColor != null) return titleColor; return UIManager.getColor("TitledBorder.titleColor"); } /** * Sets the text of the title. * * @param title the new title text, or <code>null</code> for displaying * no text at all. */ public void setTitle(String title) { // Swing borders are not JavaBeans, thus no need to fire an event. this.title = title; } /** * Sets the border underneath the title. * * @param border a border, or <code>null</code> to use the * border that is supplied by the current LookAndFeel. * * @see #getBorder() */ public void setBorder(Border border) { // Swing borders are not JavaBeans, thus no need to fire an event. this.border = border; } /** * Sets the vertical position of the title text in relation * to the border. * * @param titlePosition one of the values {@link #ABOVE_TOP}, * {@link #TOP}, {@link #BELOW_TOP}, {@link #ABOVE_BOTTOM}, * {@link #BOTTOM}, {@link #BELOW_BOTTOM}, * or {@link #DEFAULT_POSITION}. * * @throws IllegalArgumentException if an unsupported value is passed * for <code>titlePosition</code>. */ public void setTitlePosition(int titlePosition) { if ((titlePosition < DEFAULT_POSITION) || (titlePosition > BELOW_BOTTOM)) throw new IllegalArgumentException(); // Swing borders are not JavaBeans, thus no need to fire an event. this.titlePosition = titlePosition; } /** * Sets the horizontal alignment of the title text in relation to the border. * * @param titleJustification the new alignment, which must be one of * {@link #LEFT}, {@link #CENTER}, {@link #RIGHT}, {@link #LEADING}, * {@link #TRAILING}, or {@link #DEFAULT_JUSTIFICATION}. * * @throws IllegalArgumentException if an unsupported value is passed * for <code>titleJustification</code>. */ public void setTitleJustification(int titleJustification) { if ((titleJustification < DEFAULT_JUSTIFICATION) || (titleJustification > TRAILING)) throw new IllegalArgumentException(); // Swing borders are not JavaBeans, thus no need to fire an event. this.titleJustification = titleJustification; } /** * Sets the font for displaying the title text. * * @param titleFont the font, or <code>null</code> to use the font * provided by the current {@link javax.swing.LookAndFeel}. * * @see #getTitleFont() */ public void setTitleFont(Font titleFont) { // Swing borders are not JavaBeans, thus no need to fire an event. this.titleFont = titleFont; } /** * Sets the color for displaying the title text. * * @param titleColor the color, or <code>null</code> to use the color * provided by the current {@link javax.swing.LookAndFeel}. * * @see #getTitleColor() */ public void setTitleColor(Color titleColor) { // Swing borders are not JavaBeans, thus no need to fire an event. this.titleColor = titleColor; } /** * Calculates the minimum size needed for displaying the border * and its title. * * @param c the Component for which this TitledBorder consitutes * a border. */ public Dimension getMinimumSize(Component c) { return getMeasurements(c).getMinimumSize(); } /** * Returns the font that is used for displaying the title text for * a given Component. * * @param c the Component for which this TitledBorder is the border. * * @return The font returned by {@link #getTitleFont()}, or a fallback * if {@link #getTitleFont()} returned <code>null</code>. */ protected Font getFont(Component c) { Font f; f = getTitleFont(); if (f != null) return f; return new Font("Dialog", Font.PLAIN, 12); } /** * Returns the horizontal alignment of the title text in relation to * the border, mapping the component-dependent alignment constants * {@link #LEADING}, {@link #TRAILING} and {@link #DEFAULT_JUSTIFICATION} * to the correct value according to the embedded component’s * orientation. * * @param c the Component for which this TitledBorder is the border. * * @return one of the values {@link #LEFT}, {@link #CENTER}, or {@link * #RIGHT}. */ private int getRealTitleJustification(Component c) { switch (titleJustification) { case DEFAULT_JUSTIFICATION: case LEADING: if ((c == null) || c.getComponentOrientation().isLeftToRight()) return LEFT; else return RIGHT; case TRAILING: if ((c == null) || c.getComponentOrientation().isLeftToRight()) return RIGHT; else return LEFT; default: return titleJustification; } } /** * Performs various measurements for the current state of this TitledBorder * and the given Component. */ private Measurements getMeasurements(Component c) { Measurements m = new Measurements(); FontMetrics fmet; m.font = getFont(c); fmet = c.getFontMetrics(m.font); m.border = getBorder(); if (m.border != null) m.borderInsets = m.border.getBorderInsets(c); else m.borderInsets = new Insets(0, 0, 0, 0); if (title != null) { m.trimmedText = title.trim(); if (m.trimmedText.length() == 0) m.trimmedText = null; } m.textAscent = fmet.getAscent(); m.textDescent = fmet.getDescent(); if (m.trimmedText != null) m.textWidth = fmet.stringWidth(m.trimmedText) + 3; m.edgeSpacing = new Insets(EDGE_SPACING, EDGE_SPACING, EDGE_SPACING, EDGE_SPACING); m.borderSpacing = new Insets(0, 0, 0, 0); switch (titlePosition) { case ABOVE_TOP: m.borderSpacing.top += m.textAscent + m.textDescent + TEXT_SPACING; break; case BELOW_TOP: m.edgeSpacing.top += m.textAscent + m.textDescent + TEXT_SPACING; break; case ABOVE_BOTTOM: m.edgeSpacing.bottom += m.textAscent + m.textDescent + TEXT_SPACING; break; case BOTTOM: m.edgeSpacing.bottom += Math.max(m.textAscent - m.borderInsets.bottom, 0); m.borderSpacing.bottom += m.textDescent; break; case BELOW_BOTTOM: m.borderSpacing.bottom += m.textAscent + m.textDescent + TEXT_SPACING; break; default: m.borderSpacing.top += m.textAscent; } return m; } /** * A private helper class for holding the result of measuring the * distances of a TitledBorder. While it would be possible to cache * these objects, it does not seem to be worth the effort. Note that * invalidating the cache would be tricky, especially since there is * no notification mechanism that would inform the cache when * border has changed, so it would return different insets. */ private static class Measurements { /** * The font used for displaying the title text. Note that it can * well be that the TitledBorder’s font is <code>null</code>, * which means that the font is to be retrieved from the current * LookAndFeel. In this case, this <code>font</code> field will * contain the result of the retrieval. Therefore, it is safe * to assume that his <code>font</code> field will never have * a <code>null</code> value. */ Font font; /** * The number of pixels between the base line and the top of the * text box. */ int textAscent; /** * The number of pixels between the base line and the bottom of * the text box. */ int textDescent; /** * The title text after removing leading and trailing white space * characters. If the title consists only of white space, the * value of <code>trimmedText</code> will be <code>null</code>. */ String trimmedText; /** * The width of the trimmed title text in pixels. */ int textWidth; /** * The border that constitues the "interior" border * underneath the title text. */ Border border; /** * The distance between the TitledBorder and the interior border. */ Insets borderSpacing; /** * The width of the interior border, as returned by * <code>border.getBorderInsets()</code>. */ Insets borderInsets; /** * The distance between the interior border and the nested * Component for which this TitledBorder is a border. */ Insets edgeSpacing; /** * Determines the insets of the nested component when it has a * TitledBorder as its border. Used by {@link * TitledBorder#getBorderInsets()}. * * @param i an Insets object for storing the results into, or * <code>null</code> to cause the creation of a * new instance. * * @return the <code>i</code> object, or a new Insets object * if <code>null</code> was passed for <code>i</code>. */ public Insets getContentInsets(Insets i) { if (i == null) i = new Insets(0, 0, 0, 0); i.left = borderSpacing.left + borderInsets.left + edgeSpacing.left; i.right = borderSpacing.right + borderInsets.right + edgeSpacing.right; i.top = borderSpacing.top + borderInsets.top + edgeSpacing.top; i.bottom = borderSpacing.bottom + borderInsets.bottom + edgeSpacing.bottom; return i; } /** * Calculates the minimum size needed for displaying the border * and its title. Used by {@link TitledBorder#getMiminumSize()}. */ public Dimension getMinimumSize() { int width; Insets insets; insets = getContentInsets(null); width = Math.max(insets.left + insets.right, textWidth + 2 * TEXT_INSET_H); return new Dimension(width, insets.top + insets.bottom); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -