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

📄 tabpanel.java

📁 Java program shows how to use tab control for jumping between multiple layouts.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		super.remove(index);
		
		if (selected >= index && selected > 0)
			selected--;
			
		if (selected > -1 && getComponentCount() > 0)
			showPhysicalTab(selected);
		invalidate();
		validate();
		repaint();
	}
	
	/** Remove a TabSelectionListener.
	 *  @param newListener com.magelang.tabsplitter.TabSelectionListener
	 *  @see #addTabSelectionListener
	 */
	public void removeTabSelectionListener(TabSelectionListener newListener) {
		if (aTabSelectionListener != null) {
			aTabSelectionListener.removeElement(newListener);
		};
	}
	
	/**
	 * Check to see if the user clicked on a tab or one of the
	 * tab panel navigation buttons.
	 * @param e (java.awt.event.MouseEvent) The mouse click.
	 */
	protected void selectTab(MouseEvent e) {
		// walk through the tab polygons to see if we have a hit...
		// first check if it's the "plus" or "minus"
		int count = getComponentCount();
		if (count > 0 && bothRect.contains(e.getX(), e.getY())) {
			if (e.getX() - bothRect.x > e.getY() - bothRect.y) // plus is upper right corner
				next();
			else // minus is the rest of the box
				previous();
		}	

		// if the user clicked on the right arrow, shift the tabs to the right
		else if (rightEnabled && tooManyTabs && rightArrow.contains(e.getX(), e.getY()))
			shiftRight();
	
		// if the user clicked on the left arrow, shift the tabs to the left
		else if (leftEnabled && tooManyTabs && leftArrow.contains(e.getX(), e.getY()))
			shiftLeft();

		// now walk through the rest of the tabs and see if the user clicked in one of them
		// the extra check is to filter out tests for the currently-selected tab
		else
			// for each tab starting at the first visible tab, moving right
			if (count > 0)
				for(int i = ((selected>=firstVisible)?firstVisible-1:firstVisible); i < count; i++) {
					int n = (i==firstVisible-1?selected:i);
					if (tabContains(n-firstVisible, e.getX(), e.getY())) {
						// select the target tab
						mergeOrShow(n);
						break;
					}	
				}	
	}
	
	/**
	 * Sets 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. 
	 * @param borderColor The new value for the property.
	 * @see #getBorderColor
	 */
	public void setBorderColor(Color borderColor) {
		this.borderColor = borderColor;
		invalidate();
	}
	
	protected void setExplicitTabText(Component c, String text) {
		explicitTabText.put(c,text);
	}	
	/** Explicitly sets which tab is the first to be visible 
	 *  @param value (int) the number for the first tab
	 */
	public void setFirstVisible(int value) {
		if (value >= getComponentCount())
			throw new IllegalArgumentException("Not that many tabs");
		firstVisible = value;
	}	
	
	/** set the font to use on the tabs
	 *  @param f (Font) -- the font to use when writing tab text
	 */
	public void setFont(Font f) {
		super.setFont(f);
		invalidate();
		// the following two lines are a hack to get the box to redraw properly
		// I'll figure out a better way sometime...
		((CardLayout)getLayout()).next(this);
		((CardLayout)getLayout()).previous(this);
		repaint();
	}
	
	/** Explicitly pick the selected tab by number.  Note that the number refers to 
	 *  the current tabs visible on the screen 
	 *  @param num int -- the tab number to select
	 *  @see #getSelectedTabNum
	 */
	public void setSelectedTabNum(int num) {
		if (num > (getComponentCount()-1))
			throw new IllegalArgumentException("Tab number greater than number of components");
		showPhysicalTab(num);
	}	
	
	/** set the color to use behind the tabs
	 *  @param color (Color) the color to draw behind the tabs
	 */
	public void setTabBackground(Color color) {
		tabBackground = color;	
		invalidate();
	}	
	
	/** Set the color to use when drawing the tabs 
	 *  @deprecated the new tabColors property should be used instead.
	 *  @see #setTabColors
	 */
	public void setTabColor(Color color) {
		setTabColors(0,color);
	}	
	
	/** Sets the colors to use when drawing the tabs.  This an an array of
	 *  colors that will be cycled through, ie, if there are more tabs than colors,
	 *  the next tab will reuse the first color.
	 *  @param tabColors The new value for the property.
	 *  @see #getTabColors
	 */
	public void setTabColors(Color[] tabColors) {
		if (tabColors == null)
			tabColors = new Color[] {null};
		this.tabColors = tabColors;
		invalidate();
		if (java.beans.Beans.isDesignTime())
			repaint();
	}
	
	/** Sets the colors to use when drawing the tabs.  This an an array of
	 *  colors that will be cycled through, ie, if there are more tabs than colors,
	 *  the next tab will reuse the first color.
	 *  @param tabColors The new value for the property.
	 *  @see #getTabColors
	 */
	public void setTabColors(int index, java.awt.Color tabColor) {
		if (index > (tabColors.length-1)) {
			Color newColors[] = new Color[index+1];
			System.arraycopy(tabColors, 0, newColors, 0, tabColors.length);
			for(int i = tabColors.length; i < index; i++)
				newColors[i] = Color.lightGray;
			tabColors = newColors;
		}	
		tabColors[index] = tabColor;
		invalidate();
		if (java.beans.Beans.isDesignTime())
			repaint();
	}
	
	/** Sets the explicit tab text to use when drawing the tabs.
	 *  @param tabText The new value for the property.
	 *  @see #getTabText
	 *  @see #determineTabText
	 */
	public void setTabText(String[] tabText) {
		this.tabText = tabText;
		return;
	}
	
	/** Sets the explicit tab text to use when drawing the tabs.
	 *  @param index (int) the specific text to set
	 *  @param tabText The new value for the property.
	 *  @see #getTabText
	 *  @see #determineTabText
	 */
	public void setTabText(int index, String tabText) {
		if (this.tabText == null) // give a little extra room...
			this.tabText = new String[index+10];
		else if(index >= this.tabText.length) {
			// reallocate
			String newTabText[] = new String[index + 10];
			System.arraycopy(this.tabText, 0, newTabText, 0, this.tabText.length);
			this.tabText = newTabText;
		}	
		this.tabText[index] = tabText;
	}
	
	/** Creates a Polygon object for each tab.  This polygon object
	 *  is used to draw the tab and to test if the user has clicked
	 *  the mouse within that tab.   The navigation buttons are also
	 *  set up in this method.
	 * @param g (java.awt.Graphics) The graphics context into which the tabs will be drawn
	 */
	protected void setupTabPolygons() {
		Dimension dim = getSize();
		int x[] = new int[6];
		int y[] = new int[12];

		bothRect  = new Rectangle(dim.width-26, 2, 24, 6+h);
		x[0] = 2;  x[1] = 11; x[2] = 11;
		y[0] = h/2+4; y[1] = h/2;  y[2] = h/2+8;
		leftArrow = new Polygon(x,y, 3);
		x[0] = bothRect.x - x[0];
		x[1] = bothRect.x - x[1];
		x[2] = bothRect.x - x[2];
		rightArrow = new Polygon(x,y, 3);
	
		int i;
		x[0]=0;   x[1]=3;   x[2]=4;   x[3]=9; x[4]=11; x[5]=14;
		y[0]=8+h; y[1]=6+h; y[2]=5+h; y[3]=5; y[4]=4;  y[5]=2;

		for(i=0; i<6; i++) y[11-i] = y[i];

		int x1[] = new int[12];

		String tabText[] = determineTabText();
				
		int tempXOff = 8;
		Component comp[] = getComponents();
		int compCount = getComponentCount();

		lastVisible = compCount-1; // assume they all fit...
		tooManyTabs = (firstVisible != 0);

		tabs = new Vector();
		for(int num=firstVisible; num<compCount; num++) {
			String text = tabText[num];		
			int textWidth = (num==selected)?
			                    boldfm.stringWidth(text) :
			                    fm.stringWidth(text);
			
			for(i=0; i<6; i++)
				x1[i] = x[i] + tempXOff;
			
			tempXOff += ((x[5]-x[0])*2) + (hslop*2) + textWidth;
		
			for(i=0; i<6; i++)
				x1[11-i] = tempXOff - x[i];
			
			tempXOff -= x[5];
		
			Polygon p = new Polygon(x1,y,12);
			tabs.addElement(p);

			// if the tab extends past the last place we can draw
			//   set tooManyTabs
			if (x1[11] > bothRect.x) {
				rightEnabled = true;
				tooManyTabs = true;
				if (num > firstVisible)
					lastVisible = num-1;
				else
					lastVisible = num;
			}	
		}
	}
	
	/** Shifts the row of tabs one position to the left
	 */
	public void shiftLeft() {
		if (firstVisible > 0) firstVisible--;
		repaint();
	}	
	
	/** Shifts the row of tabs one position to the right
	 */
	public void shiftRight() {
		if (firstVisible < (getComponentCount()-1)) firstVisible++;
		repaint();
	}
	
	/** Select a tab by number
	 *  @param n (int) the number of the tab to select
	 */
	public void show(int n) {
		showPhysicalTab(n);
	}
	
	/** Select a tab by component
	 *  @param comp (Component) the main component of the tab to select
	 */
	public void show(Component comp) {
		Component c[] = getComponents();
		int i=0;
		while(c[i] != comp) i++;
		
		if (i < getComponentCount())
			showPhysicalTab(i);
	}	
	
	/** Select a tab by text
	 *  @param tabName (String) the text of the tab to be selected
	 */
	public void show(String tabName) {
		String tabText[] = determineTabText();
		for(int i=0; i<tabText.length; i++) {
			if (tabText[i].equals(tabName)) {
				showPhysicalTab(i);
				return;
			}	
		}
		throw new IllegalArgumentException("No tab found with text \""+tabName+"\"");
	}	
	
	/** Select a tab by number
	 *  @param n (int) the number of the tab to select
	 */
	public void showPhysicalTab(int n) {
		if (n >= getComponentCount())
			throw new IllegalArgumentException("Not that many components!");
		selected = n;
		determineVisible();
		if (selected < firstVisible || selected > lastVisible)
			firstVisible = selected;
		((CardLayout)getLayout()).show(this, getComponent(n).getName());
		fireTabSelected(new TabSelectionEvent(this, getVisibleComponent(), 
		                                      selected, getSelectedName(), getVisibleComponentNum()));
		invalidate();
		validate();
		repaint();
	}
	
	/** bring up the popup menu for the tabsplitter
	 *  @param e (MouseEvent) tells where to bring it up
	 */
	protected void showPopup(MouseEvent e) {
		if (e.isMetaDown()) { // check right click
			if (tabMenuItems != null)
				for(int i = tabMenuItems.length-1;i>-1;i--)
					popupMenu.remove(tabMenuItems[i]);
			if ((tabMenuItems == null) || (tabMenuItems.length != getComponentCount()))
				tabMenuItems = new MenuItem[getComponentCount()];

			// add tab Strings to the popup menu
			String tabNames[] = determineTabText();
			for(int i = 0; i < tabNames.length; i++) {
				popupMenu.add(tabMenuItems[i] = new MenuItem(tabNames[i]));
				tabMenuItems[i].addActionListener(this);
			}	
			popupMenu.show(this, e.getX(), e.getY());
		}	
	}
	
	/** Check to see if the numbered tab contains an x,y location.  Used
	 *  to determine if the mouse was clicked in a given tab 
	 *  @param num (int) number of the tab to check
	 *  @param x (int) x-coord to check
	 *  @param y (int) y-coord to check
	 *  @return true if the (x,y) point is in the tab; false otherwise
	 */
	protected boolean tabContains(int num, int x, int y) {
		return ((Polygon)tabs.elementAt(num)).contains(x, y);
	}	
	
	/** Overridden to get rid of screen erase between drawings */
	public void update(Graphics g) {
		paint(g);
	}
	
}

⌨️ 快捷键说明

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