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

📄 textbox.java

📁 关于J4ME J2ME实例
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		int size = 0;
		
		if ( contents != null )
		{
			size = contents.length();
		}
		
		return size;
	}
	
	/**
	 * Paints the text box.
	 * 
	 * @param g is the <code>Graphics</code> object to be used for rendering the item.
	 * @param theme is the application's theme.  Use it to get fonts and colors.
	 * @param width is the width, in pixels, to paint the component.
	 * @param height is the height, in pixels, to paint the component.
	 * @param selected is <code>true</code> when this components is currently selected
	 *  and <code>false</code> when it is not.
	 * 
	 * @see org.j4me.ui.components.Component#paintComponent(Graphics, Theme, int, int, boolean)
	 */
	protected void paintComponent (Graphics g, Theme theme, int width, int height, boolean selected)
	{
		int y = 0;
		
		// Paint the label above this component.
		if ( label != null )
		{
			// Make the justification the same as for this component.
			label.setHorizontalAlignment( this.getHorizontalAlignment() );
			
			// Paint the label.
			label.paint( g, theme, getScreen(), 0, 0, width, height, selected );
			
			// The top of the progress bar is below the label.
			int labelHeight = label.getHeight();
			y = labelHeight;
			height -= labelHeight;
		}
		
		// Paint the text box.
		int offset = paintRect( g, theme, 0, y, width, height, selected );
		
		// Paint the contents of the text box.
		if ( contents != null )
		{
			// Indent the text a bit from the sides of the component's interior.
			offset += TEXT_OFFSET;
			
			// Calculate the layout of the text inside the text box.
			int left = offset;
			int top = y + offset;
			width -= 2 * offset;
			height -= 2 * offset;
	
			int anchor = Graphics.LEFT | Graphics.TOP;
	
			g.clipRect( left, top, width, height );
			g.setColor( theme.getFontColor() );
			
			// Determine the text to display inside the text box.
			String display = contents;
			
			if ( isPassword() )
			{
				int length = contents.length();
				StringBuffer builder = new StringBuffer( length );
				
				for ( int i = 0; i < length; i++ )
				{
					builder.append( '*' );
				}
				
				display = builder.toString();
			}
			else if ( isPhoneNumber() )
			{
				// Modifiy phone numbers with an area code. 
				if ( display.length() == 10 )
				{
					StringBuffer builder = new StringBuffer( 15 );
					
					builder.append( "(" );
					builder.append( display.substring(0, 3) );
					builder.append( ") " );
					
					builder.append( display.substring(3, 6) );
					builder.append( "-" );
					
					builder.append( display.substring(6, 10) );
					
					display = builder.toString();
				}
			}
			
			// Paint the text inside the text box.
			g.drawString( display, left, top, anchor );
		}
	}

	/**
	 * Returns the dimensions of the text box.
	 * 
	 * @see org.j4me.ui.components.Component#getPreferredComponentSize(org.j4me.ui.Theme, int, int)
	 */
	protected int[] getPreferredComponentSize (Theme theme, int viewportWidth, int viewportHeight)
	{
		int fontHeight = theme.getFont().getHeight();
		int height = fontHeight + 2 * (HIGHLIGHTED_BORDER_WIDTH + TEXT_OFFSET);
		
		// Add the height of the label above the component.
		if ( label != null )
		{
			int[] labelDimensions = label.getPreferredComponentSize( theme, viewportWidth, viewportHeight );
			height += labelDimensions[1];
		}
		
		return new int[] { viewportWidth, height };
	}
	
	/**
	 * An event raised whenever the component is made visible on the screen.
	 * This is called before the <code>paintComponent</code> method.
	 * 
	 * @see Component#showNotify()
	 */
	protected void showNotify ()
	{
		// Pass the event to contained components.
		if ( label != null )
		{
			label.show( true );
		}

		// Continue processing the event.
		super.showNotify();
	}

	/**
	 * An event raised whenever the component is removed from the screen.
	 * 
	 * @see Component#hideNotify()
	 */
	protected void hideNotify ()
	{
		// Pass the event to contained components.
		if ( label != null )
		{
			label.show( false );
		}
		
		// Continue processing the event.
		super.hideNotify();
	}
	
	/**
	 * @return <code>true</code> because this component accepts user input.
	 */
	public boolean acceptsInput ()
	{
		return true;
	}

	/**
	 * Called when a key is pressed.
	 * 
	 * @param keyCode is the key code of the key that was pressed.
	 */
	public void keyPressed (int keyCode)
	{
		// Was it an input character?
		//   Otherwise it was a menu button of special character.
		if ( (keyCode > 0) || (keyCode == DeviceScreen.FIRE) )
		{
			select();
		}
	}
	
	/**
	 * Called when the pointer is pressed.
	 * 
	 * @param x is the horizontal location where the pointer was pressed
	 *  relative to the top-left corner of the component.
	 * @param y is the vertical location where the pointer was pressed
	 *  relative to the top-left corner of the component.
	 */
	public void pointerPressed (int x, int y)
	{
		// If anywhere on the text box is pressed it has been selected.
		select();
		
		// Stop processing the pointer event.
		//   i.e. do not call super.pointerPressed( x, y );
	}
	
	/**
	 * Called when the text box's value is being edited.  This method
	 * replaces the current screen with the JVM's <code>TextBox</code> screen.
	 * When the user enters a new value the original screen returns and
	 * is updated with the new value.
	 */
	protected void select ()
	{
		// Create a text entry screen using the LCDUI.
		//   We use the LCDUI because we can't provide this functionality
		//   on all phones by painting ourselves.  For instance Motorola
		//   phones provide special menus for deleting characters because
		//   they do not have a dedicated button.
		//
		//   There is a problem with ProGuard 4.0 optimizations.  We need
		//   to actually pass in the label and contents strings instead of
		//   having them collected by the constructor of TextInput.  It
		//   does not work and throws an exception without doing this to
		//   trick the optimizer.
		DeviceScreen current = UIManager.getScreen();
		String label = getLabel();
		String contents = getString();
		int maxSize = getMaxSize();
		TextInput entry = new TextInput( current, this, label, contents, maxSize, constraints );

		// Display the text entry screen.
		Display display = UIManager.getDisplay();
		display.setCurrent( entry );
	}
	
	/**
	 * The native implementation for inputting text.  This takes over the
	 * entire screen and returns when the user is done entering text.
	 */
	private final class TextInput
		extends javax.microedition.lcdui.TextBox
		implements CommandListener
	{
		/**
		 * The Cancel button.
		 */
		private final Command cancel;

		/**
		 * The OK button.
		 */
		private final Command ok;

		/**
		 * The screen that invoked this one.
		 */
		private final DeviceScreen parent;
		
		/**
		 * The component this screen is collecting data for.
		 */
		private final TextBox component;

		/**
		 * Creates a native system input screen for text.
		 * 
		 * @param parent is the screen that invoked this one.
		 * @param box is the component on <code>parent</code> this input is for.
		 * @param label is the title for the input.
		 * @param contents is the initial value of the contents.
		 * @param maxSize is the maximum number of characters that can be entered.
		 * @param constraints are the options that effect the type of data
		 *  that can be entered.
		 */
		public TextInput (DeviceScreen parent, TextBox box, String label, String contents, int maxSize, int constraints)
		{
			super( label,
				   contents,
				   maxSize,
				   constraints );
			
			// Record the owners.
			this.parent = parent;
			this.component = box;

			// Add the menu buttons.
			Theme theme = UIManager.getTheme();

			String cancelText = theme.getMenuTextForCancel();
			cancel = new Command( cancelText, Command.CANCEL, 1 );
			addCommand( cancel );
			
			String okText = theme.getMenuTextForOK();
			ok = new Command( okText, Command.OK, 2 );
			addCommand( ok );
			
			setCommandListener( this );
		}

		/**
		 * Called when the user hits the OK or Cancel button.
		 */
		public void commandAction (Command c, Displayable d)
		{
			if ( c == ok )
			{
				// Update the contents of owning box.
				String input = this.getString();
				component.setString( input );
			}

			// Return to the parent screen.
			parent.show();
			parent.repaint();
		}
	}
}

⌨️ 快捷键说明

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