课外天地 李树青学习天地信息检索原理课件 → 访问Google搜索API的swing窗体程序


  共有20766人关注过本帖树形打印复制链接

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

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
访问Google搜索API的swing窗体程序  发帖心情 Post By:2009/5/6 19:08:27 [只看该作者]

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;
        }
}

[此贴子已经被作者于2010-12-14 09:38:43编辑过]

 回到顶部