📄 guiutilities.java
字号:
/*====================================================================*\GuiUtilities.javaGUI utility methods 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.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.GraphicsConfiguration;import java.awt.GraphicsEnvironment;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.Toolkit;import javax.swing.BorderFactory;import javax.swing.Box;import javax.swing.JComponent;import javax.swing.JPanel;import javax.swing.text.JTextComponent;//----------------------------------------------------------------------// GUI UTILITY METHODS CLASSpublic class GuiUtilities{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// public static final Color LINE_BORDER_COLOUR = new Color( 160, 160, 160 ); private static final String CLASS_NAME_APP_FONT = "AppFont"; private static final String METHOD_NAME_GET_FONT = "getFont"; private static final String METHOD_NAME_SET_FONT = "setFont"; private static final int DEFAULT_BORDER_PADDING = 6;////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// private GuiUtilities( ) { } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static int getCharWidth( int codePoint, FontMetrics fontMetrics ) { return fontMetrics.charWidth( fontMetrics.getFont( ).canDisplay( codePoint ) ? codePoint : fontMetrics.getFont( ).getMissingGlyphCode( ) ); } //------------------------------------------------------------------ public static Toolkit getToolkit( Component component ) { Toolkit toolkit = null; if ( component != null ) toolkit = component.getToolkit( ); if ( toolkit == null ) toolkit = Toolkit.getDefaultToolkit( ); return toolkit; } //------------------------------------------------------------------ public static Insets getScreenInsets( Component component ) { Insets insets = null; Toolkit toolkit = getToolkit( component ); if ( toolkit != null ) { GraphicsConfiguration graphConfig = null; if ( component != null ) graphConfig = component.getGraphicsConfiguration( ); if ( graphConfig == null ) graphConfig = GraphicsEnvironment.getLocalGraphicsEnvironment( ). getDefaultScreenDevice( ).getDefaultConfiguration( ); if ( graphConfig != null ) insets = toolkit.getScreenInsets( graphConfig ); } return insets; } //------------------------------------------------------------------ public static Rectangle getScreenBounds( ) { return getScreenBounds( null ); } //------------------------------------------------------------------ public static Rectangle getScreenBounds( Component component ) { Dimension screenSize = getToolkit( component ).getScreenSize( ); Insets insets = getScreenInsets( component ); return new Rectangle( insets.left, insets.top, screenSize.width - insets.left - insets.right, screenSize.height - insets.top - insets.bottom ); } //------------------------------------------------------------------ public static Point getLocationWithinScreen( Component component, Point point ) { Rectangle rect = new Rectangle( new Point( point.x, Math.max( 0, point.y ) ), component.getSize( ) ); Rectangle screenRect = getScreenBounds( component ); return ( rect.intersects( screenRect ) ? rect.getLocation( ) : screenRect.getLocation( ) ); } //------------------------------------------------------------------ public static Point getComponentLocation( Component component, Point point ) { Dimension screenSize = getToolkit( component ).getScreenSize( ); Insets insets = getScreenInsets( component ); return new Point( Math.max( insets.left, Math.min( point.x, screenSize.width - insets.right - component.getWidth( ) ) ), Math.max( insets.top, Math.min( point.y, screenSize.height - insets.bottom - component.getHeight( ) ) ) ); } //------------------------------------------------------------------ public static Point getComponentLocation( Component component, Rectangle rect ) { Rectangle screenRect = getScreenBounds( component ); int width = component.getWidth( ); int height = component.getHeight( ); int deltaWidth = screenRect.width - width; int deltaHeight = screenRect.height - height; int x = (rect == null) ? screenRect.x + Math.max( 0, Math.min( deltaWidth / 2, deltaWidth ) ) : Math.max( screenRect.x, Math.min( rect.x + (rect.width - width) / 2, screenRect.x + deltaWidth ) ); int y = (rect == null) ? screenRect.y + Math.max( 0, Math.min( deltaHeight / 2, deltaHeight ) ) : Math.max( screenRect.y, Math.min( rect.y + (rect.height - height) / 2, screenRect.y + deltaHeight ) ); return new Point( x, y ); } //------------------------------------------------------------------ public static Point getComponentLocation( Component component, Component relativeLocator ) { return getComponentLocation( component, (relativeLocator == null) ? null : relativeLocator.getBounds( ) ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -