用Swing写个scrapy爬虫图形界面

最近软件老师叫写个英语助手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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值