java javafx 图书管理系统_Javafx简单实现【我的电脑资源管理器】效果

本文实例讲述了Javafx简单实现【我的电脑资源管理器】效果。分享给大家供大家参考。具体如下:

1. java代码:

package ttt;

import java.io.File;

import javafx.application.Application;

import javafx.beans.value.ChangeListener;

import javafx.beans.value.ObservableValue;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.scene.Scene;

import javafx.scene.control.TableColumn;

import javafx.scene.control.TableView;

import javafx.scene.control.TreeItem;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.Priority;

import javafx.stage.Stage;

import javafx.scene.control.TreeView;

import javafx.scene.control.cell.PropertyValueFactory;

public class TreeViews extends Application {

public static ObservableList data = FXCollections.observableArrayList();

public static void main(String[] args) {

launch(args);

}

@Override

public void start(Stage primaryStage) {

primaryStage.setTitle("Javafx 实现\"我的电脑\"资源管理器");

TreeItem rootItem = new TreeItem<>(new File(System.getenv("COMPUTERNAME")));

for (File file : File.listRoots()) {

FileTreeItem rootsitem = new FileTreeItem(file);

rootItem.getChildren().add(rootsitem);

}

TreeView tree = new TreeView(rootItem);

HBox root = new HBox();

TableView tableView = new TableView<>(data);

TableColumn firstColumn = new TableColumn<>("文件");

firstColumn.setCellValueFactory(new PropertyValueFactory("FileName"));

firstColumn.setPrefWidth(120);

TableColumn secondColumn = new TableColumn<>("类型");

secondColumn.setCellValueFactory(new PropertyValueFactory("type"));

secondColumn.setPrefWidth(120);

TableColumn thirdColumn = new TableColumn<>("最后修改");

thirdColumn.setCellValueFactory(new PropertyValueFactory("LastModified"));

thirdColumn.setPrefWidth(200);

tableView.getColumns().setAll(firstColumn, secondColumn, thirdColumn);

HBox.setHgrow(tree, Priority.ALWAYS);

HBox.setHgrow(tableView, Priority.ALWAYS);

root.getChildren().addAll(tree,tableView);

tree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener>() {

@Override

public void changed(ObservableValue extends TreeItem> observable, TreeItem oldValue,

TreeItem newValue) {

ObservableList> treelist = newValue.getChildren();

ObservableList tablelist = FXCollections.observableArrayList();

for (TreeItem item : treelist) {

FileDetail filedetail = new FileDetail(item.getValue());

tablelist.add(filedetail);

}

data.setAll(tablelist);

}

});

primaryStage.setScene(new Scene(root));

primaryStage.setHeight(600);

primaryStage.show();

}

}

2. java代码:

package ttt;

import java.io.File;

import java.text.SimpleDateFormat;

import java.util.Date;

public class FileDetail {

private String FileName;

private String LastModified;

private boolean isFile;

private boolean isFolder;

private boolean exists;

private String type;

private long length;

private SimpleDateFormat fmt;

public FileDetail(File file) {

isFile = file.isFile();

isFolder = file.isDirectory();

exists = file.exists();

if (exists) {

this.FileName = file.getName();

fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");

Date date = new Date(file.lastModified());

this.LastModified = fmt.format(date);

this.length = file.length();

if (isFolder) {

this.type = "Folder";

} else

this.type = String.valueOf(this.length / (long) 1024) + "KB";

}

}

public String getFileName() {

return FileName;

}

public void setFileName(String fileName) {

FileName = fileName;

}

public String getLastModified() {

return LastModified;

}

public void setLastModified(String lastModified) {

LastModified = lastModified;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public long getLength() {

return length;

}

public void setLength(long length) {

this.length = length;

}

}

3. java代码:

package ttt;

import java.io.File;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.scene.control.TreeItem;

public class FileTreeItem extends TreeItem {

private boolean isLeaf;

private boolean isFirstTimeChildren = true;

private boolean isFirstTimeLeaf = true;

public FileTreeItem(File file) {

super(file);

}

@Override

public ObservableList> getChildren() {

if (isFirstTimeChildren) {

isFirstTimeChildren = false;

super.getChildren().setAll(buildChildren(this));

}

return super.getChildren();

}

@Override

public boolean isLeaf() {

if (isFirstTimeLeaf) {

isFirstTimeLeaf = false;

File f = (File) getValue();

isLeaf = f.isFile();

}

return isLeaf;

}

private ObservableList> buildChildren(TreeItem TreeItem) {

File f = TreeItem.getValue();

if (f != null && f.isDirectory()) {

File[] files = f.listFiles();

if (files != null) {

ObservableList> children = FXCollections.observableArrayList();

for (File childFile : files) {

children.add(new FileTreeItem (childFile));

}

return children;

}

}

return FXCollections.emptyObservableList();

}

}

4. 运行效果截图:

d0898e9c78ad1759a52d26bb10dd5392.png

希望本文所述对大家的java程序设计有所帮助。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值