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

📄 searchchineseform.java

📁 手机在线系统 采用Java 中的J2ME, JSP 跟MySql 运行环境是:jsdk1.4以上
💻 JAVA
字号:
/**
 * @(#)SearchChineseForm.java	1.11 01/08/23
 * Copyright (c) 2004-2005 wuhua of workroom Inc. All Rights Reserved.
 * @version 	1.0, 10/05/2004
 * @author 	饶荣庆
 * @author 	余煜辉
 */
package com.j2me.dictionary;

import com.j2me.language.*;
import com.j2me.main.*;
import com.j2me.common.*;
import java.io.*;
import javax.microedition.lcdui.*;

/**
 *此类是用中文查找单词的显示层
 */
public class SearchChineseForm extends Form implements CommandListener, ItemStateListener
{
	private Display display;
	private DictionaryMenu dictionary;

	/*数据输入对象*/
	public TextField key; //输入查找关键字;
	public StringItem result; //返回查找结果

	/*定义ChoiceGroup对象数组用来给用户筛选*/
	public ChoiceGroup choice;

	/*声音线程*/
	private PronunciationThread pT;

	/*错误*/
	private Alert errorAlert = null;

	//定义离开软键
	private Command backCommand = null;		
	private Command okCommand = null;
	private Command playCommand = null;
	private Command stopCommand = null;
    
    
	/*用于判断是否精确查找*/
	private String isExactitude = "true";
	private String[] data = new String[2];
	private String uRL; //查找地址
	private	String par;  //查找参数
	private int word; //判断有多少个单词
	private String str; //获得单词,当只查询到一个单词时

	private boolean reNew; 

	/*-----------------------------------------------------------------------
	 *构造方法
	 */
	public SearchChineseForm(String title)
	{
		super(title);
		this.key = new TextField("输入单词:", "", 100, TextField.ANY);
		this.result = new StringItem("详细信息:  ", "");
		this.choice = new ChoiceGroup("结果",  Choice.EXCLUSIVE);
		//this.choice.append("精确查找", null);
		//this.choice.append("模糊查找", null);
		this.backCommand = new Command("返回", Command.BACK, 2);	
		this.okCommand = new Command("确定", Command.BACK, 2);	
		this.playCommand = new Command("播放", Command.OK, 2);
		this.stopCommand = new Command("停止", Command.OK, 2);
		this.addCommand(backCommand);
		this.addCommand(okCommand);
		this.append(key);
		this.append(choice); //显示数据
		this.append(result);
		this.setCommandListener(this);
		this.setItemStateListener(this);
	}

	/*显示菜单界面方法*/
	public void showForm(Display display, DictionaryMenu dictionary)	//用来显示界面
	{		
		this.display = display;
	   	this.dictionary = dictionary;
		this.display.setCurrent(this);
	} 


	
	/**
	 *定义私有方法下载数据
	 */
	private void callEnglish(String URLString, String parameter)
	{		
		uRL = URLString;
		par = parameter;

		Thread t = new Thread() 
		{   
			public void run() 
			{ 
				String html= "";
				try
				{	
					//获得服务器用户保存的内容
					html = ConnectHttp.invokeJSP(uRL, par);
				
     				//处理HTML标记语言
			    	html = DisposeHTML.getBody(html);	
					System.out.println(html);
			    }
			    catch(IOException e)
				{
					errorAlert  = new Alert("ERROR", "对不起!连接错误", null, AlertType.ERROR);	
					errorAlert.setTimeout(2000);
					display.setCurrent(errorAlert);
					System.out.println(e);
				}
				if (html.trim().compareTo("false") == 0)
				{
					errorAlert  = new Alert("ERROR", "对不起!找不到这个单词!", null, AlertType.ERROR);
					errorAlert.setTimeout(2000);
					display.setCurrent(errorAlert);
					result.setText(""); //设置为空,表示连接成功,并设置为空
				}
				else
				{
					/*Image offscreen_buffer = Image.createImage(getWidth(), getHeight());
					Graphics g = offscreen_buffer.getGraphics();
					g.fillRect(0, 0, getWidth(), getHeight());
					g.setColor(10, 10, 10);*/
					int j = 0;
					html = html.trim();
					while(true)	  //处理从网络上获得的数据并对其进行处理
					{
						result.setText(""); //设置为空,表示连接成功,并设置为空
						j = html.indexOf("~");
						if (j == -1)
						{
							break;
						}
						choice.append(html.substring(0, j), null);
						word++; //看查找到多少个单词
						str = html.substring(0, j);
						html = html.substring(j + 1, html.length());
					}

				}

			}
		};

	   t.start();	  //启动线程让它进行连网	
	}




	/**
	 *定义私有方法下载数据
	 */
	private void callHttp(String URLString, String parameter)
	{		
		uRL = URLString;
		par = parameter;

		Thread t = new Thread() 
		{   
			public void run() 
			{ 
				String html= "";
				try
				{	
					//获得服务器用户保存的内容
					html = ConnectHttp.invokeJSP(uRL, par);
				
     				//处理HTML标记语言
			    	html = DisposeHTML.getBody(html);	
			    	data = DisposeData.getStringGroup(html, "~", 2);
					System.out.println(html);
			    }
			    catch(IOException e)
				{
					errorAlert  = new Alert("ERROR", "对不起!连接错误", null, AlertType.ERROR);	
					errorAlert.setTimeout(2000);
					display.setCurrent(errorAlert);
					System.out.println(e);
				}

				if (html.trim().compareTo("false") == 0)
				{
					errorAlert  = new Alert("ERROR", "对不起!找不到这个单词!", null, AlertType.ERROR);
					errorAlert.setTimeout(2000);
					display.setCurrent(errorAlert);
				}
				else
				{
					result.setText("\n" + data[0]);
					System.out.println(data[0]);
					addCommand(playCommand);					
				}
			}
		};

	   t.start();	  //启动线程让它进行连网	
	}


	public void commandAction(Command c,Displayable s)
	{
		if (c == backCommand)
		{
			this.display.setCurrent(dictionary);
			this.pT.close();
		}
		if (c == okCommand)
		{
			/*假如只有一个单词则显示出来*/
			if (word == 1)
		    {
				//获得查找关键字,并搜索网络
				String parameter =  "key=" + str + "&filter=" + "true";
				this.execte(parameter);
				this.addCommand(playCommand);
				System.out.println(word);
			}
			else
			{
				/*判断用户是否重新查找*/
				if (this.reNew)
				{
					try
					{
						this.choice.deleteAll(); //如果是重新查找则删除上次查找信息
						this.removeCommand(playCommand);
					}
					catch(NullPointerException n)
					{}
				}
				
				//参数
				String parameter =  "key=" + key.getString().trim();
		    	if (key.getString().trim().compareTo("") != 0)
			    {
		    		this.result.setText("\n连接网络...");
		    		this.callEnglish(DictionaryServerURL.getURL() + "chinese_find.jsp", parameter);
		     	}
			    else
	     		{
		    		errorAlert  = new Alert("ERROR", "对不起!请输入单词", null, AlertType.ERROR);	
             		errorAlert.setTimeout(2000);
		            this.display.setCurrent(errorAlert);
		    	}
			}
			
		}
		if (c == playCommand)
		{
			pT = new PronunciationThread(DictionaryServerURL.getURL() +"pronunciation/" + data[1] + ".mid");
			pT.start();
			//this.append("从服务器上下载声音!请稍等...");
			this.addCommand(stopCommand);
			this.removeCommand(playCommand);
		}
		if (c == stopCommand)
		{	
			this.pT.stop();
			this.addCommand(playCommand);
			this.removeCommand(stopCommand);
		}
		
	}

	private void execte (String parameter)
	{
		String parameter1 =  parameter;
		if (key.getString().trim().compareTo("") != 0)
		{
			this.result.setText("\n连接网络...");
			this.callHttp(DictionaryServerURL.getURL() + "english_find.jsp", parameter1);
		}
		else
		{
			errorAlert  = new Alert("ERROR", "对不起!请输入单词", null, AlertType.ERROR);	
         	errorAlert.setTimeout(2000);
		    this.display.setCurrent(errorAlert);
		}
		this.removeCommand(stopCommand);
			
		try
		{
			this.pT.stop();
		}
		catch(Exception e)
		{
		}
		this.word = 0; //查找详细信息以后,重新分配为0
	}

	public void itemStateChanged(Item item)
	{
		
		if (item == choice)
		{
			//获得查找关键字,并搜索网络
			String parameter =  "key=" + this.choice.getString(choice.getSelectedIndex()).trim() + "&filter=" + "true";
			this.execte(parameter);
		}
		if (item == key)
		{
			reNew = true;   //重新查找
		}
	}


}

⌨️ 快捷键说明

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