Swing 学习1

来源:互联网 发布:迅雷9右侧 知乎 编辑:程序博客网 时间:2024/06/04 17:46

package com.resume.sky;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;
import javax.swing.*;


public class AutoDownLoadPanel extends JPanel implements ActionListener {

 public  String resumeStorePath;
 
 private String selItem="10分钟";//下载更新时间间隔
 
 private int totalResumeNum=0;

 private JButton startButton;

 private JButton stopButton;
 
 private JButton resetButton;
 
 private JComboBox selectCombox;
 
 private JLabel updateLabel;
 
 private JPanel paraJPanel;
 
 private JPanel viewJPanel;
 
 private JTextArea viewTextArea;
 
 private JScrollPane scrollTextAreaPane;
 
 private AutoDownLoad autoDownLoad;
 
 private ShowTimePanel sTimePanel;

 public AutoDownLoadPanel(String resumeStorePath) {
  this.resumeStorePath=resumeStorePath;
  this.resumeStorePath+="//";
  jbInit();
 }
   
 public void setRootStorePath(String resumeStorePath)
 {
  this.resumeStorePath=resumeStorePath;
  this.resumeStorePath+="//";
 }
 
 public void jbInit() {
  this.setLayout(new BorderLayout(5, 10));
  paraJPanel = new JPanel();
  paraJPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
    .createEtchedBorder(), "控制面板"));

  viewJPanel= new JPanel();
  viewJPanel.setLayout(new BorderLayout(5, 10));
  viewJPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory
    .createEtchedBorder(), "下载监控"));
  
  startButton = new JButton("开始下载");
  stopButton = new JButton("停止下载");
  resetButton = new JButton("刷新");
  updateLabel = new JLabel("下载更新时间间隔");
  selectCombox=new JComboBox(new String[]{"10分钟","15分钟","30分钟","45分钟","60分钟"});
  selectCombox.setSelectedIndex(0);//默认显示第一条简历
  selectCombox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                selItem = (String)cb.getSelectedItem();
            }
        });
  stopButton.setEnabled(false);
  startButton.addActionListener(this);
  resetButton.addActionListener(this);
  stopButton.addActionListener(this);
  paraJPanel.add(updateLabel);
  paraJPanel.add(selectCombox);
  paraJPanel.add(startButton);
  paraJPanel.add(stopButton);
  paraJPanel.add(resetButton);
  this.add(paraJPanel, BorderLayout.NORTH);
       
  viewTextArea=new JTextArea();
  scrollTextAreaPane=new JScrollPane();
  scrollTextAreaPane.setViewportView(viewTextArea);
  viewJPanel.add(scrollTextAreaPane,BorderLayout.CENTER);
  
  sTimePanel=new ShowTimePanel();
  viewJPanel.add(sTimePanel,BorderLayout.NORTH);
  this.add(viewJPanel, BorderLayout.CENTER );
  autoDownLoad = new AutoDownLoad("DetailSingle[*].htm");

 }

 public void actionPerformed(ActionEvent evt) {
  if (evt.getSource() == startButton) {
   viewTextArea.setText("----------------------------------------------------------------------------------------------------------------" +
     "简历下载进程----------------------------------------------------------------------------------------------------------------/n/n");
   startButton.setEnabled(false);
   startButton.setText("下载中...");
   stopButton.setEnabled(true);
   autoDownLoad.setDownLoad(true);
   selectCombox.setEnabled(false);
   Thread t = new Thread(autoDownLoad);
   t.start();//自动下载简历
   sTimePanel.setShowing(true);
   sTimePanel.clockThread.start();//下载时间记录开启

  } else {
   if (evt.getSource() == stopButton) {
    if (JOptionPane.showConfirmDialog(null, "确定要停止下载吗?", "提示",
      JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
     autoDownLoad.setDownLoad(false);
     startButton.setEnabled(true);
     startButton.setText("开始下载");
     stopButton.setEnabled(false);
     selectCombox.setEnabled(true);
     sTimePanel.setShowing(false);
     totalResumeNum=0;
    }
   }
   else
   {
    if (evt.getSource() == resetButton) {//刷新
     viewTextArea.setText("");
     sTimePanel.resetLabelText();
     sTimePanel.resetNumLabelText();
    }
   }
  }
 }

 /**
  * 自动下载线程类
  * @author qKF9319
  *
  */
 class AutoDownLoad implements Runnable {

  private SearchResultFile sFile = new SearchResultFile();

  private boolean isDownLoad = false;

  private String fileChara;

  public AutoDownLoad(String fileChara) {
   this.fileChara = fileChara;//下载文件的文件名特征
  }

  public void setDownLoad(boolean isDownLoad) {
   this.isDownLoad = isDownLoad;
  }

  public boolean getDownLoad() {
   return isDownLoad;
  }

  public void run() {

   Vector<File> downLoadFiles = null;

   ParseTxtContent ptContent = new ParseTxtContent();
           
   SimpleDateFormat sdf = new SimpleDateFormat("",Locale.SIMPLIFIED_CHINESE);
   
   String downloadTime;
   
   long updateTimes=Integer.parseInt(selItem.substring(0,2))*60000;//更新时间间隔(毫秒为单位)
   
   int i=1;
   while (isDownLoad) {
    try {
     sdf.applyPattern("yyyy年MM月dd日");
     downLoadFiles = sFile.getResContentFile(fileChara);
     String spath1;
     String res;
     String resumePublishedTime;
     downloadTime = sdf.format(new Date());
     File dir=new File(resumeStorePath+downloadTime);
     if(!dir.exists())
      dir.mkdir();//按照时间简历文件夹并保存下载的简历
      
     for (File file : downLoadFiles) {
      sdf.applyPattern("yyyy-MM-dd-HH");
      resumePublishedTime = sdf.format(file.lastModified());
      spath1 = dir.getPath()+"//简历"+resumePublishedTime+"-"+file.lastModified()
        + ".htm";
      InputStream is = new FileInputStream(file);
      String readRes = ptContent.readToNormalBuffer(is);
      is.close();
      StringBuffer buffer = new StringBuffer();
      int searchContentSIndex = readRes.indexOf("<div id");
      int searchContentEIndex = readRes
        .indexOf("</TABLE></div>");

      buffer.append("<br><br>"
        + readRes.substring(searchContentSIndex,
          searchContentEIndex + 14));

      res = buffer.toString();
      if (res.contains("IMG SRC=/"http://")) {
       res = res
         .replace("IMG SRC=/"http://", "IMG SRC=/"");
      }
      if (res.contains("img src=/"http://")) {
       res = res
         .replace("img src=/"http://", "img src=/"");
      }
      res = res.replace("/n", "/r/n");// 注意改成/r/n
      buffer = null;
      File outPutFile = new File(spath1);
      if (outPutFile.exists())
       outPutFile.delete();
      ptContent.writeFromBuffer(res, new FileOutputStream(
        outPutFile));
      sdf.applyPattern("yyyy年MM月dd日_HH时mm分ss秒");
      String finishTime = sdf.format(new Date());
  
      String viewText=i+". 下载完成, 并保存到"+outPutFile.getAbsolutePath()+"      简历发布时间: "+resumePublishedTime+"      下载完成时间: "+finishTime+"/n/n/n";
      totalResumeNum++;
      sTimePanel.setNumLabelText(""+totalResumeNum);
      viewTextArea.append(viewText); 
      i++;
      Thread.sleep(200);
     }
     Thread.sleep(updateTimes);
     //int fileCollectionSize=downLoadFiles.size();

     //for(int k=0;k<fileCollectionSize;k++)//删除临时文件夹下面下载过的文件
     //{
      //downLoadFiles.elementAt(k).delete();
     //}


    } catch (Exception e) {
     e.printStackTrace();
     JOptionPane.showMessageDialog(null, "下载出错,请检查出错原因!", "出错",
       JOptionPane.ERROR_MESSAGE);
    }
   }
  }
 }

}
 

原创粉丝点击