欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

夹娃展示目录结构

程序员文章站 2022-11-23 10:52:38
夹娃展示目录结构最后是酱紫夹娃代码其实网上都有,拿下来改两下就能用,肯定有人会问我为嘛不用jtree,俩原因,一是最开始是想做成log的样子而不是权限树,可以剪切出来处理,还是蛮方便的,所以是使用的JTextArea,换成JTextPane则是因为想着加个图标,后面没找到合适的小图标,大图标布局太*了,二是jtree没有内部逻辑,扩展起来挺麻烦,而JTextPane自己写了内部逻辑,改成jtable更容易 代码片.package wendangguanli;import java.awt.B...

夹娃展示目录结构

最后是酱紫

夹娃展示目录结构

夹娃代码

其实网上都有,拿下来改两下就能用,肯定有人会问我为嘛不用jtree,俩原因,一是最开始是想做成log的样子而不是权限树,可以剪切出来处理,还是蛮方便的,所以是使用的JTextArea,换成JTextPane则是因为想着加个图标,后面没找到合适的小图标,大图标布局太*了,二是jtree没有内部逻辑,扩展起来挺麻烦,而JTextPane自己写了内部逻辑,改成jtable更容易 代码片.

package wendangguanli;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class PathTree extends JPanel {
	private static final long serialVersionUID = 1L;

	private JButton stopButton = new JButton("停止扫描");
	private JButton browseButton = new JButton("选择文件夹");
	private JTextPane pathsTextArea = new JTextPane();
	// private JCheckBox showHiddenFilesCheckbox = new JCheckBox("显示隐藏文件", false);

	public PathTree(String dir) {
		initGui(dir);
	}

	// 初始化界面
	private void initGui(String dir) {
		this.setLayout(new BorderLayout());

		JPanel buttonsPanel = new JPanel();
		buttonsPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY));
		// buttonsPanel.add(showHiddenFilesCheckbox);
		buttonsPanel.add(browseButton);
		buttonsPanel.add(stopButton);
		this.add(buttonsPanel, BorderLayout.NORTH);

		JScrollPane scroller = new JScrollPane(pathsTextArea);
		scroller.setBorder(null);
		this.add(scroller, BorderLayout.CENTER);

		JFileChooser chooser = getChooser(dir);
		FactTree ft = new FactTree(chooser, pathsTextArea);
		Thread t = new Thread(ft);
		t.start();
		browseButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int result = chooser.showOpenDialog(PathTree.this);
				if (result == JFileChooser.APPROVE_OPTION) {
					Thread t = new Thread(ft);
					t.start();
				}
			}
		});

		stopButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ft.setStopped(true);
			}
		});
	}

	private JFileChooser getChooser(String dir) {
		// 选择文件夹
		String d;
		if (dir == null) {
			d = ".";
		} else {
			dir = dir.trim();
			if (dir.equals("")) {
				d = ""; // 我的文档
			} else {
				File f = new File(dir);
				if (!f.exists()) {
					d = ".";
				} else {
					d = dir;
				}
			}
		}
		JFileChooser chooser = new JFileChooser(d);
		System.out.println("d:" + d);
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		chooser.setSelectedFile(new File(d));
		return chooser;
	}

	// 创建主窗口
	private static void createGUIAndShow(String dir) {
		JFrame frame = new JFrame("目录结构树");
		frame.setContentPane(new PathTree(dir));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
		int w = 600;
		int h = 700;
		int x = (ss.width - w) / 2;
		int y = (ss.height - h) / 2 - 40;
		x = x > 0 ? x : 0;
		y = y > 0 ? y : 0;
		frame.setBounds(x, y, w, h);
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		System.out.println("args.length:" + args.length);
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			// UIManager.setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}

		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				String dir = null;
				if (args.length > 0)
					dir = args[0];

				System.out.println("dir:" + dir);
				createGUIAndShow(dir);
				// createGUIAndShow("c:");
			}
		});
	}
}

class FactTree implements Runnable {
	private JFileChooser chooser;
	private JTextPane pathsTextArea;
	private SimpleAttributeSet attrset = new SimpleAttributeSet();
	private boolean stopped;
	private Map<Integer, String> pathIndexes = new HashMap<Integer, String>();
	// private JCheckBox showHiddenFilesCheckbox = new JCheckBox("显示隐藏文件", false);
	private FileFilter docFilter = new DocFilter(); // 文档过滤器
	private FileFilter dirFilter = new DirFilter(); // 文件夹过滤器

