Java 创建一个快捷窗口 用于监控文件夹与打开文件夹

这个Java程序创建了一个窗口,展示从配置文件读取的文件夹快捷方式。每个快捷方式是一个按钮,显示文件夹名称和包含文件的数量。程序使用多线程动态更新按钮上的文件计数,提供了一种便捷的方式来监控文件夹内容的变化。

样式

在这里插入图片描述

代码结构

在这里插入图片描述

全部代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import java.util.*;
import java.util.List;

public class Main extends JFrame {
    //设置内边距
    Insets insets = new Insets(0, 0, 0, 0);

    public Main() {
        //标题
        setTitle("快捷大开");
        //窗口尺寸
        setSize(350, 150);
        //布局方式  流式布局
        setLayout(new FlowLayout(FlowLayout.LEFT));
        //读取配置文件
        Map<String, String> map = readFile("info/txt/paths.txt");
        //button列表用于存放按钮 然后动态设置他的数量
        List<JButton> jButtonList = new ArrayList<>();
        //遍历Map 配置文件
        for (Map.Entry<String, String> entry : map.entrySet()) {
            //等号前面的  名称
            String mapKey = entry.getKey();
            //等号后面的  路径
            String mapValue = entry.getValue();
            //创建一个按钮
            JButton jButton = new JButton(mapKey);
            //创建一个字体
            Font font = new Font("宋体", Font.PLAIN, 12);
            //设置字体
            jButton.setFont(font);
            //设置内边距
            jButton.setMargin(insets);
            //给按钮一个点击事件
            jButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        //打开对应文件夹
                        Desktop.getDesktop().open(new File(mapValue));
                    } catch (Exception e1) {
                        throw new RuntimeException(e1);
                    }
                }
            });
            //把按钮添加到窗口里面
            this.add(jButton);
            //把按钮添加 列表 用于动态设置文件夹数量
            jButtonList.add(jButton);
        }
        //显示窗口
        setVisible(true);
        //关闭程序的时候多线程也会退出
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //后台线程动态设置窗口按钮的数据
        thread(jButtonList,map);
    }

    public static void main(String[] args) {
        //启动程序
        new Main();
    }

    /**
     * 多线程
     * @param jButtonList
     * @param map
     */
    public void thread(List<JButton> jButtonList,Map<String, String> map) {
        new Thread(){//创建一个匿名内部类
            public void run(){
                while (true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //动态设置按钮内容
                    setButtonName(jButtonList,map);
                }

            }

        }.start();//开启多线程
    }

    /**
     * 设置按钮内容
     * @param jButtonList
     * @param map
     */
    private void setButtonName(List<JButton> jButtonList, Map<String,String> map) {
        for(JButton jButton:jButtonList){
            //获取按钮上面文字
            String text = jButton.getText();
            //如果有 [ 就提取 [前面的内容
            if(text.indexOf("[")!=-1){
                text = text.substring(0,text.indexOf("["));
            }
            //通过按钮文字获取路径
            String s = map.get(text);
            if (s!=null) {
                //通过路径 获取该路径下有多少个文件
                File[] files = new File(s).listFiles();
                //设置按钮的文字
                jButton.setText(text+"["+files.length+"]");
            }

        }
    }

    /**
     * 读取记事本文件
     * @param filename 记事本路径
     * @return 返回一个map
     */
    public static Map<String, String> readFile(String filename) {
        try {
            Map<String, String> map = new HashMap<>();
            File file = new File(filename);
            Scanner sc = new Scanner(file);
            while (sc.hasNextLine()) {
                String s = sc.nextLine();
                if (s != null && s.indexOf("=") != -1) {
                    map.put(s.split("=")[0].trim(), s.split("=")[1].trim());
                }
            }
            return map;

        } catch (Exception e) {

        }
        return null;

    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值