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

📄 textfield.java

📁 j2me开发的1个简单的wap浏览器.程序框架还是不错的.包括对低级事件的处理.通信框架,标签的解析
💻 JAVA
字号:
/********************************************************************
 * 项目名称				:<b>j2me学习 J2me Wap Explorer</b>			<br/>
 * 
 * Copyright 2005-2006 Wuhua. All rights reserved </br>
 *
 * 本程序只用于学习目的,不能用于商业目的。如有需要请联系作者
 ********************************************************************/
package org.wuhua.wap.ui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

import org.wuhua.wap.log.Logger;
import org.wuhua.wap.ui.core.Platform;
import org.wuhua.wap.ui.timer.TimerTaskManager;

 

/**
 * <b>类名:TextField.java</b> </br> 
 * 编写日期: 2006-12-28 <br/>
 * 程序功能描述: <br/>
 * Demo: <br/>
 * Bug: <br/>
 * 
 * 程序变更日期 :<br/> 
 * 变更作者 :<br/> 
 * 变更说明 :<br/>
 * 
 * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
 */
public class TextField extends Part implements Runnable, javax.microedition.lcdui.CommandListener{
	private static Logger logger = Logger.getLogger("TextField");

	private Command ok = new Command("确定", Command.OK, Command.SCREEN);
	private Command cancel = new Command("取消", Command.CANCEL, Command.SCREEN);
 
	
	 

	
	public static final int TEXT = 0x00000;
	public static final int PASSWORD = 0x10000;
	
	private int numOfEls;
	
	/**
	 * 最多显示的字符数字
	 */
	private int maxSize;
	
	private char[] textChars;
	
	 
 
	 
	private boolean showCaret = false;
	
	private int type;
	private java.util.TimerTask task;
    
 

	private TextFiledGUI tgui;
	
	public TextField(String label, String text,int maxSize, int type) {
		super(0, 22, 0, 0);
		this.maxSize = maxSize;
		if(label == null)
			this.label = "";
		else
			this.label = label;
		this.setString(text);
		this.setType(type);
		
		this.view[HEIGHT] = font.getHeight();
		tgui = new   TextFiledGUI("输入文本", text, maxSize, type);
		tgui.setCommandListener(this);
		 
		 
	}
	
	private void setType(int type) {
		this.type = type;
		 
	}

	private void setString(String text){
		if(text == null)
			text = "";
		this.text = text;
		textChars = text.toCharArray();
		if(textChars.length >= this.maxSize){
			this.numOfEls = this.maxSize;
		}else{
			this.numOfEls =textChars.length;
		}
		this.url = text;
		
		 
	}
	
	
	/**
	 * 改变部件的位置
	 */
	public void changePosition(int x, int y, int width, int height){
		super.changePosition(x,y,width,height);
	 
	 
	}
	
	public void paint(Graphics g) {
		
		this.font = g.getFont();
		paintLabel(g);
		paintTextImpl(g);

	}
	
	/**
	 * 绘制标签
	 * @param g
	 */
	void paintLabel(Graphics g){
		 
		if(label == null || label.equals(""))
			return;
		//logger.debug(view[Y]);
	 	g.setColor(fontColor);
		g.drawString(label,view[X],view[Y]+1,Graphics.TOP | Graphics.LEFT);
		 
	}
	
	/**
	 * 绘制文本,同时绘制文本框
	 * @param g
	 */
	void paintTextImpl(Graphics g){
		 
		int charWidth = view[X]  + font.stringWidth(label);
		int charHeight = view[Y] ;
	
		if(type == TextField.PASSWORD)
			charWidth = paintPassword(g, charWidth, charHeight);
		else
			charWidth = paintText(g, charWidth, charHeight);
		
		paintCursor(charWidth + 4,view[Y]+2,g);
	}

	/**
	 * 绘制光标,这个光标是要求闪动得
	 */
	private void paintCursor(int x, int y, Graphics g){
		activeTask();
		
		g.setColor(0x3112323);
		if(showCaret && this.hasFocus){			 
			g.drawChar('|',x,y-1,Graphics.TOP | Graphics.LEFT);
		} 	
	}

	private void activeTask() {
		if(this.hasFocus && task == null){
			task = TimerTaskManager.getInstace().add( this , 300 );
		}else{
			if(!hasFocus && task != null){
				task.cancel();
				task = null;
			}
			
			
		}
	}
	
	
	private  int paintText(Graphics g, int charWidth, int charHeight) {
		int x = charWidth;
		for(int i=0 ; i < this.numOfEls; i++){
			g.drawChar(this.textChars[i],charWidth + 4,charHeight + 1,Graphics.TOP | Graphics.LEFT); 
			charWidth = charWidth +  font.charWidth(textChars[i]);
		}
		paintRect(g, charWidth, charHeight, x);
		
		return charWidth;
		 
	}

	private void paintRect(Graphics g, int charWidth, int charHeight, int x) {
		if(this.hasFocus){
			g.setColor(0x0A24C3);
			g.drawRect(x-1, charHeight-1, charWidth+2,  font.getHeight()+2);
		}		
		else
			g.setColor(this.bgColor);
		g.drawRect(x, charHeight, charWidth,  font.getHeight());
	}
	private  int paintPassword(Graphics g, int charWidth, int charHeight) {
		int x = charWidth;
		for(int i=0 ; i < this.numOfEls; i++){
			g.drawChar('*',charWidth+4,charHeight+2,Graphics.TOP | Graphics.LEFT); 
			charWidth = charWidth +  font.charWidth(textChars[i]);
		}
		paintRect(g, charWidth + 4, charHeight, x);
		
		return charWidth;
	}
	
	 
	
	 

	 

	public void onClick(int keyCode) {	
		if (keyCode >= 48 && keyCode <= 58) {
			addTextImpl(keyCode);
		}else if(keyCode == Platform.KEY_CLEAR) {
			deleteTextImpl();
		}else if(keyCode == Platform.KEY_ENTER){
			openGUIFor();
		}

	}

	 
	
	 
	
	
	private void openGUIFor() {
		tgui.setString(text);
		display.setCurrent(tgui);
		
		
	}

	//添加数据
	private void addTextImpl(int keyCode){
		if(text.length() >= this.maxSize)
			return;
		int k = keyCode - 48;
		text = text + String.valueOf(k);
		setString(text);
	}
	// 清除数据
	private void deleteTextImpl() {
		text = text.substring(0, text.length() > 1 ? text.length() - 1
								: 0);
		setString(text);
	}

	public void run() {
		try{
			flashCaret();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
	}
	/**
	 * 此为一个内部的计时器类,主要用来负责,输入框的闪动效果</br>
	 * 具体是0.3秒闪一次
	 *  
	 *
	 */
     
	private void flashCaret(){
		showCaret = !showCaret;
		//logger.debug(container);
		this.container.repaint();
	}
	
	public int getHeight() {
		return font.getHeight() + 2;
	}

	public int getWidth() {
		return this.maxSize;
	}
	
	
	
	class TextFiledGUI extends javax.microedition.lcdui.TextBox   {
		public TextFiledGUI(String title, String text, int maxSize, int constraints) {
			super(title, text, maxSize, constraints);
			this.addCommand(ok);
			this.addCommand(cancel);
			
		}
		
	}

	public void commandAction(Command c, Displayable d) {
		 if(c == ok){
			 this.setString(tgui.getString());
			this.url = tgui.getString();
		 }
		 display.setCurrent(this.container);
		
	}
	
}

⌨️ 快捷键说明

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