	public FactTree(JFileChooser chooser, JTextPane pathsTextArea) {
		super();
		this.chooser = chooser;
		this.pathsTextArea = pathsTextArea;
		// this.pathsTextArea.setEditable(false);
		StyleConstants.setFontSize(attrset, 12);
	}

	public void setStopped(boolean stopped) {
		this.stopped = stopped;
	}

	@Override
	public void run() {
		File dir = chooser.getSelectedFile();
		if (dir == null) {
			dir = new File(".");
			pathsTextArea.setText(dir.getName());
		}
		System.out.println("dir:" + dir.getAbsolutePath());
		pathsTextArea.setText("");
		stopped = false;
		walkTree(dir, 0);
	}

	// 递归遍历目录树
	private void walkTree(File dir, int level) {
		// 1. current dir path
		// 2. docs path that located in this dir
		// 3. sub dirs path

		// System.out.println(dir.getAbsolutePath() + ",00," + level);
		if (stopped) {
			return;
		}

		// System.out.println(dir.getAbsolutePath() + ",11," + level + "," + //
		// dir.isHidden() + "," + showHiddenFilesCheckbox.isSelected());
		// 如果不显示隐藏文件,则返回
		// if (dir.isHidden() && !showHiddenFilesCheckbox.isSelected()) {
		// return;
		// }
		if (!dir.canRead())
			return;

		// System.out.println(dir.getAbsolutePath() + ",22," + level);
		final StringBuilder pathBuffer = new StringBuilder(1024);

		// 访问当前目录
		pathBuffer.append(createPath(dir, level));

		// System.out.println(dir.getAbsolutePath() + ",1," + level);
		// 访问文档
		for (File doc : dir.listFiles(docFilter)) {
			if (!doc.canRead()) {
				continue;
			}
			// if (doc.isHidden() && !showHiddenFilesCheckbox.isSelected()) {
			// continue;
			// }
			// System.out.println(dir.getAbsolutePath() + ",2," + level);
			pathBuffer.append(createPath(doc, level + 1));
		}

		// System.out.println(dir.getAbsolutePath() + ",3," + level);
		// 把当前目录下的文件更新到text area中
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				// pathsTextArea.append(pathBuffer.toString());
				StyledDocument docs = (StyledDocument) pathsTextArea.getDocument();
				try {
					// Style style = docs.addStyle("StyleName", null);
					// StyleConstants.setIcon(style, new ImageIcon("c:/test/1.png"));
					// docs.insertString(docs.getLength(), "ignored text", style);
					docs.insertString(docs.getLength(), pathBuffer.toString(), attrset);
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		});

		// 递归遍历子目录
		for (File subDir : dir.listFiles(dirFilter)) {
			walkTree(subDir, level + 1);
		}
	}

	// 创建文件的路径
	public String createPath(File file, int level) {
		StringBuilder pathBuffer = new StringBuilder(128);
		// System.out.println(file.getAbsolutePath());
		// System.out.println(file.getName());
		pathBuffer.append(getPathIndex(level)).append(file.getName()).append("\n");
		
		return pathBuffer.toString();
	}

	// 创建目录的缩进
	private String getPathIndex(int level) {
		// 如果不存在,则创建
		if (pathIndexes.get(level) == null) {
			StringBuilder indexBuffer = new StringBuilder(128);
			for (int i = 0; i < level; ++i) {
				indexBuffer.append("|        ");
			}

			indexBuffer.append("|----");
			pathIndexes.put(Integer.valueOf(level), indexBuffer.toString());
			return indexBuffer.toString();
		} else {
			return pathIndexes.get(level);
		}
	}

}

class DocFilter implements FileFilter {
	@Override
	public boolean accept(File file) {
		return file.isFile();
	}
}

class DirFilter implements FileFilter {
	@Override
	public boolean accept(File file) {
		return file.isDirectory();
	}
}

MARKDOWN是要比富文本方便点

  • 呵呵

然后说下脚本

就以windows脚本为例啦,其实很简单,打好的假包朝一个固定路径一甩,然后就可以当dll来用啦 代码片.

echo off
cls

set curdir1=%~dp0
set curdriv1=%~d0

cd %curdir1%
%curdriv1%

java -jar "D:\pqy\shortcut\runinpath\wdgl.jar" %curdir1%

完鸟

以前从来不粘完整代码的,最多就是粘点snippet,今天破例一次,因为夹娃代码是在网上下下来的,只是稍微改了下而已,尊重他人劳动成果,附上他的连接https://wenda.so.com/q/1528356215218392

本文地址:https://blog.csdn.net/flyfkue/article/details/107267069