我尝试制作自己的搜索引擎,但连接到谷歌或 雅虎,但我的搜索引擎不起作用。可以提出什么解决方案 用这个代码?它无法连接到浏览器。
import java.applet.Applet;
import java.net.*;
import java.awt.*;
public class search extends Applet {
//private String viewerTitle="Google";
TextField searchParameter;
Choice searchEngine;
Button searchButton;
// initialize the display
public void init() {
setBackground(Color.white);
searchParameter = new TextField(20);
add(searchParameter);
searchEngine = new Choice();
searchEngine.addItem("Yahoo");
searchEngine.addItem("Google");
searchEngine.select(0);
add(searchEngine);
searchButton = new Button("Search");
add(searchButton);
setTitle("eBook reader");
setSize(600, 1000);
setVisible(true);
}
private void setTitle(String string) {
// TODO Auto-generated method stub
}
public boolean action(Event e, Object o) {
if (e.target.equals(searchButton)) {
try {
sendSearch();
}
catch (Exception e1) {
showStatus("Exception caught:" + e1.toString());
}
}
return true;
}
@SuppressWarnings("deprecation")
public void sendSearch() throws Exception {
String searchString = searchParameter.getText();
if (searchString.equals("")) {
showStatus("Must enter a search string");
return;
}
String url;
switch (searchEngine.getSelectedIndex()) {
case 1: url = "http://search.yahoo.com/bin/search?p=";
break;
case 2: url = "http://www.google.com/search?q=";
break;
default: showStatus("Invalid search engine selected.");
return;
}
// encode the search data
url += URLEncoder.encode(searchString);
// launch the search engine
showStatus("Connecting to search location " + url);
getAppletContext().showDocument(new URL(url), "_top");
}
}