程序代码:
import java.applet.applet;
import java.net.*;
import java.awt.*;
public class searchengine extends applet
{
textfield keyword = new textfield(30); // 定义搜索的关键字
choice enginename; // 使用的搜索引擎列表,使用下拉框
button go = new button("开始搜索");
public void init()
{
setbackground(color.white); // 设置背景为白色以便配合网页色彩
keyword = new textfield(20);
enginename = new choice();
enginename.additem("中文雅虎");
enginename.additem("搜狐");
enginename.additem("新浪");
enginename.additem("网易");
enginename.select(0); // 设置缺省显示的项目为 "中文雅虎"
add(keyword);
add(enginename);
add(go);
}
public boolean action(event e, object obj)
{
if(e.target.equals(go))
{
try
{ gosearch(); }
catch (exception e1)
{ showstatus("搜索时发生异常:" + e1.tostring()); }
}
return true;
}
public void gosearch() throws exception
{
string str = keyword.gettext();
if(str.equals(""))
{
showstatus("请填写搜索的关键字!");
return;
}
string url = "";
switch (enginename.getselectedindex())
{
case 0 :
url = "http://cn.search.yahoo.com/search/cn?p=";
break;
case 1 :
url = "http://site.search.sohu.com/sitesearch.jsp?key_word=";
break;
case 2 :
url = "http://http://search.sina.com.cn/cgi-bin/search/search.cgi? _searchkey=";
break;
case 3 :
url = "http://nisearch.163.com/search?q=";
}
url += urlencoder.encode(str); // 将关键字编码成url格式的,就是例如空格编码为 20%
showstatus("正在连接搜索引擎" + url);
getappletcontext().showdocument(new url(url), "_black");// 在新窗口打开搜索结果
showstatus("搜索完成");
}
}
闽公网安备 35060202000074号