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

📄 tabsplitter.java

📁 Java program shows how to use tab control for jumping between multiple layouts.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				((SplitterPanel)t).add((SplitterPanel)s);
			else 
				((SplitterPanel)t).add(names[source], s, spos, sText);
			position.put(t, new Integer(tpos));
			add(t, t.getName(), tpos);
		}
		else if (s instanceof SplitterPanel) {
			((SplitterPanel)s).add(names[target], t, tpos, tText);			
			position.put(s, new Integer(tpos));
			add(s, s.getName(), tpos);
		}	
	
		else {	
			// Neither are splitters
			// Create a new splitter panel
			// Create two new TabLabelPanels
			// Add the target/source to the TabLabelPanels
			// Add the TabLabelPanels to the splitter panel w/ a splitterbar
			SplitterPanel p = new SplitterPanel();
			p.add(names[target], t, tpos, tText);
			p.add(names[source], s, spos, sText);
			position.put(p, new Integer(tpos));
			add(p, p.getName(), tpos);
		}	
	
		// Make sure we get repainted/layed out
		showPhysicalTab(target>source?target-1:target);
		s.setVisible(true);
		t.setVisible(true);
	}
	
	public void mouseDragged(MouseEvent e) {
		// if the mouse is dragged over the left or right arrows, shift!
		if (leftEnabled && leftArrow.contains(e.getX(), e.getY()))
			shiftLeft();
		else if (rightEnabled && rightArrow.contains(e.getX(), e.getY()))
			shiftRight();
	}
	/** Handle the mouse exiting the TabSplitter */
	public void mouseExited(java.awt.event.MouseEvent e) {
		// user moved mouse out of the tab panel -- abort a drag
		tabBeingDragged = -1;
		setCursor(DEF_CURSOR);
		super.mouseExited(e);
	}
	
	public void mouseMoved(MouseEvent e) {
	}	
	/** Handle the mouse being pressed on a TabSplitter */
	public void mousePressed(java.awt.event.MouseEvent e) {
		int selected = getSelectedTabNum();
		int firstVisible = getFirstVisible();
		
		if (!e.isMetaDown()) { // we deal with left click only...
			if ((selected > firstVisible) && tabContains(selected-firstVisible, e.getX(), e.getY()))
				tabBeingDragged = selected;
			else {
				for(int i=firstVisible; i < getComponentCount(); i++)
					if (tabContains(i-firstVisible, e.getX(), e.getY())) {
						tabBeingDragged = i;
						break;
						}
				}	
			if (tabBeingDragged != -1) {
				setCursor(HAND_CURSOR);
			}	
		}	
	
		super.mousePressed(e);
	}
	
	/** Handle the mouse being released on a tabsplitter */
	public void mouseReleased(java.awt.event.MouseEvent e) {
		setCursor(DEF_CURSOR);
		super.mouseReleased(e);
	}
	
	/** Remove a component from the container.
	 *  Need to determine a good way to do this!
	 *  This method is overridden to force a repaint in design mode.
	 * @param index (int) Which component to remove.
	 */
	public void remove(int index) {
		// find the component in our position list
		Component targetComponent = null;
		Enumeration e = position.keys();
		while(e.hasMoreElements()) {
			Object o = e.nextElement();
			if ((!(o instanceof SplitterPanel)) && getPosition(o) == index) {
				removeBody((Component)o);
				return;
			}	
		}	
		
		throw new IllegalArgumentException("tab position not found");
	}
	
	public void remove(Component comp) {
		Integer oldI = (Integer)position.get(comp);
		if (oldI == null)
			throw new IllegalArgumentException("tab not found");
		removeBody(comp);
	}	
	
	/** Remove all components from the container.
	 *  This method is overridden to force a repaint in design mode.
	 */
	public void removeAll() {
		super.removeAll();
		position = new Hashtable();
		tabBeingDragged = -1;
	}
	
	/** Support code for remove()s */
	private void removeBody(Component c) {
		tabBeingDragged = -1;
		boolean found=false;
		boolean special = false;
				
		// check if the component is in a splitter or is directly a component
		int count = getComponentCount();
		Component comp[] = getComponents();
		for(int i = 0; !found && i < count; i++)
			found = (comp[i] == c);

		for(int i = 0; !found && i < count; i++) {
			if (comp[i] instanceof SplitterPanel &&
				 ((SplitterPanel)comp[i]).contains(c)) {
				 // separate c from the splitter panel
				 ((SplitterPanel)comp[i]).separate(c);
				 comp = getComponents();
				 count = getComponentCount();
				 compsInSplitters.remove(c); //hack
				 found = special = true;
			} 
		}	

		if (found) {
			// adjust/remove the positions...
			if ((!(c instanceof SplitterPanel)) && 
			    compsInSplitters.get(c) == null) { // if NOT in a splitter
				int posIndex = getPosition(c);
				position.remove(c);
				for(int i=0;i<count;i++)
					if (comp[i] instanceof SplitterPanel)
						((SplitterPanel)comp[i]).decrPositions(posIndex);
				
				Enumeration e = position.keys();
				while(e.hasMoreElements()) {
					Object o = e.nextElement();
					int p = getPosition(o);
					if (p > posIndex)
						position.put(o, new Integer(p-1));
				}	
			}

			// figure out where the component really is...
			for(int index=0; index<count; index++)
				if (comp[index] == c)
					super.remove(index);
		}
		
		if (special)
			showPhysicalTab(getSelectedTabNum());
	}	
	
	/** separateTabs -- split the selected tab out from its parent Splitterlayout panel */
	public void separateTabs(String name1, Component comp1, String exp1, 
	                         String name2, Component comp2, String exp2,
	                         SplitterPanel p) {
		int ppos = 0;
		Component comps[] = getComponents();
		int count = getComponentCount();
		while(ppos < count && comps[ppos] != p) ppos++;

		remove(p);
		
		// if keeping the splitter, re-add it to the CardLayout.  This is necessary to
		//   update the card name. grr.
		if (comp2 == null)
			add(p, p.getTabName(), ppos);
		else
			position.remove(p);
			
		// re-add the components to the TabSplitter
		if (exp1 != null)
			setExplicitTabText(comp1, exp1);
		findWhereToAdd(name1, comp1);
		compsInSplitters.remove(comp1);
		if (comp2 != null) {
			if (exp2 != null)
				setExplicitTabText(comp2, exp2);
			findWhereToAdd(name2, comp2);
			compsInSplitters.remove(comp2);
		}	
		
		show(comp1);
		
		invalidate();
		validate();
		repaint();
	}	
	/** Set the font to use when writing the tab text */
	public void setFont(Font f) {
		super.setFont(f);
		invalidate();
		validate();
		// 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();
	}
	
	/** Show the nth tab (starting at 0)
	 *  The number refers to the tab added in the nth position to the 
	 *  tab splitter, not the actual nth tab on the panel.  This method
	 *  will find where component number n is located and display it,
	 *  even if it is in a SplitterPanel
	 */
	public void show(int n) {
		// find the component that was in that position
		Component comp[] = getComponents();
		int count = getComponentCount();
		for(int i = 0; i<count; i++) {
			Integer pos = (Integer)position.get(comp[i]);
			if (pos != null && pos.intValue()==n) {
				show(comp[n]);
				return;
			}	
		}
		throw new IllegalArgumentException("Tab not found");
	}
	
	/** Show a component.
	* This method will find where the component is located and display it,
	 *  even if it is in a SplitterPanel
	 */
	public void show(Component comp) {
		// find the component -- note that it could be in
		//   a SplitterPanel...
		
		// simple find first...
		try {
			super.show(comp);
		}
		catch (IllegalArgumentException e) {
			Component comps[] = getComponents();
			int count = getComponentCount();
			for(int i = 0; i< count; i++) {
				if (comps[i] instanceof SplitterPanel) {
					if (((SplitterPanel)comps[i]).contains(comp)) {
						showPhysicalTab(i);
						return;
					}
				}	
			}	
			throw new IllegalArgumentException("Tab not found");
		}	
	}
	
	/** Show a component based on tab name
	 *  This method will find where the component with the specified text 
	 *  is located and display it, even if it is in a SplitterPanel
	 */
	public void show(String tabName) {
		try {
			super.show(tabName);
		}
		catch (IllegalArgumentException e) {
			// find the component -- note that it could be in a SplitterPanel.
			Component comp[] = getComponents();
			int count = getComponentCount();
			for(int i = 0; i< count; i++) {
				if (comp[i] instanceof SplitterPanel) {
					if (((SplitterPanel)comp[i]).contains(tabName)) {
						showPhysicalTab(i);
						return;
					}
				}	
			}	
			throw new IllegalArgumentException("No tab found with text \""+tabName+"\"");
		}	
	}	
	
	/** Change the orientation (horizontal/vertical) of all SplitterPanels */
	public void swapOrientation() {
		Component comps[] = getComponents();
		for(int i = getComponentCount()-1; i>-1; i--)
			if (comps[i] instanceof Container &&
				 ((Container)comps[i]).getLayout() instanceof SplitterLayout) {
				 Container cont = (Container)comps[i];
				((SplitterLayout)cont.getLayout()).swapOrientation(cont);
				}	
	}	
	
}

⌨️ 快捷键说明

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