样式
![在这里插入图片描述](https://img-blog.csdnimg.cn/95b5a5ab94d54e6892799b1e46e77f2f.png)
代码结构
![在这里插入图片描述](https://img-blog.csdnimg.cn/0301ce33a8b04f709e10c712fa7ce4dd.png)
全部代码
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");
List<JButton> jButtonList = new ArrayList<>();
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();
}
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();
}
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+"]");
}
}
}
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;
}
}