📄 tabbedpanel.java
字号:
//-------------------------------------------------------------- public void mouseReleased( MouseEvent event ) { if ( (mouseSelectionListWindow != null) && (event.getWhen( ) > pressTime + CLICK_INTERVAL) ) { mouseSelectionListWindow.updateSelection( event ); mouseSelectionListWindow.doSelection( ); } } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods : MouseMotionListener interface //////////////////////////////////////////////////////////////////// public void mouseDragged( MouseEvent event ) { if ( mouseSelectionListWindow != null ) mouseSelectionListWindow.updateSelection( event ); } //-------------------------------------------------------------- public void mouseMoved( MouseEvent event ) { // do nothing } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////////// private long pressTime; } //================================================================== // TAB CLASS private class Tab extends JComponent implements MouseListener, MouseMotionListener { //////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////// private static final int TOP_MARGIN = 2; private static final int BOTTOM_MARGIN = 3; private static final int TEXT_LEADING_MARGIN = 6; private static final int TEXT_TRAILING_MARGIN = 6; private static final int BUTTON_TRAILING_MARGIN = 1; private static final int BUTTON_ICON_WIDTH = 8; private static final int BUTTON_ICON_HEIGHT = 7; private static final int BUTTON_WIDTH = BUTTON_ICON_WIDTH + 6; private static final int BUTTON_HEIGHT = BUTTON_ICON_HEIGHT + 6; private static final int MIN_WIDTH = TEXT_LEADING_MARGIN; //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private Tab( String title, Action closeAction ) { // Initialise instance variables this.title = title; this.closeAction = closeAction; closeButtonState = ButtonState.NOT_OVER; GuiUtilities.setFont( Constants.FontName.MAIN, this ); FontMetrics fontMetrics = getFontMetrics( getFont( ) ); preferredWidth = TEXT_LEADING_MARGIN + fontMetrics.stringWidth( title ) + TEXT_TRAILING_MARGIN + BUTTON_WIDTH + BUTTON_TRAILING_MARGIN; height = TOP_MARGIN + Math.max( fontMetrics.getHeight( ), BUTTON_HEIGHT ) + BOTTOM_MARGIN; // Set attributes setOpaque( true ); setFocusable( false ); // Add listeners addMouseListener( this ); addMouseMotionListener( this ); } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods : MouseListener interface //////////////////////////////////////////////////////////////////// public void mouseClicked( MouseEvent event ) { // do nothing } //-------------------------------------------------------------- public void mouseEntered( MouseEvent event ) { setCloseButtonState( inCloseButton( event.getPoint( ) ) ? ButtonState.OVER : ButtonState.NOT_OVER ); } //-------------------------------------------------------------- public void mouseExited( MouseEvent event ) { setCloseButtonState( ButtonState.NOT_OVER ); } //-------------------------------------------------------------- public void mousePressed( MouseEvent event ) { setCloseButtonState( inCloseButton( event.getPoint( ) ) ? ButtonState.PRESSED : ButtonState.NOT_OVER ); if ( closeButtonState == ButtonState.PRESSED ) closeButtonPressed = true; else showTab( getIndex( ) ); } //-------------------------------------------------------------- public void mouseReleased( MouseEvent event ) { setCloseButtonState( inCloseButton( event.getPoint( ) ) ? ButtonState.OVER : ButtonState.NOT_OVER ); if ( (closeButtonState == ButtonState.OVER) && closeButtonPressed && (closeAction != null) ) { String command = closeAction.getValue( Action.ACTION_COMMAND_KEY ).toString( ) + Integer.toString( getIndex( ) ); closeAction.actionPerformed( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, command, event.getModifiersEx( ) ) ); } closeButtonPressed = false; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods : MouseMotionListener interface //////////////////////////////////////////////////////////////////// public void mouseDragged( MouseEvent event ) { setCloseButtonState( inCloseButton( event.getPoint( ) ) ? ButtonState.PRESSED : ButtonState.NOT_OVER ); } //-------------------------------------------------------------- public void mouseMoved( MouseEvent event ) { setCloseButtonState( inCloseButton( event.getPoint( ) ) ? ButtonState.OVER : ButtonState.NOT_OVER ); } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods : overriding methods //////////////////////////////////////////////////////////////////// public Dimension getPreferredSize( ) { return new Dimension( preferredWidth, height ); } //-------------------------------------------------------------- protected void paintComponent( Graphics gr ) { // Fill background Rectangle rect = gr.getClipBounds( ); gr.setColor( selected ? SELECTED_BACKGROUND_COLOUR : BACKGROUND_COLOUR ); gr.fillRect( rect.x, rect.y, rect.width, rect.height ); // Get text int width = getWidth( ); int height = getHeight( ); boolean fullWidth = (width == preferredWidth); FontMetrics fontMetrics = gr.getFontMetrics( ); String str = title; if ( !fullWidth ) { int maxTextWidth = width - TEXT_LEADING_MARGIN - TEXT_TRAILING_MARGIN; str = (maxTextWidth < fontMetrics.stringWidth( Constants.ELLIPSIS_STR )) ? null : TextUtilities.getWidthLimitedString( str, fontMetrics, maxTextWidth, false ); } // Draw text boolean leftToRight = getComponentOrientation( ).isLeftToRight( ); if ( str != null ) { gr.setColor( TEXT_COLOUR ); gr.drawString( str, leftToRight ? TEXT_LEADING_MARGIN : width - TEXT_LEADING_MARGIN - fontMetrics.stringWidth( str ), (height - fontMetrics.getHeight( )) / 2 + fontMetrics.getAscent( ) ); } // Draw close button if ( fullWidth ) { int x = leftToRight ? width - BUTTON_WIDTH - BUTTON_TRAILING_MARGIN : BUTTON_TRAILING_MARGIN; int y = (height - BUTTON_HEIGHT) / 2; if ( closeButtonState != ButtonState.NOT_OVER ) { gr.setColor( BUTTON_BACKGROUND_COLOUR ); gr.fillRect( x, y, BUTTON_WIDTH, BUTTON_HEIGHT ); int x1 = x; int x2 = x1 + BUTTON_WIDTH - 1; int y1 = y; int y2 = y1 + BUTTON_HEIGHT - 1; gr.setColor( closeButtonState == ButtonState.PRESSED ? LIGHT_HIGHLIGHT_COLOUR : DARK_SHADOW_COLOUR ); gr.drawLine( x2, y1, x2, y2 ); --x2; gr.drawLine( x1, y2, x2, y2 ); --y2; gr.setColor( closeButtonState == ButtonState.PRESSED ? DARK_SHADOW_COLOUR : LIGHT_HIGHLIGHT_COLOUR ); gr.drawLine( x1, y1, x1, y2 ); ++x1; gr.drawLine( x1, y1, x2, y1 ); ++y1; } gr.drawImage( CROSS_ICON.getImage( ), x + (BUTTON_WIDTH - BUTTON_ICON_WIDTH) / 2, y + (BUTTON_HEIGHT - BUTTON_ICON_HEIGHT) / 2, null ); } // Draw border if ( fullWidth ) { gr.setColor( HEADER_BACKGROUND_COLOUR ); gr.fillRect( 0, 0, 2, 2 ); gr.fillRect( width - 2, 0, 2, 2 ); gr.setColor( selected ? LIGHT_HIGHLIGHT_COLOUR : DARK_HIGHLIGHT_COLOUR ); gr.drawLine( 0, 2, 0, height - 1 ); gr.drawLine( 1, 1, 1, 1 ); gr.drawLine( 2, 0, width - 3, 0 ); gr.setColor( selected ? DARK_SHADOW_COLOUR : LIGHT_SHADOW_COLOUR ); gr.drawLine( width - 2, 1, width - 2, 1 ); gr.drawLine( width - 1, 2, width - 1, height - 1 ); } else { if ( leftToRight ) { gr.setColor( HEADER_BACKGROUND_COLOUR ); gr.fillRect( 0, 0, 2, 2 ); gr.setColor( selected ? LIGHT_HIGHLIGHT_COLOUR : DARK_HIGHLIGHT_COLOUR ); gr.drawLine( 0, 2, 0, height - 1 ); gr.drawLine( 1, 1, 1, 1 ); gr.drawLine( 2, 0, width - 1, 0 ); } else { gr.setColor( HEADER_BACKGROUND_COLOUR ); gr.fillRect( width - 2, 0, 2, 2 ); gr.setColor( selected ? LIGHT_HIGHLIGHT_COLOUR : DARK_HIGHLIGHT_COLOUR ); gr.drawLine( 0, 0, width - 3, 0 ); gr.setColor( selected ? DARK_SHADOW_COLOUR : LIGHT_SHADOW_COLOUR ); gr.drawLine( width - 2, 1, width - 2, 1 ); gr.drawLine( width - 1, 2, width - 1, height - 1 ); } } if ( !selected ) { gr.setColor( HEADER_BORDER_BOTTOM_COLOUR ); gr.drawLine( 0, height - 1, width - 1, height - 1 ); } } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods //////////////////////////////////////////////////////////////////// private String getTitle( ) { return title; } //-------------------------------------------------------------- private void setSelected( boolean selected ) { if ( this.selected != selected ) { this.selected = selected; repaint( ); } } //-------------------------------------------------------------- private void setTitle( String title ) { if ( !this.title.equals( title ) ) { this.title = title; FontMetrics fontMetrics = getFontMetrics( getFont( ) ); int width = TEXT_LEADING_MARGIN + fontMetrics.stringWidth( title ) + TEXT_TRAILING_MARGIN + BUTTON_WIDTH + BUTTON_TRAILING_MARGIN; if ( preferredWidth != width ) { preferredWidth = width; updateTabs( ); } else repaint( ); } } //-------------------------------------------------------------- private boolean inCloseButton( Point point ) { int width = getWidth( ); if ( width < preferredWidth ) return false; int x1 = 0; int x2 = 0; if ( getComponentOrientation( ).isLeftToRight( ) ) { x2 = width - BUTTON_TRAILING_MARGIN; x1 = x2 - BUTTON_WIDTH; } else { x1 = BUTTON_TRAILING_MARGIN; x2 = x1 + BUTTON_WIDTH; } int y1 = (getHeight( ) - BUTTON_HEIGHT) / 2; int y2 = y1 + BUTTON_HEIGHT; return ( (point.x >= x1) && (point.x < x2) && (point.y >= y1) && (point.y < y2) ); } //-------------------------------------------------------------- private void setCloseButtonState( ButtonState buttonState ) { if ( closeButtonState != buttonState ) { closeButtonState = buttonState; repaint( ); } } //-------------------------------------------------------------- private int getIndex( ) { for ( int i = 0; i < elements.size( ); ++i ) { if ( elements.get( i ).tab == this ) return i; } return -1; } //--------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -