📄 preferencesdialog.java
字号:
//////////////////////////////////////////////////////////////////// // Member classes : non-inner classes //////////////////////////////////////////////////////////////////// // SIZE SPINNER CLASS private static class SizeSpinner extends IntegerSpinner { //////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////// private SizeSpinner( int value ) { super( value, MIN_SIZE, MAX_SIZE, SIZE_FIELD_LENGTH ); AppFont.TEXT_FIELD.setFont( this ); } //---------------------------------------------------------- //////////////////////////////////////////////////////////////// // Instance methods : overriding methods //////////////////////////////////////////////////////////////// protected int getEditorValue( ) throws NumberFormatException { IntegerValueField field = (IntegerValueField)getEditor( ); return ( field.isEmpty( ) ? 0 : field.getValue( ) ); } //---------------------------------------------------------- protected void setEditorValue( int value ) { IntegerValueField field = (IntegerValueField)getEditor( ); if ( value == 0 ) field.setText( null ); else field.setValue( value ); } //---------------------------------------------------------- } //============================================================== //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private FontPanel( FontEx font, String[] fontNames ) { nameComboBox = new FComboBox( ); nameComboBox.addItem( DEFAULT_FONT_STR ); for ( String fontName : fontNames ) nameComboBox.addItem( fontName ); nameComboBox.setSelectedIndex( Util.getIndex( fontNames, font.getName( ) ) + 1 ); styleComboBox = new FComboBox( FontEx.Style.values( ) ); styleComboBox.setSelectedItem( font.getStyle( ) ); sizeSpinner = new SizeSpinner( font.getSize( ) ); } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods //////////////////////////////////////////////////////////////////// public FontEx getFont( ) { String name = (nameComboBox.getSelectedIndex( ) <= 0) ? null : (String)nameComboBox.getSelectedItem( ); return new FontEx( name, (FontEx.Style)styleComboBox.getSelectedItem( ), sizeSpinner.getIntValue( ) ); } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////////// JComboBox nameComboBox; JComboBox styleComboBox; SizeSpinner sizeSpinner; } //==================================================================////////////////////////////////////////////////////////////////////////// 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 PreferencesDialog( Window owner ) { super( owner, TITLE_STR, Dialog.ModalityType.APPLICATION_MODAL ); init( owner ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static PreferencesDialog showDialog( Window owner ) { return new PreferencesDialog( owner ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// 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 ); int index = methodName.indexOf( '.' ); if ( index < 0 ) Util.getDeclaredMethod( getClass( ), methodName, false ).invoke( this ); else Util.getDeclaredMethod( getClass( ), methodName.substring( 0, index ), true ). invoke( this, methodName.substring( index + 1 ) ); } catch ( Exception e ) { e.printStackTrace( ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : ChangeListener interface//////////////////////////////////////////////////////////////////////// public void stateChanged( ChangeEvent event ) { tabIndex = tabbedPane.getSelectedIndex( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public boolean isAccepted( ) { return accepted; } //------------------------------------------------------------------ private void init( Window owner ) { // Set icons setIconImages( owner.getIconImages( ) ); //---- Tabbed pane tabbedPane = new JTabbedPane( ); AppFont.MAIN.setFont( tabbedPane ); tabbedPane.addTab( GENERAL_STR, getPanelGeneral( ) ); tabbedPane.addTab( APPEARANCE_STR, getPanelAppearance( ) ); tabbedPane.addTab( PLOT_STR, getPanelPlot( ) ); tabbedPane.addTab( FUNCTION_STR, getPanelFunction( ) ); tabbedPane.addTab( FONTS_STR, getPanelFonts( ) ); tabbedPane.setSelectedIndex( tabIndex ); tabbedPane.addChangeListener( this ); //---- Button panel: save configuration JPanel saveButtonPanel = new JPanel( new GridLayout( 1, 0, 8, 0 ) ); // Button: save configuration JButton saveButton = new FButton( SAVE_CONFIGURATION_STR + AppConstants.ELLIPSIS_STR ); saveButton.setActionCommand( Command.SAVE_CONFIGURATION ); saveButton.addActionListener( this ); saveButtonPanel.add( saveButton ); //---- Button panel: OK, cancel JPanel okCancelButtonPanel = new JPanel( new GridLayout( 1, 0, 8, 0 ) ); // Button: OK JButton okButton = new FButton( AppConstants.OK_STR ); okButton.setActionCommand( Command.ACCEPT ); okButton.addActionListener( this ); okCancelButtonPanel.add( okButton ); // Button: cancel JButton cancelButton = new FButton( AppConstants.CANCEL_STR ); cancelButton.setActionCommand( Command.CLOSE ); cancelButton.addActionListener( this ); okCancelButtonPanel.add( cancelButton ); //---- Button panel GridBagLayout gridBag = new GridBagLayout( ); GridBagConstraints gbc = new GridBagConstraints( ); JPanel buttonPanel = new JPanel( gridBag ); buttonPanel.setBorder( BorderFactory.createEmptyBorder( 2, 24, 3, 24 ) ); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.5; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 0, 0, 12 ); gridBag.setConstraints( saveButtonPanel, gbc ); buttonPanel.add( saveButtonPanel ); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.5; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 0, 12, 0, 0 ); gridBag.setConstraints( okCancelButtonPanel, gbc ); buttonPanel.add( okCancelButtonPanel ); //---- Main panel JPanel mainPanel = new JPanel( gridBag ); mainPanel.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) ); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets( 0, 0, 0, 0 ); gridBag.setConstraints( tabbedPane, gbc ); mainPanel.add( tabbedPane ); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; 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 ); // Show dialog setVisible( true ); } //------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -