📄 noneditabletextareadialog.java
字号:
//------------------------------------------------------------------ protected JTextArea getTextArea( ) { return textArea; } //------------------------------------------------------------------ protected void setTextAreaAttributes( ) { // do nothing } //------------------------------------------------------------------ protected void applyOrientation( ) { // do nothing } //------------------------------------------------------------------ private void init( Window owner, int numColumns, int numRows, String text, boolean canClear ) { // Set icons if ( owner != null ) setIconImages( owner.getIconImages( ) ); //---- Text area scroll pane // Text area textArea = new JTextArea( text ); GuiUtilities.setFont( Constants.FontName.TEXT_FIELD, textArea ); textArea.setBorder( BorderFactory.createEmptyBorder( ) ); textArea.setEditable( false ); textArea.setCaretPosition( 0 ); // Scroll pane JScrollPane textAreaScrollPane = new JScrollPane( textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS ); FontMetrics fontMetrics = textArea.getFontMetrics( textArea.getFont( ) ); int width = numColumns * GuiUtilities.getCharWidth( '0', fontMetrics ); int height = numRows * fontMetrics.getHeight( ); textAreaScrollPane.getViewport( ).setPreferredSize( new Dimension( width, height ) ); textAreaScrollPane.setViewportBorder( BorderFactory. createMatteBorder( 2, 4, 2, 4, textArea.getBackground( ) ) ); // Set text area attributes setTextAreaAttributes( ); //---- Button panel JPanel buttonPanel = new JPanel( new GridLayout( 1, 0, 24, 0 ) ); buttonPanel.setBorder( BorderFactory.createEmptyBorder( 2, 8, 3, 8 ) ); // Button: clear if ( canClear ) { clearButton = new FButton( CLEAR_STR ); clearButton.setMnemonic( KeyEvent.VK_X ); clearButton.setToolTipText( CLEAR_TOOL_TIP_STR ); clearButton.setActionCommand( Command.CLEAR ); clearButton.addActionListener( this ); buttonPanel.add( clearButton ); } // Button: copy copyButton = new FButton( COPY_STR ); copyButton.setMnemonic( KeyEvent.VK_C ); copyButton.setToolTipText( COPY_TOOL_TIP_STR ); copyButton.setActionCommand( Command.COPY ); copyButton.addActionListener( this ); buttonPanel.add( copyButton ); // Button: close closeButton = new FButton( CLOSE_STR ); closeButton.setActionCommand( Command.CLOSE ); closeButton.addActionListener( this ); buttonPanel.add( closeButton ); //---- Main panel GridBagLayout gridBag = new GridBagLayout( ); GridBagConstraints gbc = new GridBagConstraints( ); JPanel mainPanel = new JPanel( gridBag ); 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( 1, 0, 0, 0 ); gridBag.setConstraints( textAreaScrollPane, gbc ); mainPanel.add( textAreaScrollPane ); 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, 2, 0 ); gridBag.setConstraints( buttonPanel, gbc ); mainPanel.add( buttonPanel ); //---- Window // Set content pane setContentPane( mainPanel ); // Update components updateComponents( ); // Set orientation of components applyOrientation( ); // Add key commands to action map KeyAction.create( mainPanel, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, KEY_COMMANDS, 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 Point location = (Point)locations.get( getClass( ) ); if ( location == null ) location = GuiUtilities.getComponentLocation( this, owner ); setLocation( location ); // Set default button getRootPane( ).setDefaultButton( closeButton ); // Show dialog setVisible( true ); } //------------------------------------------------------------------ private void updateComponents( ) { if ( textArea.getText( ).length( ) == 0 ) { copyButton.setEnabled( false ); closeButton.requestFocusInWindow( ); } } //------------------------------------------------------------------ private void doClear( ) { if ( clearButton != null ) { textArea.setText( null ); clearButton.setEnabled( false ); cleared = true; updateComponents( ); } } //------------------------------------------------------------------ private void doCopy( ) { StringSelection selection = new StringSelection( textArea.getText( ) ); try { try { getToolkit( ).getSystemClipboard( ).setContents( selection, selection ); } catch ( IllegalStateException e ) { throw new AppException( ErrorId.CLIPBOARD_UNAVAILABLE, e ); } } catch ( AppException e ) { JOptionPane.showMessageDialog( this, e, CLIPBOARD_ERROR_STR, JOptionPane.ERROR_MESSAGE ); } } //------------------------------------------------------------------ private void doClose( ) { locations.put( getClass( ), getLocation( ) ); setVisible( false ); dispose( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class variables//////////////////////////////////////////////////////////////////////// private static Hashtable<Class,Point> locations = new Hashtable<Class,Point>( );////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private boolean cleared; private JTextArea textArea; private JButton clearButton; private JButton copyButton; private JButton closeButton;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -