📄 tabbedpanel.java
字号:
} catch ( Exception e ) { e.printStackTrace( ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : ComponentListener interface//////////////////////////////////////////////////////////////////////// public void componentHidden( ComponentEvent event ) { // do nothing } //------------------------------------------------------------------ public void componentMoved( ComponentEvent event ) { // do nothing } //------------------------------------------------------------------ public void componentResized( ComponentEvent event ) { if ( !elements.isEmpty( ) ) updateStartIndex( true ); } //------------------------------------------------------------------ public void componentShown( ComponentEvent event ) { // do nothing } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : PropertyChangeListener interface//////////////////////////////////////////////////////////////////////// /** * Tracks changes to the focused component. If the new focus owner is a descendant of this panel, the * focus-owner field of its ancestor in the list of elements is updated accordingly. If the new focus * owner is not a descendant of this panel, any key-selection list or mouse-selection list that is * currently display is destroyed. * * @param event the {@code PropertyChangeEvent} that notifies the change of focus owner. */ public void propertyChange( PropertyChangeEvent event ) { Component focusOwner = (Component)event.getNewValue( ); if ( isAncestorOf( focusOwner ) ) { for ( Element element : elements ) { if ( SwingUtilities.isDescendingFrom( focusOwner, element.component ) ) { element.focusOwner = focusOwner; break; } } } else { destroyKeySelectionList( ); destroyMouseSelectionList( ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : overriding methods//////////////////////////////////////////////////////////////////////// public Dimension getMinimumSize( ) { if ( isMinimumSizeSet( ) ) return super.getMinimumSize( ); int maxWidth = 0; int maxHeight = 0; for ( Element element : elements ) { Dimension elementSize = element.component.getMinimumSize( ); if ( maxWidth < elementSize.width ) maxWidth = elementSize.width; if ( maxHeight < elementSize.height ) maxHeight = elementSize.height; } return new Dimension( BORDER_LEFT + Math.max( MIN_HEADER_WIDTH, maxWidth ) + BORDER_RIGHT, BORDER_TOP + headerHeight + maxHeight + BORDER_BOTTOM ); } //------------------------------------------------------------------ public Dimension getPreferredSize( ) { if ( isPreferredSizeSet( ) ) return super.getPreferredSize( ); int maxWidth = 0; int maxHeight = 0; for ( Element element : elements ) { Dimension elementSize = element.component.getPreferredSize( ); if ( maxWidth < elementSize.width ) maxWidth = elementSize.width; if ( maxHeight < elementSize.height ) maxHeight = elementSize.height; } return new Dimension( BORDER_LEFT + maxWidth + BORDER_RIGHT, BORDER_TOP + headerHeight + maxHeight + BORDER_BOTTOM ); } //------------------------------------------------------------------ public void doLayout( ) { // Perform layout Component focusOwner = null; if ( elements.isEmpty( ) ) focusOwner = getWindow( ); else { // Tabs boolean leftToRight = getComponentOrientation( ).isLeftToRight( ); int width = getWidth( ); int maxTotalTabWidth = getMaxTotalTabWidth( ); int x = BORDER_LEFT; for ( int i = 0; i < getComponentCount( ); ++i ) { Component component = getComponent( i ); if ( component instanceof Tab ) { int tabHeight = component.getPreferredSize( ).height; int tabWidth = Math.min( component.getPreferredSize( ).width, maxTotalTabWidth - x ); component.setBounds( leftToRight ? x : width - x - tabWidth, BORDER_TOP + headerHeight - tabHeight, tabWidth, tabHeight ); x += tabWidth; } } // Buttons x = width - (2 * SCROLL_BUTTON_WIDTH + SCROLL_BUTTON_TRAILING_MARGIN + LIST_BUTTON_WIDTH + LIST_BUTTON_TRAILING_MARGIN); int y = BORDER_TOP + (headerHeight - HEADER_BUTTON_HEIGHT) / 2; leftScrollButton.setBounds( leftToRight ? x : width - x - 2 * SCROLL_BUTTON_WIDTH, y, SCROLL_BUTTON_WIDTH, HEADER_BUTTON_HEIGHT ); x += SCROLL_BUTTON_WIDTH; rightScrollButton.setBounds( leftToRight ? x : width - x, y, SCROLL_BUTTON_WIDTH, HEADER_BUTTON_HEIGHT ); x += SCROLL_BUTTON_WIDTH + SCROLL_BUTTON_TRAILING_MARGIN; listButton.setBounds( leftToRight ? x : width - x - LIST_BUTTON_WIDTH, y, LIST_BUTTON_WIDTH, HEADER_BUTTON_HEIGHT ); // Main component Element element = elements.get( selectedIndex ); x = BORDER_LEFT; y = BORDER_TOP + headerHeight; element.component.setBounds( x, y, Math.max( 0, width - x - BORDER_RIGHT ), Math.max( 0, getHeight( ) - y - BORDER_BOTTOM ) ); focusOwner = element.focusOwner; } // Request focus if ( focusOwner != null ) focusOwner.requestFocusInWindow( ); } //------------------------------------------------------------------ protected void paintComponent( Graphics gr ) { // Fill background Rectangle rect = gr.getClipBounds( ); gr.setColor( getBackground( ) ); gr.fillRect( rect.x, rect.y, rect.width, rect.height ); // Draw header background if ( !elements.isEmpty( ) ) { gr.setColor( HEADER_BACKGROUND_COLOUR ); gr.fillRect( rect.x, BORDER_TOP, rect.width, headerHeight ); int y = BORDER_TOP + headerHeight - 1; gr.setColor( HEADER_BORDER_BOTTOM_COLOUR ); gr.drawLine( rect.x, y, rect.x + rect.width - 1, y ); } // Draw border int x1 = 0; int x2 = x1 + getWidth( ) - 1; int y1 = 0; int y2 = y1 + getHeight( ) - 1; gr.setColor( LIGHT_HIGHLIGHT_COLOUR ); gr.drawLine( x2, y1, x2, y2 ); --x2; gr.drawLine( x1, y2, x2, y2 ); --y2; gr.setColor( DARK_SHADOW_COLOUR ); gr.drawLine( x1, y1, x1, y2 ); ++x1; gr.drawLine( x1, y1, x2, y1 ); ++y1; } //------------------------------------------------------------------ protected boolean processKeyBinding( KeyStroke keyStroke, KeyEvent event, int condition, boolean pressed ) { if ( (keySelectionListWindow != null) && (event.getKeyCode( ) == KeyEvent.VK_CONTROL) && !pressed ) keySelectionListWindow.doSelection( ); return super.processKeyBinding( keyStroke, event, condition, pressed ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public int getNumTabs( ) { return elements.size( ); } //------------------------------------------------------------------ public int getSelectedIndex( ) { return selectedIndex; } //------------------------------------------------------------------ public Component getSelectedComponent( ) { return ( (selectedIndex < 0) ? null : elements.get( selectedIndex ).component ); } //------------------------------------------------------------------ public Dimension getFrameSize( ) { return new Dimension( BORDER_LEFT + BORDER_RIGHT, BORDER_TOP + headerHeight + BORDER_BOTTOM ); } //------------------------------------------------------------------ public void setIgnoreCase( boolean ignoreCase ) { this.ignoreCase = ignoreCase; } //------------------------------------------------------------------ public void setTitle( int index, String title ) { elements.get( index ).tab.setTitle( title ); } //------------------------------------------------------------------ public void setToolTipText( int index, String text ) { elements.get( index ).tab.setToolTipText( text ); } //------------------------------------------------------------------ public void setComponent( int index, Component component ) { elements.get( index ).component = component; if ( index == selectedIndex ) updateComponents( ); } //------------------------------------------------------------------ public void addComponent( String title, Action closeAction, Component component ) { // Destroy any selection list destroyKeySelectionList( ); destroyMouseSelectionList( ); // Add new component to lists elements.add( new Element( createTab( title, closeAction ), component ) ); int index = elements.size( ) - 1; recentElements.add( 0, elements.get( index ) ); // Select new component setSelectedIndex( index ); // Request focus for new component component.requestFocusInWindow( ); } //------------------------------------------------------------------ public void removeComponent( int index ) throws IndexOutOfBoundsException { // Validate index if ( (index < 0) || (index >= elements.size( )) ) throw new IndexOutOfBoundsException( ); // Destroy any selection list destroyKeySelectionList( ); destroyMouseSelectionList( ); // Remove component from lists recentElements.remove( elements.get( index ) ); elements.remove( index ); // Adjust selected index if ( selectedIndex < elements.size( ) ) { int i = selectedIndex; selectedIndex = -1; setSelectedIndex( i ); } else { if ( selectedIndex > 0 ) setSelectedIndex( selectedIndex - 1 ); else { selectedIndex = -1; fireStateChanged( ); } } // Adjust index of first tab int maxStartIndex = getMaximumStartIndex( ); if ( startIndex > maxStartIndex ) startIndex = maxStartIndex; // Update buttons and components updateButtons( ); updateComponents( ); } //------------------------------------------------------------------ public void setSelectedIndex( int index ) throws IndexOutOfBoundsException { // Validate index if ( (index < 0) || (index >= elements.size( )) ) throw new IndexOutOfBoundsException( );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -