Undo button to make a swing and implement an undo function

static ArrayList<File> a = new ArrayList<>();// 装文件
static ArrayList<File> b = new ArrayList<>();// 装文件夹
public static void get(File f) throws IOException {
	if (f != null) {// 先判断文件夹不为空
		if (f.isFile()) {// 判断文件夹中的元素是不是文件
			a.add(f);// 返回此抽象路径名的绝对路径名字符串 添加到集合中
		} else {// 不是文件就是文件夹目录
			b.add(f);
			File[] listFiles = f.listFiles();// 得到文件夹目录下的文件,用抽象的路径名数组表示
			if (f.length() != 0) {// 继续判断子文件夹中不为空
				for (File file : listFiles) {// 遍历数组
					get(file);// 调用方法
				}
			}

		}
	}
}
public static void main(String[] args) throws IOException {

	JFrame frame = new JFrame("我的JFrame");
	JButton button = new JButton("撤销按钮");
	frame.setSize(100, 100);
	frame.setLocationRelativeTo(null);
	frame.add(button);
	button.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			try {
				get(file);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	});
	frame.setVisible(true);
	for (File file3 : b) {
		file3.mkdir();
	}
	for (File file4 : a) {
		file4.createNewFile();
	}
	System.out.println("撤销成功");
}
Published 14 original articles · won praise 1 · views 235

Guess you like

Origin blog.csdn.net/weixin_45061669/article/details/104830214