package com.testswing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.util.FileUtil;
public class TestSwingOne extends JFrame implements ActionListener {
private int jfWidth = 800;
private int jfheight = 400;
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private int x = (screenSize.width - jfWidth) / 2;
private int y = (screenSize.height - jfheight) / 2;
// 第一行开始的X,Y
private int firstRowX = 70;
private int firstRowY = 70;
// 行和行的间隔
private int Space = 20;
// 第一个控件的位置和大小
private int firstX = firstRowX;
private int firstY = firstRowY;
private int firstW = 150;
private int firstH = 30;
// 第二个控件的位置和大小
private int secX = 210;
private int secY = firstRowY;
private int secW = 350;
private int secH = 30;
// 第三个控件的位置和大小
private int thirdX = 570;
private int thirdY = firstRowY;
private int thirdW = 80;
private int thirdH = 30;
JTextField selectText = null;
JTextField selectText2 = null;
JTextField selectText3 = null;
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new TestSwingOne();
}
public TestSwingOne() {
this.setLayout(null);
this.setBounds(x, y, jfWidth, jfheight);
this.setVisible(true);
this.setTitle("选择类型文件到指定目录工具");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel selectFile = new JLabel("要查找的文件夹:");
this.add(selectFile);
selectFile.setBounds(firstX, firstY, firstW, firstH);
// selectFile.setBackground(Color.blue);
selectFile.setOpaque(true);
selectFile.setFont(new Font("Dialog", 1, 18));
selectText = new JTextField(" 请选择要查找的文件夹路径");
this.add(selectText);
selectText.setBounds(secX, secY, secW, secH);
selectText.setEnabled(false);
selectText.setFont(new Font("Dialog", 0, 16));
JButton open = new JButton("选择...");
this.add(open);
open.setBounds(thirdX, thirdY, thirdW, thirdH);
// 下面为第二行的控件
JLabel selectFile2 = new JLabel("文件类型:");
this.add(selectFile2);
selectFile2.setBounds(firstX, firstY + firstH + Space, firstW, firstH);
// selectFile.setBackground(Color.blue);
selectFile2.setOpaque(true);
selectFile2.setFont(new Font("Dialog", 1, 18));
selectText2 = new JTextField(" 请输入要查找的文件类型,如:.java;.html");
this.add(selectText2);
selectText2.setBounds(secX, firstY + firstH + Space, secW, secH);
selectText2.setEnabled(true);
selectText2.setFont(new Font("Dialog", 0, 16));
JLabel jl3 = new JLabel("以英文分号分开");
this.add(jl3);
jl3.setBounds(thirdX, firstY + firstH + Space, firstW, firstH);
jl3.setOpaque(true);
jl3.setFont(new Font("Dialog", 0, 16));
// 下面为第三行的控件
JLabel selectFile3 = new JLabel("存放结果文件夹:");
this.add(selectFile3);
selectFile3.setBounds(firstX, firstY + firstH + Space + secH + Space, firstW, firstH);
// selectFile3.setBackground(Color.blue);
selectFile3.setOpaque(true);
selectFile3.setFont(new Font("Dialog", 1, 18));
selectText3 = new JTextField(" 请选择存放结果的文件夹路径");
this.add(selectText3);
selectText3.setBounds(secX, firstY + firstH + Space + secH + Space, secW, secH);
selectText3.setEnabled(false);
selectText3.setFont(new Font("Dialog", 0, 16));
JButton open2 = new JButton("选择...");
this.add(open2);
open2.setBounds(thirdX, firstY + firstH + Space + secH + Space, thirdW, thirdH);
JButton ok = new JButton("开始");
this.add(ok);
ok.setBounds(thirdX, firstY + firstH + Space + secH + Space + thirdH + Space, thirdW, thirdH);
open.addActionListener(new select1());
open2.addActionListener(new select2());
ok.addActionListener(this);
}
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent e) {
String fileType = selectText2.getText();
if(fileType!=null && !fileType.equals("")) {
if(fileType.startsWith(".")) {
String[] fileT = fileType.split(";");
FileUtil fu = new FileUtil();
//要查找的文件路径
String filePath = selectText.getText();
try {
@SuppressWarnings("static-access")
ArrayList<File> fileList = fu.getFiles(filePath);
//存放结果的文件夹路径
String fileGoTo = selectText3.getText();
System.out.println(fileGoTo);
for (File file : fileList) {
String f = file.getAbsolutePath();
if(f.contains(".")) {
String prefix = f.substring(f.lastIndexOf("."));
if(fileIsBelongTo(fileT,prefix)) {
fu.fileCopy(file, fileGoTo);
}
}
}
System.out.println("该次查找结束!");
JOptionPane.showMessageDialog(null, "该次查找结束!");
} catch (Exception e1) {
e1.printStackTrace();
}
}else {
JOptionPane.showMessageDialog(null, "请输入正确的文件类型!", "WARNING",JOptionPane.WARNING_MESSAGE);
}
}
System.out.println(fileType);
}
private class select1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
selectFile(selectText);
}
}
private class select2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
selectFile(selectText3);
}
}
private void selectFile(JTextField jt) {
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jfc.showDialog(new JLabel(), "选择");
File file = jfc.getSelectedFile();
if (file != null) {
if (file.isDirectory()) {
System.out.println("文件夹:" + file.getAbsolutePath());
jt.setText(file.getAbsolutePath());
} else if (file.isFile()) {
System.out.println("文件:" + file.getAbsolutePath());
}
System.out.println(jfc.getSelectedFile().getName());
}
}
//判断文件是否属于要求的文件类型
private boolean fileIsBelongTo(String[] fileTypes,String file) {
for (String oneFile : fileTypes) {
if(file.equals(oneFile)) {
return true;
}
}
return false;
}
}