⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabpanel.java

📁 Java program shows how to use tab control for jumping between multiple layouts.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			g.drawLine(x[7],y[7],x[8],y[8]);
			if (tabNum == selected) {
				g.drawLine(x[0],y[0],2,y[0]);
				Dimension d = getSize();
				g.drawLine(x[11]-((tabNum==selected)?1:0),y[0],d.width-4,y[0]);
				g.setColor(tabColor);
			}
	
			// write the text for the tab
			g.setColor(getForeground());
			String text;
			text = tabText[tabNum];
			g.drawString(text, x[5]+hslop, y[0] - (fm.getDescent() + fm.getLeading()/2));
			
			// if the tab extends past the last place we can draw
			//   set tooManyTabs
//			if (x[11] > bothRect.x) {
//				rightEnabled = true;
//				tooManyTabs = true;
//				if (tabNum > firstVisible)
//					lastVisible = tabNum-1;
//				else
//					lastVisible = tabNum;
//			}	
		}
		g.setFont(currentFont);

		if (tooManyTabs) {
			// blank out area used for right arrow
			g.setColor(getBorderColor());
			g.fillRect(bothRect.x-13, 0, dim.width-bothRect.x+13, h+8);
		
			// draw arrows
			g.setColor(getTabBackground());
			leftEnabled = (firstVisible > 0);
			if (leftEnabled)
				g.fillPolygon(leftArrow);
			if (rightEnabled)
				g.fillPolygon(rightArrow);
			g.setColor(getTabBackground().brighter());
			if (leftEnabled)
				g.drawPolygon(leftArrow);
			if (rightEnabled)
				g.drawPolygon(rightArrow);
		}	
		else {
			// blank out area used for right arrow
			g.setColor(getBorderColor());
			g.fillRect(bothRect.x-2, 0, dim.width-bothRect.x+2, h+8);
		}	
	
		// draw the +/- for moving through tabs
		g.setColor(getTabBackground());
		g.fill3DRect(bothRect.x, bothRect.y, bothRect.width-1, bothRect.height-1, true);
		
		g.setColor(getTabBackground().brighter());
		g.drawLine(bothRect.x + 3, bothRect.y + 3, bothRect.x + bothRect.width-6, bothRect.y + bothRect.width-6);
		
		g.setColor(getForeground());
		int w2  = bothRect.width/2;
		int w4  = bothRect.width/4;
		int w8  = bothRect.width/8;
		
		g.drawLine(bothRect.x + w2,    bothRect.y + w4,
				 bothRect.x + w2+w4, bothRect.y + w4);
		g.drawLine(bothRect.x + w2+w8, bothRect.y + w8,
		           bothRect.x + w2+w8, bothRect.y + w4+w8);
		g.drawLine(bothRect.x + w8,    bothRect.y + w2+w8,
		           bothRect.x + w4+w8, bothRect.y + w2+w8);
	}

	/** let those who care know when a tab was selected 
	 *  @param e (TabSelectionEvent) -- the event to pass to the listeners
	 *  @see #addTabSelectionListener
	 *  @see #removeTabSelectionListener
	 */
	protected void fireTabSelected(TabSelectionEvent e) {
		if (aTabSelectionListener == null) return;  // noone cares, so get out

		// walk through the list and call the tabSelected methods
		int currentSize = aTabSelectionListener.size();
		for (int index = 0; index < currentSize; index++){
			TabSelectionListener l =
				(TabSelectionListener)aTabSelectionListener.elementAt(index);
			if (l != null)
				l.tabSelected(e);
		};
	}

	/**
	 * Gets the borderColor property (java.awt.Color) value.
	 * The borderColor is the color to paint behind the tabs and around
	 *   the edge of the panel display area.
	 * @return The borderColor property value.
	 * @see #setBorderColor
	 */
	public Color getBorderColor() {
		/* Returns the borderColor property value. */
		if (borderColor == null)  // use gray as the default
			borderColor = Color.gray;
		return borderColor;
	}

	protected String getExplicitTabText(Component c) {
		return (String)explicitTabText.get(c);
	}	
	/** Get the number of the first-visible physical tab.
	 *  @return The number of the tab that is visible.
	 *  @see #setFirstVisible
	 */
	public int getFirstVisible() {
		return firstVisible;
	}	

	/** Returns the insets of the TabPanel. The insets indicate the size of
	 *  the border of the container.
	 *  @see java.awt.LayoutManager
	 */
	public Insets getInsets() {
		FontMetrics fm = getFontMetrics(getFont());
		return new Insets(fm.getHeight()+12,6,6,6);
	}

	/* Gets the popup menu.  This is provided so the subclass TabSplitter
	 *   can add its own menu items to the popup menu.
	 * @return The popup menu for the tab panel
	 */
	protected PopupMenu getPopupMenu() {
		return popupMenu;
	}		

	/** Return the position of the component in the tabsplitter
	 *  (its physical tab number)
	 *  @param comp (Component) -- the component to search for.
	 *  @return The position of the component (-1 means not found)
	 */
	protected int getPosition(Object comp) {
		int count = getComponentCount();
		Component comps[] = getComponents();
		int i = 0;
		while(i < count && comps[i] != comp) i++;
		if (i == count) i = -1;
		return i;
	}	

	/** Get the tab text of the currently-selected component 
	 *  @return A String containing the currently-selected tab's text
	 */
	public String getSelectedName() {
		return determineTabText()[selected];
	}	

	/** Get the number of the currently-selected tab 
	 *  @return an int for the currently-selected tab
	 *  @see #setSelectedTabNum
	 */
	public int getSelectedTabNum() {
		return selected;
	}	

	/** Gets the color that's behind the tabs
	 *  @return The color behind the tabs
	 *  @see #setTabBackground
	 */
	public Color getTabBackground() {
		if (tabBackground == null)
			tabBackground = Color.lightGray;
		return tabBackground;
	}	

	/** Get the color to use when drawing the tabs 
	 *  @deprecated the new tabColors property should be used instead.
	 *  @see #getTabColors
	 */
	public Color getTabColor() {
		return getTabColors(0);
	}	
	
	/** Get the colors used to draw the tabs.  These colors are cycled through all
	 *  tabs that are painted.
	 *  @return The tabColors property value.
	 *  @see #setTabColors
	 */
	public Color[] getTabColors() {
		// lazy instantiation of the tab colors property
		if (tabColors == null)
			return new Color[] {null};
		return tabColors;
	}

	/** get a specific tab color.
	 * @return The tabColor property value.
	 * @param index The index value into the property array.
	 * @see #getTabColors
	 * @see #setTabColors
	 */
	public Color getTabColors(int index) {
		/* Returns the tabColors index property value. */
		if (tabColors == null)
			return null;
	
		return tabColors[index % tabColors.length];
	}

	/** Gets the explicitly-set tab text array.
	 *  Note that this method should not be used to see the text
	 *    displayed on the tabs; instead, <tt>determineTabText()</tt> should be used.
	 *  @return The tabText property value.
	 *  @see #setTabText
	 *  @see #determineTabText
	 */
	public String[] getTabText() {
		/* Returns the tabText property value. */
		return tabText;
	}

	/** Gets a specific string from the explicitly-set tab text array.
	 *  Note that this method should not be used to see the text
	 *    displayed on the tabs; instead, <tt>determineTabText()</tt> should be used.
	 *  @return The tabText property value.
	 *  @see #setTabText
	 *  @see #determineTabText
	 */
	public String getTabText(int index) {
		/* Returns the tabText index property value. */
		return getTabText()[index];
	}

	/** Get the component that is currently visible
	 *  @return the component that is on the currently-selected tab (note that this
	 *  method returns an Object because its subclass returns an array of visible comps)
	 */
	public Object getVisibleComponent() {
		return getComponent(selected);
	}	

	/** Return an array containing the selected tab number.  (It's done this way
	 *  to provide a consistent interface for TabSplitter.)
	 *  If you are using TabPanel and just want the int, use getSelectedTabNum()
	 *  @return an array containing the selected tab number.
	 */
	public int[] getVisibleComponentNum() {
		return new int[] {selected};
	}	

	/** A convenience method so TabSplitter can add merge capability
	 *  @param n (int) -- the target tab or a merge or tab to show
	 */
	protected void mergeOrShow(int n) {
		showPhysicalTab(n);
	}	

	/** Dummy method for the MouseListener interface... */
	public void mouseClicked(MouseEvent e) {	}
	
	/** Dummy method for the MouseListener interface... */
	public void mouseEntered(MouseEvent e) {	}
	
	/** Dummy method for the MouseListener interface... */
	public void mouseExited(MouseEvent e) {	}
	
	/** handle the mouse button being pressed -- check for right mouse
	 *  click to bring up popup menu
	 */
	public void mousePressed(MouseEvent e) {
		if (((e.getSource() == this) && e.isMetaDown()) )
			showPopup(e);
	}
	
	/** When the mouse is released, check for tab selection
	 * @param e (MouseEvent)
	 */
	public void mouseReleased(MouseEvent e) {
		selectTab(e);
	}
	
	/** Move to next tab, wrapping around to first tab if necessary. */
	public void next() {
		selected = ++selected % getComponentCount();
		showPhysicalTab(selected);
	}
	
	/** Paints the tab panel image (tabs, border, background)
	 * @param g (Graphics) The graphics context into which the tab panel is painted
	 */
	public void paint(Graphics g) {
		if (currentFont != getFont()) {
			currentFont = getFont();
			boldFont = new Font(currentFont.getName(), Font.BOLD, currentFont.getSize());
			fm     = getFontMetrics(currentFont);
			boldfm = getFontMetrics(boldFont);
			h  = fm.getHeight();
		}	

		Dimension d = getSize();
		if ((d.width != lastW) || (d.height != lastH) || (image == null)) {
			// size has changed, must resize image
			image = createImage(d.width, d.height);
			if (g1 != null) g1.dispose();
			g1 = image.getGraphics();

			lastW = d.width;
			lastH = d.height;
			}

		// draw the borders
		g1.setColor(getBorderColor());
		g1.fillRect(0,0,d.width,d.height);
		g1.setColor(getBackground());
		g1.fill3DRect(2,h+8,d.width-4,d.height-h-10,true);
		g1.setColor(getTabBackground());
		g1.draw3DRect(4,h+10,d.width-8,d.height-h-14,false);

		drawTabs(g1);

		g.drawImage(image,0,0,this);
		super.paint(g);
	}
	
	/** Move to previous tab, wrapping around to last tab if necessary. */
	public void previous() {
		if (--selected < 0) selected = getComponentCount()-1;
		showPhysicalTab(selected);
	}
	
	/** Remove a component from the container.
	 * @param index (int) Which component to remove.
	 */
	public void remove(int index) {
		if (index >= getComponentCount())
			throw new IllegalArgumentException("Not that many tabs");
		removeBody(getComponent(index), index);
	}
	
	/** Remove a component from the container.
	 * @param comp (Component) Which component to remove.
	 */
	public void remove(Component comp) {
		// figure out which tab contains the text
		Component c[] = getComponents();
		int num;
		for(num = 0; num < c.length && c[num] != comp; num++);
		if (num < c.length)
			removeBody(comp, num);
	}
	
	/** Remove all components from the container.
	 *  This method is overridden to force a repaint in design mode.
	 */
	public void removeAll() {
		super.removeAll();
		explicitTabText = new Hashtable();
		invalidate();
		validate();
		repaint();
		selected = 0;
	}
	
	/** Remove a component from the container.
	 * @param index (int) Which component to remove.
	 */
	private void removeBody(Component c, int index) {
		explicitTabText.remove(c);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -