英语背单词联网版本
源代码在线查看: searchform.java
/* * SearchForm.java * * Created on 2004年1月13日, 下午7:03 */ package handenglish; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.*; /** * * @author Administrator * @version */ public class SearchForm extends Form implements ReceiveListener,CommandListener,Runnable{ private Receive m_Receive; private Midlet midlet; private TextField m_Input; private Command m_CmdSearch; private StringItem m_Result; private Vector m_Words; private boolean doSearch = false; private Thread searchThread; public SearchForm(Midlet midlet) { super( "单词查询" ); this.midlet = midlet; m_Receive = new Receive(midlet); m_Receive.setReceiveListener( this ); m_Input = new TextField( "输入查询单词","",60,TextField.ANY ); m_CmdSearch = new Command( "查询",Command.OK,0 ); m_Words = new Vector(); m_Result = new StringItem( "","" ); append( m_Input ); addCommand( m_CmdSearch ); addCommand( midlet.cmdBack ); setCommandListener( this ); startThread(); } public void dataReceived(int cmd, int status) { m_Result.setLabel( "查询结果" ); StringBuffer strs = new StringBuffer(); for( int i = 0;i< m_Words.size();i++ ) { strs.append( m_Words.elementAt(i) ); strs.append( "\n\n" ); } if ( m_Words.size() == 0 ) m_Result.setText( "您查的单词未找到!" ); else m_Result.setText( strs.toString() ); } public void dataReceiving(int cmd, int pos, int total, int send) { } public void errorReceived(int errno) { Alert alert = new Alert( "错误信息","通讯错误,请与服务提供商联系!错误号=" + errno ,null,AlertType.ERROR ); alert.setTimeout( Alert.FOREVER ); Display.getDisplay( midlet ).setCurrent( alert ); } public void setCurState(int curState) { } public void startThread() { if(searchThread==null) searchThread=new Thread(this); searchThread.start(); } public void stopThread() { searchThread = null; } public void commandAction(Command c, Displayable s) { if ( c == midlet.cmdBack && s == this ) { stopThread(); midlet.nowDisplay( s,midlet.WIN_MENU ); } else if( c == m_CmdSearch && s == this ) { if ( m_Input.getString().equals( null ) || m_Input.getString().equals( "" ) ) { Alert alert = new Alert( "警告信息","请输入查询单词!" ,null,AlertType.WARNING ); alert.setTimeout( Alert.FOREVER ); Display.getDisplay( midlet ).setCurrent( alert ); return; } removeResult(); m_Result.setLabel( "正在查询中,请稍候..." ); m_Result.setText(""); append( m_Result ); doSearch = true; } } private void removeResult() { for( int i=0;i< size();i++ ) { Item item = get( i ); if ( item == m_Result ) delete( i ); } } public void run() { Thread curThread = Thread.currentThread(); while ( searchThread == curThread ) { try { Thread.sleep( 200 ); if ( doSearch ) { m_Receive.queryWord( m_Input.getString(),m_Words ); doSearch = false; } }catch(Exception e){ //System.out.println(e.toString()); } } } }