JFrame显示指定类型的文件

代码:

package unitTwelve;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 描述:显示指定类型的文件
 * 作者: ChengShanyunduo
 * 创建日期: 2017-07-07
 * 修改记录:
 */
public class FilesList extends JFrame{
    JFrame jFrame = new JFrame();
    JPanel jPanel1 = new JPanel();
    JPanel jPanel2 = new JPanel();
    JLabel label = new JLabel("输入文件扩展名:");
    JTextField jTextField = new JTextField();
    JButton button = new JButton("选择文件夹");
    String[] columnNames = {"文件名","文件大小","修改时间"};
    String[][]data=new String[0][3];
    DefaultTableModel model = new DefaultTableModel(data,columnNames);
    JTable jTable = new JTable(model);
    JScrollPane scroll = new JScrollPane(jTable);
    FilesList(){
        jPanel1.setLayout(new GridLayout(1,3));
        jPanel1.setSize(450,30);
        jTextField.setSize(150,30);

        jPanel1.add(label);
        jPanel1.add(jTextField);
        jPanel1.add(button);
        //监听
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        jFrame.setLayout(new FlowLayout());
        jFrame.add(jPanel1);
        jFrame.add(scroll);

        jFrame.setSize(500,500);
        jFrame.setVisible(true);
    }


    protected void do_button_actionPerformed(ActionEvent e){
        //获取text中内容
        final String fileType = jTextField.getText();
        //如果为空。弹出提醒
        if (fileType.isEmpty()){
            JOptionPane.showMessageDialog(this,"请输入文件类型","",JOptionPane.WARNING_MESSAGE);
            return;
        }
        //文件选择器
        JFileChooser chooser = new JFileChooser();
        //只能选择文件夹
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //禁止选择多个文件夹
        chooser.setMultiSelectionEnabled(false);
        //打开文件选择器
        int result = chooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION){
            //获得相同类型的文件
            File[] listFiles = chooser.getSelectedFile().listFiles(new FileFilter(){
                @Override
                public boolean accept(File pathname){
                    if (pathname.getName().endsWith(fileType)){
                        return true;
                    }else{
                        return false;
                    }
                }
            });
            DefaultTableModel model = (DefaultTableModel) jTable.getModel();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //遍历File数组
            for (File file : listFiles){
                String name = file.getName();
                long size = file.length();
                String modifyDate = format.format(new Date(file.lastModified()));
                model.addRow(new String[]{name,""+size,modifyDate});
            }
            jTable.setModel(model);
        }
    }

    public static void main(String[] args){
        FilesList a = new FilesList();
    }
}

结果: 
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页