📄 titledborder.java
字号:
/*====================================================================*\TitledBorder.javaTitled border class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program. If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage gui;//----------------------------------------------------------------------// IMPORTSimport java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Insets;import javax.swing.BorderFactory;import javax.swing.JComponent;import javax.swing.border.AbstractBorder;//----------------------------------------------------------------------// TITLED BORDER CLASSpublic class TitledBorder extends AbstractBorder{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// public static final Color DEFAULT_BORDER_COLOUR = new Color( 160, 160, 160 ); public static final Color DEFAULT_TITLE_BACKGROUND_COLOUR = new Color( 184, 212, 192 ); public static final Color DEFAULT_TITLE_FOREGROUND_COLOUR = Color.BLACK; private static final int TITLE_HORIZONTAL_MARGIN = 5; private static final int TITLE_VERTICAL_MARGIN = 2; private static final int DEFAULT_PADDING = 6;////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// public TitledBorder( String text ) { this( text, DEFAULT_BORDER_COLOUR, DEFAULT_TITLE_BACKGROUND_COLOUR, DEFAULT_TITLE_FOREGROUND_COLOUR ); } //------------------------------------------------------------------ public TitledBorder( String text, Color borderColour ) { this( text, borderColour, DEFAULT_TITLE_BACKGROUND_COLOUR, DEFAULT_TITLE_FOREGROUND_COLOUR ); } //------------------------------------------------------------------ public TitledBorder( String text, Color borderColour, Color titleBackgroundColour, Color titleForegroundColour ) { this.text = text; this.borderColour = borderColour; this.titleBackgroundColour = titleBackgroundColour; this.titleForegroundColour = titleForegroundColour; } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static void setPaddedBorder( JComponent component, String text ) { setPaddedBorder( component, text, DEFAULT_PADDING, DEFAULT_PADDING, DEFAULT_PADDING, DEFAULT_PADDING ); } //------------------------------------------------------------------ public static void setPaddedBorder( JComponent component, String text, int padding ) { setPaddedBorder( component, text, padding, padding, padding, padding ); } //------------------------------------------------------------------ public static void setPaddedBorder( JComponent component, String text, int vertical, int horizontal ) { setPaddedBorder( component, text, vertical, horizontal, vertical, horizontal ); } //------------------------------------------------------------------ public static void setPaddedBorder( JComponent component, String text, int top, int left, int bottom, int right ) { component.setBorder( BorderFactory.createCompoundBorder( new TitledBorder( text ), BorderFactory.createEmptyBorder( top, left, bottom, right ) ) ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : overriding methods//////////////////////////////////////////////////////////////////////// public Insets getBorderInsets( Component component ) { return getBorderInsets( component, new Insets( 0, 0, 0, 0 ) ); } //------------------------------------------------------------------ public Insets getBorderInsets( Component component, Insets insets ) { insets.top = 2 * TITLE_VERTICAL_MARGIN + component.getFontMetrics( GuiUtilities.getFont( Constants.FontName.MAIN ) ). getHeight( ); insets.bottom = 1; insets.left = 1; insets.right = 1; return insets; } //------------------------------------------------------------------ public boolean isBorderOpaque( ) { return true; } //------------------------------------------------------------------ public void paintBorder( Component component, Graphics gr, int x, int y, int width, int height ) { // Set font gr.setFont( GuiUtilities.getFont( Constants.FontName.MAIN ) ); // Get dimensions of text and title FontMetrics fontMetrics = gr.getFontMetrics( ); int textWidth = fontMetrics.stringWidth( text ); int titleWidth = Math.min( width, 2 * TITLE_HORIZONTAL_MARGIN + textWidth ); int titleHeight = Math.min( height, 2 * TITLE_VERTICAL_MARGIN + fontMetrics.getHeight( ) ); // Fill component background of title gr.setColor( component.getBackground( ) ); gr.fillRect( x, y, width, titleHeight ); // Fill background of title boolean leftToRight = component.getComponentOrientation( ).isLeftToRight( ); gr.setColor( titleBackgroundColour ); gr.fillRect( leftToRight ? x : x + width - titleWidth, y, titleWidth, titleHeight ); // Draw text gr.setColor( titleForegroundColour ); gr.drawString( text, leftToRight ? x + TITLE_HORIZONTAL_MARGIN : x + width - TITLE_HORIZONTAL_MARGIN - textWidth, TITLE_VERTICAL_MARGIN + fontMetrics.getAscent( ) ); // Draw border gr.setColor( borderColour ); gr.drawRect( x, y, x + width - 1, y + height - 1 ); // Draw title border int y1 = y + 1; int y2 = y + titleHeight - 1; if ( leftToRight ) { int x1 = x + 1; int x2 = x + titleWidth - 1; gr.drawLine( x2, y1, x2, y2 ); --x2; gr.drawLine( x1, y2, x2, y2 ); } else { int x2 = x + width - 1; int x1 = x2 - titleWidth + 1; gr.drawLine( x1, y1, x1, y2 ); ++x1; gr.drawLine( x1, y2, x2, y2 ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public Color getBorderColour( ) { return borderColour; } //------------------------------------------------------------------ public Color getTitleBackgroundColour( ) { return titleBackgroundColour; } //------------------------------------------------------------------ public Color getTitleForegroundColour( ) { return titleForegroundColour; } //------------------------------------------------------------------ public Dimension getTitleSize( Component component ) { FontMetrics fontMetrics = component.getFontMetrics( GuiUtilities.getFont( Constants.FontName.MAIN ) ); return new Dimension( 2 * TITLE_HORIZONTAL_MARGIN + fontMetrics.stringWidth( text ), 2 * TITLE_VERTICAL_MARGIN + fontMetrics.getHeight( ) ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private String text; private Color borderColour; private Color titleBackgroundColour; private Color titleForegroundColour;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -