Skip to content

Commit 6596b0a

Browse files
committed
v1.3 界面优化
1 parent 96029ee commit 6596b0a

File tree

4 files changed

+132
-80
lines changed

4 files changed

+132
-80
lines changed

src/sample/Controller.java

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package sample;
22

33
import javafx.application.Platform;
4+
import javafx.beans.property.ReadOnlyStringWrapper;
45
import javafx.event.ActionEvent;
56
import javafx.fxml.FXML;
67
import javafx.fxml.Initializable;
7-
import javafx.scene.control.Button;
8-
import javafx.scene.control.TextField;
9-
import javafx.scene.control.TreeItem;
10-
import javafx.scene.control.TreeView;
8+
import javafx.scene.control.*;
119
import javafx.stage.DirectoryChooser;
1210
import javafx.stage.Window;
11+
import sample.domain.FileTreeItem;
1312

1413
import java.io.File;
1514
import java.io.IOException;
@@ -36,23 +35,26 @@ public class Controller implements Initializable {
3635
public TextField ignoreFileSize;
3736

3837
@FXML
39-
private TreeView<String> myTreeView;
38+
private TreeTableView<FileTreeItem> myTreeTableView;
39+
40+
TreeTableColumn<FileTreeItem, String> fileNameColumn;
41+
TreeTableColumn<FileTreeItem, String> fileSizeColumn;
4042
private Window stage;
4143

4244

4345
@Override
4446
public void initialize(URL location, ResourceBundle resources) {
4547
// TODO (don't really need to do anything here).
46-
}
47-
48-
/**
49-
* 生成目录文件树;包含文件、目录大小
50-
*
51-
* @param path 路径
52-
* @return TreeItem<String>
53-
*/
54-
private TreeItem<String> initTreeView(String path) {
55-
return initTreeView(path, 0);
48+
System.out.println("初始化文件树窗口");
49+
fileNameColumn = new TreeTableColumn<>("文件名");
50+
fileNameColumn.setPrefWidth(530);
51+
fileNameColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<FileTreeItem, String> param)
52+
-> new ReadOnlyStringWrapper(param.getValue().getValue().getName()));
53+
fileSizeColumn = new TreeTableColumn<>("文件大小");
54+
fileSizeColumn.setPrefWidth(100);
55+
fileSizeColumn.setCellValueFactory((TreeTableColumn.CellDataFeatures<FileTreeItem, String> param)
56+
-> new ReadOnlyStringWrapper(param.getValue().getValue().getSize()));
57+
myTreeTableView.getColumns().setAll(fileNameColumn, fileSizeColumn);
5658
}
5759

5860
/**
@@ -62,27 +64,17 @@ private TreeItem<String> initTreeView(String path) {
6264
* @param size 过滤小于size字节的文件
6365
* @return TreeItem<String>
6466
*/
65-
private TreeItem<String> initTreeView(String path, long size) {
66-
67+
private TreeItem<FileTreeItem> initTreeView(String path, long size) {
6768
long size0;
6869
File file = new File(path);
6970
size0 = getDirSize(file);
70-
TreeItem<String> item = new TreeItem<>(file.getName() + "\t" + sizeFormat(size0));
71+
TreeItem<FileTreeItem> item = new TreeItem<>(new FileTreeItem(file.getName(), sizeFormat(size0)));
7172
item.setExpanded(false);
7273
if (file.exists()) {
7374
File[] files = file.listFiles();
7475
if (files != null && files.length != 0) {
75-
//对文件和文件夹按名称进行排序
76-
Arrays.sort(files, (o1, o2) -> {
77-
//将文件夹与文件分开排序
78-
if (o1.isDirectory() && o2.isFile())
79-
return -1;
80-
else if (o2.isDirectory() && o1.isFile())
81-
return 1;
82-
//对字符串大写处理,使返回的拼音为小写、英文为大写,从而将英文和中文分开排序。
83-
return GetPinYin.getPinYin(o1.getName().toUpperCase()).compareTo(GetPinYin.getPinYin(o2.getName().toUpperCase()));
84-
});
85-
76+
// 对文件和文件夹按名称进行排序
77+
sortFiles(files);
8678
for (File file2 : files) {
8779
if (file2.isDirectory()) {
8880
size0 = getDirSize(file2);
@@ -92,7 +84,7 @@ else if (o2.isDirectory() && o1.isFile())
9284
} else {
9385
size0 = file2.length();
9486
if (size == 0 || size0 > size) {
95-
TreeItem<String> i2 = new TreeItem<>(file2.getName() + "\t" + sizeFormat(size0));
87+
TreeItem<FileTreeItem> i2 = new TreeItem<>(new FileTreeItem(file2.getName(), sizeFormat(size0)));
9688
item.getChildren().add(i2);
9789
}
9890
}
@@ -105,38 +97,50 @@ else if (o2.isDirectory() && o1.isFile())
10597
return item;
10698
}
10799

108-
//"生成文件树"按钮
100+
/**
101+
* "生成文件树"按钮
102+
*
103+
* @param event
104+
*/
109105
public void getDir(ActionEvent event) {
110-
//多线程
111106
new Thread(() -> {
112107
String path = myText.getText();
113-
114108
Platform.runLater(() -> myButton.setText("生成中..."));
115109
System.out.println(path);
116110
long size = Long.parseLong(ignoreFileSize.getText());
117-
TreeItem<String> item = initTreeView(path, size * 1024);
111+
TreeItem<FileTreeItem> item = initTreeView(path, size * 1024);
118112
item.setExpanded(true);
119-
//等到Application Thread空闲的时候,Platform.runLater就会自动执行队列中修改界面的工作了
113+
114+
// 等到Application Thread空闲的时候,Platform.runLater就会自动执行队列中修改界面的工作了
120115
Platform.runLater(() -> {
121-
myTreeView.setRoot(item);
116+
myTreeTableView.setRoot(item);
117+
myTreeTableView.getColumns().setAll(fileNameColumn, fileSizeColumn);
122118
myButton.setText("生成文件树");
123119
});
124120
}).start();
125121
}
126122

127-
//"选择目录"按钮:获得想要制作文件树的路径
123+
/**
124+
* "选择目录"按钮:获得想要制作文件树的路径
125+
*
126+
* @param event
127+
*/
128128
public void openDir(ActionEvent event) {
129129
DirectoryChooser directoryChooser = new DirectoryChooser();
130130
File file = directoryChooser.showDialog(null);
131-
if (file != null)
131+
if (file != null) {
132132
myText.setText(file.getPath());
133+
}
133134
}
134135

135136

136-
//"保存文件树"按钮:将层级目录保存到本地
137+
/**
138+
* "保存文件树"按钮:将层级目录保存到本地
139+
*
140+
* @param event
141+
*/
137142
public void saveDir(ActionEvent event) {
138-
//多线程
139-
new Thread(() ->{
143+
new Thread(() -> {
140144
Platform.runLater(() -> saveDir.setText("生成中..."));
141145
String path = myText.getText();
142146
long size = Long.parseLong(ignoreFileSize.getText());
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package sample.domain;
2+
3+
import javafx.beans.property.SimpleStringProperty;
4+
5+
/**
6+
* 文件数节点
7+
*
8+
* @author izumi
9+
*/
10+
public class FileTreeItem {
11+
private SimpleStringProperty name;
12+
private SimpleStringProperty size;
13+
14+
public FileTreeItem(String name, String size) {
15+
this.name = new SimpleStringProperty(name);
16+
this.size = new SimpleStringProperty(size);
17+
}
18+
19+
public String getName() {
20+
return name.get();
21+
}
22+
23+
public void setName(String name) {
24+
this.name.set(name);
25+
}
26+
27+
public String getSize() {
28+
return size.get();
29+
}
30+
31+
public void setSize(String size) {
32+
this.size.set(size);
33+
}
34+
}

src/sample/fileTool.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,7 @@ private static void saveFileTree(String path, String name, long size, int level)
4848
if (files != null && bufw != null) {
4949
if (files.length != 0) {
5050
//对文件和文件夹进行排序
51-
Arrays.sort(files, (o1, o2) -> {
52-
//将文件夹与文件分开排序
53-
if (o1.isDirectory() && o2.isFile())
54-
return -1;
55-
else if (o2.isDirectory() && o1.isFile())
56-
return 1;
57-
//对字符串大写处理,使返回的拼音为小写、英文为大写,从而将英文和中文分开排序。
58-
return GetPinYin.getPinYin(o1.getName().toUpperCase()).compareTo(GetPinYin.getPinYin(o2.getName().toUpperCase()));
59-
});
60-
51+
sortFiles(files);
6152
long size0;
6253
for (File file2 : files) {
6354

@@ -114,6 +105,19 @@ else if (o2.isDirectory() && o1.isFile())
114105
}
115106
}
116107

108+
static void sortFiles(File[] files) {
109+
Arrays.sort(files, (o1, o2) -> {
110+
// 将文件夹与文件分开排序
111+
if (o1.isDirectory() && o2.isFile()) {
112+
return -1;
113+
} else if (o2.isDirectory() && o1.isFile()) {
114+
return 1;
115+
}
116+
// 对字符串大写处理,使返回的拼音为小写、英文为大写,从而将英文和中文分开排序。
117+
return GetPinYin.getPinYin(o1.getName().toUpperCase()).compareTo(GetPinYin.getPinYin(o2.getName().toUpperCase()));
118+
});
119+
}
120+
117121
//保存目录文件和文件夹名称和大小
118122
static void saveFileTree(String path, String name) {
119123
saveFileTree(path, name, 0, 0);
@@ -124,15 +128,20 @@ static void saveFileTree(String path, String name, long size) {
124128
saveFileTree(path, name, size, 0);
125129
}
126130

127-
//获取文件夹目录大小
131+
/**
132+
* 获取文件夹目录大小
133+
*
134+
* @param file
135+
* @return
136+
*/
128137
static long getDirSize(File file) {
129138
long size = 0;
130139
if (file.exists()) {
131140
File[] files = file.listFiles();
132141
if (files != null) {
133-
if (files.length == 0)
142+
if (files.length == 0) {
134143
return 0;
135-
else {
144+
} else {
136145
for (File file1 : files) {
137146
if (file1.isDirectory()) {
138147
size = size + getDirSize(file1);
@@ -146,16 +155,24 @@ static long getDirSize(File file) {
146155
return size;
147156
}
148157

149-
//文件大小格式化
158+
/**
159+
* 文件大小格式化
160+
*
161+
* @param size 文件大小
162+
* @return
163+
*/
150164
static String sizeFormat(long size) {
151-
if (size < 1024)
152-
return size + " B";
153-
else if (size < 1024 * 1024)
154-
return String.format("%.2f", size / 1024.0) + "KB";
155-
else if (size < 1024 * 1024 * 1024)
156-
return String.format("%.2f", size / (1024 * 1024.0)) + "MB";
157-
else
158-
return String.format("%.2f", size / (1024 * 1024 * 1024.0)) + "GB";
165+
String sizeString;
166+
if (size < 1024) {
167+
sizeString = size + " B";
168+
} else if (size < 1024 * 1024) {
169+
sizeString = String.format("%.2f", size / 1024.0) + "KB";
170+
} else if (size < 1024 * 1024 * 1024) {
171+
sizeString = String.format("%.2f", size / (1024 * 1024.0)) + "MB";
172+
} else {
173+
sizeString = String.format("%.2f", size / (1024 * 1024 * 1024.0)) + "GB";
174+
}
175+
return sizeString;
159176
}
160177

161178
}

src/sample/sample.fxml

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@
33
<?import javafx.scene.control.*?>
44
<?import javafx.scene.layout.*?>
55

6-
<AnchorPane prefHeight="691.0" prefWidth="682.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
7-
fx:controller="sample.Controller">
8-
<Button fx:id="myButton" layoutX="569.0" layoutY="64.0" mnemonicParsing="false" onAction="#getDir" prefHeight="30.0"
9-
prefWidth="95.0" text="生成文件树" AnchorPane.rightAnchor="18.0" AnchorPane.topAnchor="64.0"/>
10-
<TextField fx:id="myText" layoutX="38.0" layoutY="32.0" prefHeight="30.0" prefWidth="549.0"
11-
AnchorPane.leftAnchor="18.0" AnchorPane.rightAnchor="116.0" AnchorPane.topAnchor="18.0"/>
12-
<TreeView fx:id="myTreeView" layoutX="18.0" layoutY="107.0" prefHeight="566.0" prefWidth="646.0"
13-
AnchorPane.bottomAnchor="18.0" AnchorPane.leftAnchor="18.0" AnchorPane.rightAnchor="18.0"
14-
AnchorPane.topAnchor="107.0"/>
15-
<Button fx:id="openDir" layoutX="584.0" layoutY="18.0" mnemonicParsing="false" onAction="#openDir" prefHeight="30.0"
16-
prefWidth="80.0" text="选择目录" AnchorPane.rightAnchor="18.0" AnchorPane.topAnchor="18.0"/>
17-
<TextField fx:id="ignoreFileSize" layoutX="322.0" layoutY="64.0" prefHeight="30.0" prefWidth="95.0" text="0"
18-
AnchorPane.rightAnchor="265.0" AnchorPane.topAnchor="64.0"/>
19-
<Label layoutX="217.0" layoutY="69.0" text="过滤过小文件:" AnchorPane.rightAnchor="360.0" AnchorPane.topAnchor="69.0"/>
20-
<Label layoutX="420.0" layoutY="69.0" text="KB" AnchorPane.rightAnchor="243.0" AnchorPane.topAnchor="69.0"/>
21-
<Button fx:id="saveDir" layoutX="448.0" layoutY="64.0" mnemonicParsing="false" onAction="#saveDir" prefHeight="30.0"
22-
prefWidth="95.0" text="保存文件树" AnchorPane.rightAnchor="131.0" AnchorPane.topAnchor="64.0"/>
6+
<AnchorPane prefHeight="691.0" prefWidth="682.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
7+
<Button fx:id="myButton" layoutX="569.0" layoutY="64.0" mnemonicParsing="false" onAction="#getDir" prefHeight="30.0" prefWidth="95.0" text="生成文件树" AnchorPane.rightAnchor="18.0" AnchorPane.topAnchor="64.0" />
8+
<TextField fx:id="myText" layoutX="38.0" layoutY="32.0" prefHeight="30.0" prefWidth="549.0" AnchorPane.leftAnchor="18.0" AnchorPane.rightAnchor="116.0" AnchorPane.topAnchor="18.0" />
9+
<Button fx:id="openDir" layoutX="584.0" layoutY="18.0" mnemonicParsing="false" onAction="#openDir" prefHeight="30.0" prefWidth="80.0" text="选择目录" AnchorPane.rightAnchor="18.0" AnchorPane.topAnchor="18.0" />
10+
<TextField fx:id="ignoreFileSize" layoutX="322.0" layoutY="64.0" prefHeight="30.0" prefWidth="95.0" text="0" AnchorPane.rightAnchor="265.0" AnchorPane.topAnchor="64.0" />
11+
<Label layoutX="217.0" layoutY="69.0" text="过滤过小文件:" AnchorPane.rightAnchor="360.0" AnchorPane.topAnchor="69.0" />
12+
<Label layoutX="420.0" layoutY="69.0" text="KB" AnchorPane.rightAnchor="243.0" AnchorPane.topAnchor="69.0" />
13+
<Button fx:id="saveDir" layoutX="448.0" layoutY="64.0" mnemonicParsing="false" onAction="#saveDir" prefHeight="30.0" prefWidth="95.0" text="保存文件树" AnchorPane.rightAnchor="131.0" AnchorPane.topAnchor="64.0" />
14+
<TreeTableView fx:id="myTreeTableView" layoutX="18.0" layoutY="110.0" prefHeight="200.0" prefWidth="646.0" AnchorPane.bottomAnchor="18.0" AnchorPane.leftAnchor="18.0" AnchorPane.rightAnchor="18.0" AnchorPane.topAnchor="110.0">
15+
<columns>
16+
<TreeTableColumn prefWidth="-1.0" text="C1" />
17+
<TreeTableColumn prefWidth="-1.0" text="C2" />
18+
</columns>
19+
</TreeTableView>
2320
</AnchorPane>

0 commit comments

Comments
 (0)