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();
    }
}

结果: 
发布了55 篇原创文章 · 获赞 13 · 访问量 5万+
展开阅读全文

java在窗体里面打开任意文件

03-11

在做毕业设计,我用的是非常落伍的applet,不知道怎么在一个JFrame窗体里面打开任意的文件, package day.two.show.card; import java.awt.FileDialog; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * @author Vector */ public class FileOpen { public static void main(String[] args) { @SuppressWarnings("unused") myFileDialog dialog = new myFileDialog(); } } class myFileDialog extends Frame implements ActionListener { private static final long serialVersionUID = 1L; FileDialog fileDialog1, fileDialog2; MenuBar menuBar; Menu menu; MenuItem menuItem1, menuItem2; /** * 对组件进行初始化 */ public myFileDialog() { super("第一天,打开文件"); setSize(300, 300); setVisible(true); menuBar = new MenuBar(); menu = new Menu("文件"); menuItem1 = new MenuItem("保存文件"); menuItem2 = new MenuItem("打开文件"); menuItem1.addActionListener(this); menuItem2.addActionListener(this); menu.add(menuItem1); menu.add(menuItem2); menuBar.add(menu); setMenuBar(menuBar); fileDialog1 = new FileDialog(this, "保存文件对话框", FileDialog.SAVE); fileDialog1.setVisible(false); fileDialog2 = new FileDialog(this, "打开文本文件对话框", FileDialog.LOAD); fileDialog2.setVisible(false); fileDialog1.addWindowListener(new WindowAdapter() { // 匿名内部类 @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); fileDialog1.setVisible(false); } }); fileDialog2.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); fileDialog2.setVisible(false); } }); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); setVisible(false); System.exit(0); } }); } // 重写方法,实现事件的动作 @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == menuItem1) { fileDialog1.setVisible(true); } else if (e.getSource() == menuItem2) { fileDialog2.setVisible(true); } } } 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览