import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class FileListFrame extends JFrame{
private static final long serialViersionUID=-615665572894071265L;
private JPanel contentPane;//容器
private JTextField chooseTextField;//文件选择
private JButton chooseButton;//文件选择按钮
private JTable table;//表格对象
private JProgressBar progressBar;//进度条
private JScrollPane scrollPane;//滚动条面板
private JButton deleteButton;
private File chooseFile;//文件duix
private JTextArea textArea;
public static void main(String[] args) {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Throwable e){
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
FileListFrame frame=new FileListFrame();
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public FileListFrame(){
setTitle("选择文件夹");
addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent arg0){
do_this_windowActivated(arg0);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭方式
setBounds(100,100,450,300);
contentPane=new JPanel();
contentPane.setBorder(new EmptyBorder(5,5,5,5));//设置布局
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0,0));
JPanel panel=new JPanel();
contentPane.add(panel,BorderLayout.NORTH);//将面板添加容器并放在北部
chooseTextField=new JTextField();
panel.add(chooseTextField);
chooseTextField.setColumns(13);
chooseButton=new JButton("\u9009\u62E9\u6587\u4EF6\u5939");
chooseButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
do_chooseButton_actionPerformed(arg0);
}
});
panel.add(chooseButton);
deleteButton=new JButton("删除");
deleteButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
do_deleteFile_actionPerformed(arg0);
}
});
panel.add(deleteButton);
progressBar=new JProgressBar();
panel.add(progressBar);//将进度条添加到面板
scrollPane=new JScrollPane();//带滚动条的面板
contentPane.add(scrollPane,BorderLayout.CENTER);
table=new JTable();
scrollPane.setViewportView(table);
}
protected void do_chooseButton_actionPerformed(ActionEvent arg0){
JFileChooser chooser=new JFileChooser();//创建文件选择器
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//限制仅能选择文件夹
chooser.setMultiSelectionEnabled(false);//禁止多从选择
int result=chooser.showOpenDialog(this);//打开文件选择器
if(result==JFileChooser.APPROVE_OPTION){
File selectDirectory=chooser.getSelectedFile();//获得选择的文件夹
chooseTextField.setText(selectDirectory.getAbsolutePath());//显示文件夹位置
final File[] files=selectDirectory.listFiles();//获得文件夹文件即子目录
final DefaultTableModel model=(DefaultTableModel)table.getModel();//获得表格模型
final SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
new Thread(){
public void run(){
for(File file:files){
if(file.isFile()){//如果是文件则向表格模型中添加数据
model.addRow(new Object[]{file.getName(),file.length(),format.format(new Date(file.lastModified()))});
try{
Thread.sleep(1000);//线程休眠
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}.start();
}
}
protected void do_deleteFile_actionPerformed(ActionEvent arg0){
JFileChooser chooser=new JFileChooser();//创建文件选择器
int result=chooser.showOpenDialog(this);
if(result==JFileChooser.APPROVE_OPTION){
File selectDirectory=chooser.getSelectedFile();//获得选择的文件夹
chooseTextField.setText(selectDirectory.getAbsolutePath());//显示文件夹位置
final File[] files=selectDirectory.listFiles();//获得文件夹文件即子目录
final DefaultTableModel model=(DefaultTableModel)table.getModel();//获得表格模型
final SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
new Thread(){
public void run(){
for(File file:files){
if(file.isFile()){//如果是文件则向表格模型中添加数据
deleteFile(chooseFile,textArea);
}
}
}
}.start();
}
}
private void deleteFile(File root,JTextArea textArea){
if(root.isFile()){
root.delete();
textArea.append(root.getAbsolutePath()+"\n\r");
}else{
File[] files=root.listFiles();
for(File file:files){
if(file.isFile()){
file.delete();
textArea.append(file.getAbsolutePath()+"\n");
}else{
deleteFile(file,textArea);
}
}
root.delete();
}
}
protected void do_this_windowActivated(WindowEvent arg0){
DefaultTableModel model=(DefaultTableModel)table.getModel();
model.setColumnIdentifiers(new Object[]{"序号","文件名","属性"});
}
}
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class FileListFrame extends JFrame{
private static final long serialViersionUID=-615665572894071265L;
private JPanel contentPane;//容器
private JTextField chooseTextField;//文件选择
private JButton chooseButton;//文件选择按钮
private JTable table;//表格对象
private JProgressBar progressBar;//进度条
private JScrollPane scrollPane;//滚动条面板
private JButton deleteButton;
private File chooseFile;//文件duix
private JTextArea textArea;
public static void main(String[] args) {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Throwable e){
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
FileListFrame frame=new FileListFrame();
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public FileListFrame(){
setTitle("选择文件夹");
addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent arg0){
do_this_windowActivated(arg0);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭方式
setBounds(100,100,450,300);
contentPane=new JPanel();
contentPane.setBorder(new EmptyBorder(5,5,5,5));//设置布局
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0,0));
JPanel panel=new JPanel();
contentPane.add(panel,BorderLayout.NORTH);//将面板添加容器并放在北部
chooseTextField=new JTextField();
panel.add(chooseTextField);
chooseTextField.setColumns(13);
chooseButton=new JButton("\u9009\u62E9\u6587\u4EF6\u5939");
chooseButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
do_chooseButton_actionPerformed(arg0);
}
});
panel.add(chooseButton);
deleteButton=new JButton("删除");
deleteButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
do_deleteFile_actionPerformed(arg0);
}
});
panel.add(deleteButton);
progressBar=new JProgressBar();
panel.add(progressBar);//将进度条添加到面板
scrollPane=new JScrollPane();//带滚动条的面板
contentPane.add(scrollPane,BorderLayout.CENTER);
table=new JTable();
scrollPane.setViewportView(table);
}
protected void do_chooseButton_actionPerformed(ActionEvent arg0){
JFileChooser chooser=new JFileChooser();//创建文件选择器
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//限制仅能选择文件夹
chooser.setMultiSelectionEnabled(false);//禁止多从选择
int result=chooser.showOpenDialog(this);//打开文件选择器
if(result==JFileChooser.APPROVE_OPTION){
File selectDirectory=chooser.getSelectedFile();//获得选择的文件夹
chooseTextField.setText(selectDirectory.getAbsolutePath());//显示文件夹位置
final File[] files=selectDirectory.listFiles();//获得文件夹文件即子目录
final DefaultTableModel model=(DefaultTableModel)table.getModel();//获得表格模型
final SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
new Thread(){
public void run(){
for(File file:files){
if(file.isFile()){//如果是文件则向表格模型中添加数据
model.addRow(new Object[]{file.getName(),file.length(),format.format(new Date(file.lastModified()))});
try{
Thread.sleep(1000);//线程休眠
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}.start();
}
}
protected void do_deleteFile_actionPerformed(ActionEvent arg0){
JFileChooser chooser=new JFileChooser();//创建文件选择器
int result=chooser.showOpenDialog(this);
if(result==JFileChooser.APPROVE_OPTION){
File selectDirectory=chooser.getSelectedFile();//获得选择的文件夹
chooseTextField.setText(selectDirectory.getAbsolutePath());//显示文件夹位置
final File[] files=selectDirectory.listFiles();//获得文件夹文件即子目录
final DefaultTableModel model=(DefaultTableModel)table.getModel();//获得表格模型
final SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
new Thread(){
public void run(){
for(File file:files){
if(file.isFile()){//如果是文件则向表格模型中添加数据
deleteFile(chooseFile,textArea);
}
}
}
}.start();
}
}
private void deleteFile(File root,JTextArea textArea){
if(root.isFile()){
root.delete();
textArea.append(root.getAbsolutePath()+"\n\r");
}else{
File[] files=root.listFiles();
for(File file:files){
if(file.isFile()){
file.delete();
textArea.append(file.getAbsolutePath()+"\n");
}else{
deleteFile(file,textArea);
}
}
root.delete();
}
}
protected void do_this_windowActivated(WindowEvent arg0){
DefaultTableModel model=(DefaultTableModel)table.getModel();
model.setColumnIdentifiers(new Object[]{"序号","文件名","属性"});
}
}