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

📄 dialog.java

📁 关于J4ME J2ME实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * @param spacing is the number of pixels that vertically separates
	 *  components.  Values less than 0 are be ignored.
	 */
	public void setSpacing (int spacing)
	{
		if ( (spacing >= 0) && (this.spacing != spacing) )
		{
			this.spacing = spacing;
			
			// Recalculate the layout with the new spacings later.
			clearLayout();
		}
	}
	
	/**
	 * Returns the width of the usuable portion of this form.  The usable
	 * portion excludes the vertical scrollbar on the right of the screen
	 * (if it exists).
	 * 
	 * @return The number of pixels wide the usable portion of the form is.
	 */
	public int getWidth ()
	{
		int canvasWidth = super.getWidth();
		int formWidth = canvasWidth - 2 * margin;
		
		if ( hasVerticalScrollbar() )
		{
			int scrollbarWidth = UIManager.getTheme().getVerticalScrollbarWidth();
			formWidth -= scrollbarWidth;
		}

		return formWidth;
	}
	
	/**
	 * Determines if the height of all the components is greater than the height
	 * of the screen.  If so a veritical scrollbar will be drawn on the form.
	 * 
	 * @return <code>true</code> if the form has a vertical scrollbar and <code>false</code>
	 *  if it does not.
	 */
	public synchronized boolean hasVerticalScrollbar ()
	{
		int screenHeight = getHeight();
		int formWidth = super.getWidth() - 2 * margin;
		
		boolean layoutJustCalculated = false;
		Theme theme = UIManager.getTheme();
		
		// Have we determined the layout?
		if ( absoluteHeights == null )
		{
			calculateLayout( theme, formWidth, screenHeight );
			layoutJustCalculated = true;
		}
		
		// Are all the components taller than the screen?
		if ( absoluteHeights[absoluteHeights.length - 1] > screenHeight )
		{
			if ( layoutJustCalculated )
			{
				// Recalculate the layout now that we're adding a vertical scrollbar.
				formWidth -= theme.getVerticalScrollbarWidth();
				calculateLayout( theme, formWidth, screenHeight );
			}
			
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Paints the vertical scrollbar.  The scrollbar must go on the right
	 * side of the form and span from the top to the bottom.  Its width
	 * is returned from this method and used to calculate the width of
	 * the remaining form area to draw components in.
	 *
	 * @param g is the <code>Graphics</code> object to paint with.
	 * @param x is the top-left X-coordinate pixel of the form area.
	 * @param y is the top-left Y-coordinate pixel of the form area.
	 * @param width is the width of the form area in pixels.
	 * @param height is the height of the form area in pixels.
	 * @param offset is the vertical scrolling position of the top pixel
	 *  to show on the form area.
	 * @param formHeight is the total height of all the components on the
	 *  form.  This is bigger than <code>height</code>.
	 */
	protected void paintVerticalScrollbar (Graphics g, int x, int y, int width, int height, int offset, int formHeight)
	{
		UIManager.getTheme().paintVerticalScrollbar( g, x, y, width, height, offset, formHeight );
	}
	
	/**
	 * Whenever the form is altered this method should be called to clear
	 * the layout.  The layout will be re-established during the next painting.
	 */
	private synchronized void clearLayout ()
	{
		componentWidths = null;
		absoluteHeights = null;
	}
	
	/**
	 * Goes through all of the components and determines their vertical
	 * positioning.  The form's overall height, the sum of all the
	 * components, is set in the <code>height</code> member variable.
	 * 
	 * @param theme is the application's current <code>Theme</code>.
	 * @param widht is the number of pixels wide the screen is.
	 * @param height is the number of pixels high the screen is.
	 */
	private synchronized void calculateLayout (Theme theme, int width, int height)
	{
		componentWidths = new int[components.size() + 1];  // Dummy element at bottom for end of components
		absoluteHeights = new int[componentWidths.length];
		int componentY = margin;  // First component is "margin" from the top
		
		// Scroll through the components determining the vertical position of each one.
		Enumeration list = components.elements();
		
		for ( int i = 0; list.hasMoreElements(); i++ )
		{
			absoluteHeights[i] = componentY;
			
			// Calculate the position of the next component.
			Component c = (Component)list.nextElement();
			
			int[] dimensions = c.getPreferredSize( theme, width, height );
			
			componentWidths[i] = dimensions[0];
			componentY += dimensions[1] + spacing;
			
			// Is this the first component that can be highlighted?
			if ( highlightedComponent < 0 )
			{
				if ( c.acceptsInput() )
				{
					highlightedComponent = i;
				}
			}
		}
		
		// Add a dummy last element to record the bottom of all components.
		absoluteHeights[absoluteHeights.length - 1] = componentY;
	}

	/**
	 * Forces the layout of all components to be recalculated.  This should
	 * be called whenever this screen or its components are altered.  For
	 * example changing a label may change its size and this method will
	 * account for that.
	 */
	public void invalidate ()
	{
		// Remove the layout of all components.
		clearLayout();
		
		// Recalculate the position of all components.
		hasVerticalScrollbar();
	}
	
	/**
	 * Moves the viewport of the form up or down and calls <code>repaint</code>.
	 * <p>
	 * The amount scrolled is dependent on the components shown.  Typically
	 * one scroll increment advances to the next component.  However, if
	 * components are taller than the screen they will be partially scrolled.
	 * 
	 * @param down is <code>true</code> when the form should scroll down and 
	 *  <code>false</code> when it should scroll up.
	 */
	private void scroll (boolean down)
	{
		// Get the dimensions of the form.
		int topOfForm = 0;
		int screenHeight = getHeight();
		int bottomOfForm = absoluteHeights[absoluteHeights.length - 1] - screenHeight;
		int bottomOfScreen = topOfScreen + screenHeight;

		// Get the dimensions of the current highlighted component.
		int current = (highlightedComponent >= 0 ? highlightedComponent : 0); 
		int currentTop = absoluteHeights[current];
		int currentBottom = absoluteHeights[current + 1];
		
		// Get the next component that can be highlighted.
		int nextHighlighted = highlightedComponent;
		
		if ( down )
		{
			int max = size();
			
			for ( int i = highlightedComponent + 1; i < max; i++ )
			{
				Component c = get( i );
				
				if ( c.acceptsInput() )
				{
					nextHighlighted = i;
					break;
				}
			}
		}
		else  // up
		{
			for ( int i = highlightedComponent - 1; i >= 0; i-- )
			{
				Component c = get( i );
				
				if ( c.acceptsInput() )
				{
					nextHighlighted = i;
					break;
				}
			}
		}
		
		// Scroll.
		if ( hasVerticalScrollbar() )
		{
			// Calculate the number of pixels to scroll the form.
			//   We scroll 90% of the screen unless there is another highlightable
			//   component within that 90%.  In which case we scroll only to the
			//   highlightable component, not the full 90%.
			int max = screenHeight * 9 / 10;  // 90% of the screen.
			int scroll;
			
			// What is the position of the next highlightable component?
			if ( (nextHighlighted < 0) || (nextHighlighted == highlightedComponent) )
			{
				// There are no highlightable components.  Always scroll the
				// maximum.
				scroll = max;
			}
			else
			{
				// Get the screen position of the next highlightable component.
				int nextTop = absoluteHeights[nextHighlighted];
				int nextBottom = absoluteHeights[nextHighlighted + 1];

				// Calculate how far to scroll to get to the next highlighted component.
				if ( down )
				{
					if ( currentTop < topOfScreen )
					{
						currentTop = topOfScreen;
					}
					
					scroll = nextTop - currentTop;
					
					// Don't scroll if the next highlighted component fits
					// completely on the screen already.
					if ( nextBottom < bottomOfScreen )
					{
						scroll = 0;
					}
				}
				else  // up
				{
					if ( currentBottom > bottomOfScreen )
					{
						currentBottom = bottomOfScreen;
					}
					
					scroll = currentBottom - nextBottom;
					
					// Don't scroll if the next highlighted component fits
					// completely on the screen already.
					if ( nextTop >= topOfScreen )
					{
						scroll = 0;
					}
				}
			}
			
			if ( scroll > max )
			{
				// The next highlightable component is too far down to scroll
				// to immediately.  Instead scroll the form part way.  The user
				// will click to scroll again if they want.
				scroll = max;
			}
			else
			{
				// Scroll to and highlight the next highlightable component.
				highlightedComponent = nextHighlighted;
			}
	
			// Set the position of the form.
			if ( down == false )
			{
				// If scrolling up, set the scroll to a negative number.
				scroll *= -1;
			}
			
			topOfScreen += scroll;
			
			if ( topOfScreen < topOfForm )
			{
				topOfScreen = topOfForm;
			}
			else if ( topOfScreen > bottomOfForm )
			{
				topOfScreen = bottomOfForm;
			}
		}
		else  // no scrollbar
		{
			// Change the highlighted component.
			highlightedComponent = nextHighlighted;
		}
		
		// Redraw the screen at the scrolled position.
		repaint();
	}
	
	/**
	 * Called when a key is pressed.  It can be identified using the
	 * constants defined in this class.
	 * 
	 * @param keyCode is the key code of the key that was pressed.
	 */
	protected void keyPressed (int keyCode)
	{
		// Forward the event to the current component.
		Component c = get( highlightedComponent );
		
		if ( c != null )
		{
			c.keyPressed( keyCode );
		}
		
		// Scrolling vertically?
		if ( keyCode == UP )
		{
			scroll( false );
		}
		else if ( keyCode == DOWN )
		{
			scroll( true );
		}

		// Continue processing the event.
		super.keyPressed( keyCode );
	}

	/**
	 * Called when a key is repeated (held down).  It can be identified using the
	 * constants defined in this class.
	 * 
	 * @param keyCode is the key code of the key that was held down.
	 */
	protected void keyRepeated (int keyCode)
	{
		// Forward the event to the current component.
		Component c = get( highlightedComponent );
		
		if ( c != null )
		{
			c.keyRepeated( keyCode );
		}
				
		// Continue processing the event.
		super.keyRepeated( keyCode );
	}

	/**
	 * Called when a key is released.  It can be identified using the
	 * constants defined in this class.
	 * 
	 * @param keyCode is the key code of the key that was released.
	 */
	protected void keyReleased (int keyCode)
	{
		// Forward the event to the current component.
		Component c = get( highlightedComponent );
		
		if ( c != null )
		{
			c.keyReleased( keyCode );
		}
		
		// Continue processing the event.
		super.keyReleased( keyCode );
	}


	/**
	 * Called when the pointer is pressed.
	 * 
	 * @param x is the horizontal location where the pointer was pressed.
	 * @param y is the vertical location where the pointer was pressed.
	 */
	protected void pointerPressed (int x, int y)
	{
		// Is this event moving the scrollbar?
		boolean movedScrollbar = false;
		
		if ( hasVerticalScrollbar() )
		{
			// Was the click over the scrollbar?
			int screenWidth = getScreenWidth();
			int scrollbarWidth = UIManager.getTheme().getVerticalScrollbarWidth();
			int scrollbarX = screenWidth - scrollbarWidth; 
			
			if ( x >= scrollbarX )
			{
				// The user clicked on the scrollbar.
				movedScrollbar = true;
				
				// Calculate the height of the trackbar.
				int height = getHeight();
				int formHeight = absoluteHeights[absoluteHeights.length - 1];
				
				int scrollableHeight = formHeight - height;
				double trackbarPercentage = (double)height / (double)formHeight;
				int trackbarHeight = (int)MathFunc.round( height * trackbarPercentage );
				trackbarHeight = Math.max( trackbarHeight, 2 * scrollbarWidth );

				// Calculate the range and location of the trackbar.
				//   The scrollbar doesn't actually go from 0% to 100%.  The top
				//   is actually 1/2 the height of the trackbar from the top of
				//   the screen.  The bottom is 1/2 the height from the bottom.
				int rangeStart = trackbarHeight / 2;
				int range = height - 2 * rangeStart;
				
				double offsetPercentage = (double)topOfScreen / (double)scrollableHeight;
				int center = rangeStart + (int)MathFunc.round( offsetPercentage * range );
				
				if ( y < center )
				{
					// Move the scrollbar up one component.
					scroll( false );
				}
				else
				{
					// Move the scrollbar down one component.
					scroll( true );
				}
			}
		}
		
		// Forward the event to the component clicked on.
		if ( movedScrollbar == false )
		{
			// Highlight the component.
			highlightedComponent = getAt( x, y );
			
			// Forward the event to the component for processing.
			Component c = get( highlightedComponent );
			
			if ( c != null )
			{
				// Adjust the click position relative to the component.
				int px = x - c.getX();
				int py = y - c.getY();
				
				c.pointerPressed( px, py );
			}
		}
		
		// Continue processing the event.
		super.pointerPressed( x, y );
	}
	
	/**
	 * Called when the pointer is dragged.
	 * 
	 * @param x is the horizontal location where the pointer was dragged.
	 * @param y is the vertical location where the pointer was dragged.
	 */
	protected void pointerDragged (int x, int y)
	{
		// Forward the event to the current component.
		Component c = get( highlightedComponent );
		
		if ( c != null )
		{
			c.pointerDragged( x, y );
		}
		
		// Continue processing the event.
		super.pointerDragged( x, y );
	}

	/**
	 * Called when the pointer is released.
	 * 
	 * @param x is the horizontal location where the pointer was released.
	 * @param y is the vertical location where the pointer was released.
	 */
	protected void pointerReleased (int x, int y)
	{
		// Forward the event to the current component.
		Component c = get( highlightedComponent );
		
		if ( c != null )
		{
			c.pointerReleased( x, y );
		}
		
		// Continue processing the event.
		super.pointerReleased( x, y );
	}
}

⌨️ 快捷键说明

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