commentdialog.java
来自「FuncPlotter is a combined Java applicati」· Java 代码 · 共 358 行
JAVA
358 行
/*====================================================================*\CommentDialog.javaComment dialog box 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/>.\*====================================================================*/// IMPORTSimport gui.GuiUtilities;import java.awt.Dialog;import java.awt.Dimension;import java.awt.FontMetrics;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.Point;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JDialog;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.KeyStroke;import util.KeyAction;//----------------------------------------------------------------------// COMMENT DIALOG BOX CLASSclass CommentDialog extends JDialog implements ActionListener{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// private static final int COMMENT_AREA_NUM_ROWS = 16; private static final int COMMENT_AREA_NUM_COLUMNS = 80; private static final String COMMENT_STR = "Comment:"; // Commands private interface Command { String ACCEPT = "accept"; String CLOSE = "close"; }////////////////////////////////////////////////////////////////////////// Member classes : inner classes//////////////////////////////////////////////////////////////////////// // WINDOW EVENT HANDLER CLASS private class WindowEventHandler extends WindowAdapter { //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private WindowEventHandler( ) { } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods : overriding methods //////////////////////////////////////////////////////////////////// public void windowClosing( WindowEvent event ) { doClose( ); } //-------------------------------------------------------------- } //==================================================================////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// private CommentDialog( Window owner, String titleStr, String text ) { super( owner, titleStr, Dialog.ModalityType.APPLICATION_MODAL ); init( owner, text ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static CommentDialog showDialog( Window owner, String titleStr, String text ) { return new CommentDialog( owner, titleStr, text ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : ActionListener interface//////////////////////////////////////////////////////////////////////// public void actionPerformed( ActionEvent event ) { try { String command = event.getActionCommand( ); String methodName = AppConstants.COMMAND_METHOD_PREFIX + command.substring( 0, 1 ).toUpperCase( ) + command.substring( 1 ); Util.getDeclaredMethod( getClass( ), methodName, false ).invoke( this ); } catch ( Exception e ) { e.printStackTrace( ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public String getComment( ) { return ( accepted ? commentArea.getText( ).trim( ) : null ); } //------------------------------------------------------------------ private void init( Window owner, String text ) { // Set icons setIconImages( owner.getIconImages( ) ); //---- Text area // Text area: comment commentArea = new JTextArea( text ); AppFont.TEXT_FIELD.setFont( commentArea ); commentArea.setBorder( BorderFactory.createEmptyBorder( ) ); // Scroll pane: comment JScrollPane commentAreaScrollPane = new JScrollPane( commentArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS ); FontMetrics fontMetrics = commentArea.getFontMetrics( commentArea.getFont( ) ); int width = COMMENT_AREA_NUM_COLUMNS * GuiUtilities.getCharWidth( '0', fontMetrics ); int height = COMMENT_AREA_NUM_ROWS * fontMetrics.getHeight( ); commentAreaScrollPane.getViewport( ).setPreferredSize( new Dimension( width, height ) ); commentAreaScrollPane.setViewportBorder( BorderFactory. createMatteBorder( 2, 4, 2, 4, commentArea.getBackground( ) ) ); //---- Button panel JPanel buttonPanel = new JPanel( new GridLayout( 1, 0, 8, 0 ) ); buttonPanel.setBorder( BorderFactory.createEmptyBorder( 2, 8, 3, 8 ) ); // Button: OK JButton okButton = new FButton( AppConstants.OK_STR ); okButton.setActionCommand( Command.ACCEPT ); okButton.addActionListener( this ); buttonPanel.add( okButton ); // Button: cancel JButton cancelButton = new FButton( AppConstants.CANCEL_STR ); cancelButton.setActionCommand( Command.CLOSE ); cancelButton.addActionListener( this ); buttonPanel.add( cancelButton ); //---- Main panel GridBagLayout gridBag = new GridBagLayout( ); GridBagConstraints gbc = new GridBagConstraints( ); JPanel mainPanel = new JPanel( gridBag ); mainPanel.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) ); int gridY = 0; gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 0, 0, 0 ); gridBag.setConstraints( commentAreaScrollPane, gbc ); mainPanel.add( commentAreaScrollPane ); gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 4, 0, 0, 0 ); gridBag.setConstraints( buttonPanel, gbc ); mainPanel.add( buttonPanel ); //---- Window // Set content pane setContentPane( mainPanel ); // Set orientation of components according to locale App.applyOrientationByLocale( this ); // Add key commands to action map KeyAction.create( mainPanel, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), Command.CLOSE, this ); // Dispose of window explicitly setDefaultCloseOperation( DO_NOTHING_ON_CLOSE ); // Handle window closing addWindowListener( new WindowEventHandler( ) ); // Prevent dialog from being resized setResizable( false ); // Resize dialog to its preferred size pack( ); // Set location of dialog box if ( location == null ) location = GuiUtilities.getComponentLocation( this, owner ); setLocation( location ); // Set default button getRootPane( ).setDefaultButton( okButton ); // Set focus commentArea.requestFocusInWindow( ); commentArea.setCaretPosition( commentArea.getText( ).length( ) ); // Show dialog setVisible( true ); } //------------------------------------------------------------------ private void doAccept( ) { accepted = true; doClose( ); } //------------------------------------------------------------------ private void doClose( ) { location = getLocation( ); setVisible( false ); dispose( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class variables//////////////////////////////////////////////////////////////////////// private static Point location;////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private JTextArea commentArea; private boolean accepted;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?