关闭

用Swing写个scrapy爬虫图形界面

标签: 爬虫swingscrapyGUI用户图形界面
1035人阅读 评论(0) 收藏 举报
分类:

最近软件老师叫写个英语助手APP,所以没什么精力学其它的了,app源码已发至GitHub:

App–English-Learning-Assistant 英语学习小助手……很简单的一个app

半个月学了安卓,2个星期写出来的,写的不好见谅哈(估计没人会翻动:))。

好了,说正文。

贴上Swing写的代码:

这里写图片描述

package com.swing2.x;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JTextArea;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;


public class swingf extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new swingf("Scrapy crawl GitHub").setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public swingf(String title) {
        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 452, 411);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblHost = new JLabel("Host :");

        JLabel lblStroe = new JLabel("stroe :");

        JLabel lblXxxxxxxxxxxx = new JLabel("https://github.com");

        JLabel lblXxxxxxxxxxxx_1 = new JLabel("media/people");

        String command2="cmd /k start cmd.bat";

        Runtime   r=Runtime.getRuntime();
        JButton btnNewButton = new JButton("start:");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {   

                try {

                    Process   p=r.exec(command2);

                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }   
        } 

        });
        JLabel lblXxxxxxxxxxxxxxxxxxxxxxxx = new JLabel("scrapy crawl github");
        JButton btnEnd = new JButton("end");
        btnEnd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            try {
                Runtime.getRuntime().exec("TaskKill /IM cmd.exe");
            } catch (IOException e1) {
                // TODO 自动生成的 catch 块
                e1.printStackTrace();
            } 
            }
        });
        JButton btnInfo = new JButton("Info :");

        JTextArea textArea_1 = new JTextArea();

        textArea_1 .setLineWrap(true);
        textArea_1.setEditable(false);
        JScrollPane jsp = new JScrollPane(textArea_1);

        textArea_1.setBounds(100, 100, 500, 600);
        //jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    //这里设置不了滚动条  特郁闷。   
    //jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        contentPane.add(jsp, BorderLayout.CENTER);
        btnInfo.addActionListener(new ActionListener() {
            Mongo m;

            @Override

            public void actionPerformed(ActionEvent e) {

                textArea_1 .setLineWrap(true);
                // TODO 自动生成的方法存根
                try {
                    m = new Mongo("localhost", 27017);
                } catch (UnknownHostException e1) {
                    // TODO 自动生成的 catch 块
                    e1.printStackTrace();
                } catch (MongoException e1) {
                    // TODO 自动生成的 catch 块
                    e1.printStackTrace();
                }
                DB db = m.getDB("github");
                DBCollection  dbCol= db.getCollection("github33_info");
                DBCursor x=dbCol.find();
                while(x.hasNext()) {
                      BasicDBObject bdbObj = (BasicDBObject) x.next();  
                      if(bdbObj != null){ 
                          textArea_1.append("{username : "+bdbObj.getString("username")+" }");  
                      }    
                   }
              }
        });






        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                .addGroup(gl_contentPane.createSequentialGroup()
                                    .addComponent(lblHost)
                                    .addPreferredGap(ComponentPlacement.UNRELATED)
                                    .addComponent(lblXxxxxxxxxxxx))
                                .addGroup(gl_contentPane.createSequentialGroup()
                                    .addComponent(lblStroe)
                                    .addGap(18)
                                    .addComponent(lblXxxxxxxxxxxx_1))))
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addComponent(btnNewButton)
                            .addGap(18)
                            .addComponent(lblXxxxxxxxxxxxxxxxxxxxxxxx)
                            .addGap(18)
                            .addComponent(btnEnd))
                        .addComponent(btnInfo)
                        .addGroup(gl_contentPane.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(textArea_1, GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)))
                    .addContainerGap())
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblHost)
                        .addComponent(lblXxxxxxxxxxxx))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblStroe)
                        .addComponent(lblXxxxxxxxxxxx_1))
                    .addGap(18)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(btnNewButton)
                        .addComponent(lblXxxxxxxxxxxxxxxxxxxxxxxx)
                        .addComponent(btnEnd))
                    .addGap(33)
                    .addComponent(btnInfo)
                    .addGap(31)
                    .addComponent(textArea_1, GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                    .addContainerGap())
        );
        contentPane.setLayout(gl_contentPane);

    }
}

此外程序是连接数据库MongoDB的,所以要引用mongo-java库,才能获取数据库数据。

然后用exe4j把导出的jar换成exe文件,这样可以直接在win7下运行了。

这里需要注意几点:

1.在exe4j中,设置classpath时,要把这个mongo-java-driver-2.6.5.jar 添加进去

2.advanced options:选64bit

然后生成、运行:

这里写图片描述

这里写图片描述

这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:19447次
    • 积分:404
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:7篇
    • 译文:0篇
    • 评论:7条
    最新评论