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

📄 splitterbar.java

📁 Java program shows how to use tab control for jumping between multiple layouts.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					} // for each component before us

				// did we push to far?
				if (currBounds.x <= insets.left) {
					int delta = currBounds.x - insets.left;
					// hide all components before that one
					for(int temp=curr-1; temp > -1; temp--)
						comps[temp].setVisible(false);
					// push all splitter bars into view
					done = false;
					for(int temp=curr;!done && temp <= origCurr; temp++)
						if (comps[temp] instanceof SplitterBar) {
							Point p = comps[temp].getLocation();
							p.x -= delta;
							comps[temp].setLocation(p);
							}
						else done = comps[temp].isVisible();
					} // pushed highest component off top edge

				// next, check if we exposed components below us
				curr = origCurr;
				// if the next component is not visible, show all between us & next
				//    splitter bar or bottom edge
				for(int temp=curr+1; temp<comps.length && !comps[temp].isVisible(); temp++)
					comps[temp].setVisible(true);
				} // HORIZONTAL -- moved left

			else if (currBounds.x > originalBounds.x) { // moved right
				// could have moved _into_ splitter bars to right (or right edge)
				//   and/or away from splitter bars to left (or left edge)

				// check to see it we've bumped into a splitter to our right us.
				boolean done=false;
				for (int temp=curr+1; !done && temp < comps.length; temp++) {
					if (comps[temp] instanceof SplitterBar) {
						Rectangle r = comps[temp].getBounds();
						if (currBounds.x + currBounds.width >= r.x) { // touching or to right...
							comps[temp].setLocation(currBounds.x + currBounds.width, r.y);
							// any comps in between should be hidden
							for(int c=curr+1; c < temp; c++)
								comps[c].setVisible(false);
							curr       = temp;
							currBounds = comps[temp].getBounds();
							} // touching or above
						else done=true; // no more compression
						} // it's a splitter bar
					} // for each component before us

				// did we push to far?
				if ((currBounds.x + currBounds.width) >= (parentBounds.width-insets.right)) {
					int delta = currBounds.x + currBounds.width - (parentBounds.width-insets.right);
					// hide all components before that one
					for(int temp=curr+1; temp < comps.length; temp++)
						comps[temp].setVisible(false);
					// push all splitter bars into view
					done = false;
					for(int temp=curr;!done && temp >= origCurr; temp--)
						if (comps[temp] instanceof SplitterBar) {
							Point p = comps[temp].getLocation();
							p.x -= delta;
							comps[temp].setLocation(p);
							}
						else done = comps[temp].isVisible();
					} // pushed highest component off top edge

				// next, check if we exposed components below us
				curr = origCurr;
				// if the next component is not visible, show all between us & next
				//    splitter bar or bottom edge
				for(int temp=curr-1; temp>-1 && !comps[temp].isVisible(); temp--)
					comps[temp].setVisible(true);
				} // HORIZONTAL -- moved right
			} // orientation==HORIZONTAL

		} // checkComponents()
	public int getOrientation() {return orientation;}
	void mouseDrag(MouseEvent e) {
		if (SplitterLayout.dragee == null)
			SplitterLayout.dragee = this;
		else if (SplitterLayout.dragee != this)
			return;
		Component c = getParent();
		Point fl = c.getLocationOnScreen();
		while(c.getParent() != null)
			c = c.getParent();
		if (!alreadyDrawn) {
			originalBounds = getBounds();
			wBar = new Window((Frame)c);
			wBar.setBackground(getBackground().darker());
			}
		Container cp = getParent();
		Dimension parentDim = cp.getSize();
		Point l = getLocationOnScreen();
		Insets insets = ((Container)cp).getInsets();
		if (orientation == SplitterLayout.VERTICAL)
			parentDim.width -= insets.right + insets.left;
		else
			parentDim.height -= insets.top + insets.bottom;
		Rectangle r = getBounds(); // mouse event is relative to this...
		int x = l.x+(orientation==SplitterLayout.HORIZONTAL?e.getX():0);
		int y = l.y+(orientation==SplitterLayout.VERTICAL?e.getY():0);
		if (x<fl.x+insets.left) x = fl.x+insets.left;
		else if ((orientation==SplitterLayout.HORIZONTAL) && (x > fl.x+parentDim.width-r.width))
			x = fl.x+parentDim.width-r.width;
		if (y<fl.y+insets.top) y = fl.y+insets.top;
		else if ((orientation==SplitterLayout.VERTICAL) && (y > fl.y+parentDim.height-r.height))
			y = fl.y+parentDim.height-r.height;
		wBar.setBounds(x,y,
					 	(orientation==SplitterLayout.HORIZONTAL)?r.width:parentDim.width,
					 	(orientation==SplitterLayout.VERTICAL)?r.height:parentDim.height);
		if (!alreadyDrawn) {
			wBar.setVisible(true);
			alreadyDrawn=true;
			}
		}
	void mouseEnter(MouseEvent e) {
		if (SplitterLayout.dragee != null) return;
		setCursor((orientation == SplitterLayout.VERTICAL)?VERT_CURSOR:HORIZ_CURSOR);
		mouseInside = true;
		invalidate();
		validate();
		repaint();
		}
	void mouseExit(MouseEvent e) {
		if (SplitterLayout.dragee != null) return;
		setCursor(DEF_CURSOR);
		mouseInside = false;
		invalidate();
		validate();
		repaint();
	   	}   
	void mouseRelease(MouseEvent e) {
		if (alreadyDrawn) {
			if (SplitterLayout.dragee != this) return;
			SplitterLayout.dragee = null;
			wBar.setVisible(false); wBar.dispose(); wBar=null; alreadyDrawn=false;
			Rectangle r = getBounds(); // mouse event is relative to this...
			r.x += (orientation==SplitterLayout.HORIZONTAL?e.getX():0);
			r.y += (orientation==SplitterLayout.VERTICAL?e.getY():0);
			setLocation(r.x, r.y);
			setCursor(DEF_CURSOR);

			// check to see if we need to move other splitters and hide other
			// components that are controlled by the layout
			// First -- find what component this one is

			checkOtherComponents();
			mouseInside = false;
			invalidate();
			getParent().validate();
			SplitterLayout.dragee = null;
			}
		}
	/** Paints the image of a SplitterBar.  If nothing was added to
		the SplitterBar, this image will only be a thin, 3D raised line that
		will act like a handle for moving the SplitterBar.
		If other components were added the SplitterBar, the thin 3D raised
		line will onlty appear where SplitterSpace components were added.
	*/
	public void paint (Graphics g) {
		if (mouseInside)
			g.setColor(Color.yellow);
		else
			g.setColor(Colors.lightSkyBlue3);
		Component c[] = getComponents();
		if (c != null && c.length > 0)
	    	for(int i = 0; i <c.length;i++) {
				if (c[i] instanceof SplitterSpace) {
					// only draw boxes where SplitterSpace components appear
					Rectangle r = c[i].getBounds();
					if (orientation == SplitterLayout.VERTICAL)
						g.fill3DRect(r.x+2,r.y+r.height/2-1,r.width-5,3,true);
					else
						g.fill3DRect(r.x+r.width/2-1,r.y+2,3,r.y+r.height-5,true);
					}
				}
		else {
			Rectangle r = getBounds();
			if (orientation == SplitterLayout.VERTICAL)
				g.fill3DRect(2,r.height/2-1,r.width-5,3,true);
			else
				g.fill3DRect(r.width/2-1,2,3,r.height-5,true);
			}
		}
	void setOrientation(int o) {orientation = o;}
	public void swapOrientation() {
		setOrientation(getOrientation()==SplitterLayout.HORIZONTAL?SplitterLayout.VERTICAL:SplitterLayout.HORIZONTAL);
	}
	/** Called by AWT to update the image produced by the SplitterBar */
	public void update (Graphics g)  {paint(g);}
}

⌨️ 快捷键说明

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