您的位置:首页 > 编程语言 > Go语言

访问Google搜索API的swing窗体程序

2009-05-06 22:54 806 查看
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import com.google.soap.search.GoogleSearch;
import com.google.soap.search.GoogleSearchFault;
import com.google.soap.search.GoogleSearchResult;

//测试类
public class Exec {
public static void main(String[] args) {
// 创建窗体类变量
DemoWindow dw = new DemoWindow("访问Google搜索引擎的搜索结果");

// 将窗体的宽度和高度分别设置为屏幕宽度和屏幕高度的1/3,左上角位置也设置为屏幕宽度和屏幕高度的1/3处
Toolkit theKit = dw.getToolkit();
Dimension wndSize = theKit.getScreenSize();
dw.setBounds(wndSize.width / 3, wndSize.height / 3, wndSize.width / 3,
wndSize.height / 3);

// 点击关闭按钮可以退出程序
dw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 设置窗体为可见
dw.setVisible(true);
}
}

// 界面窗体
class DemoWindow extends JFrame implements ActionListener {
// 输入检索词的文本框
JTextField jtf = new JTextField(15);

String[] values = { "搜索关键词", "搜索Word文件", "得到拼写建议" };

JComboBox jcb = new JComboBox(values);

// 操作按钮
JButton jb = new JButton("搜索");

// 显示搜索结果的文本区
JTextArea jta = new JTextArea();

// 设置文本区的滚动条
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jta, v, h);

// 布局面板
JPanel jp = new JPanel();

// 利用Google搜索引擎实现的搜索类
SearchInGoogle sig = new SearchInGoogle();

// 构造函数
public DemoWindow(String title) {
super(title);

// 窗体布局
jp.add(jtf);
jp.add(jcb);
jp.add(jb);
add(jp, BorderLayout.NORTH);
add(jsp, BorderLayout.CENTER);

// 添加事件监听器
jb.addActionListener(this);
jtf.addActionListener(this);
}

// 响应单击按钮
public void actionPerformed(ActionEvent e) {
// 显示从Google获取的搜索结果
jta.setText(sig.getResults((String) (jcb.getSelectedItem()), jtf
.getText()));
}
}

// 利用Google搜索引擎实现的搜索类
class SearchInGoogle {
// 从Google搜索引擎获取搜索结果的函数
public String getResults(String directive, String query) {
// 输入客户端密钥,可以从Google搜索引擎上注册获取
String clientKey = "********************************";

// 返回结果
String result = "";

// 创建GoogleSearch类变量
GoogleSearch gs = new GoogleSearch();

// 设置客户端密钥
gs.setKey(clientKey);

try {
// 如果是搜索关键词
if (directive.equals("搜索关键词")) {
// 设置Google搜索引擎的查询内容
gs.setQueryString(query);

// 获取Google搜索引擎的查询结果
GoogleSearchResult r = gs.doSearch();

// 输出检索结果
result += "Google搜索引擎的搜索结果为:/n";
result += r.toString();
}
// 如果是搜索Word文件
else if (directive.equals("搜索Word文件")) {
// 设置Google搜索引擎的查询内容
gs.setQueryString("filetype:doc "+query);

// 获取Google搜索引擎的查询结果
GoogleSearchResult r = gs.doSearch();

// 输出检索结果
result += "Google搜索引擎搜索的Word文件为:/n";
result += r.toString();
}
// 如果是得到拼写建议
else if (directive.equals("得到拼写建议")) {
// 获取Google搜索引擎的拼写建议
String suggestion = gs.doSpellingSuggestion(query);

// 输出检索结果
result += "Google搜索引擎的拼写建议为/n";
result += suggestion;
}
} catch (GoogleSearchFault f) {
System.out.println(f.toString());
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: