程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Javafx簡略完成【我的電腦資本治理器】後果

Javafx簡略完成【我的電腦資本治理器】後果

編輯:關於JAVA

Javafx簡略完成【我的電腦資本治理器】後果。本站提示廣大學習愛好者:(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<FileDetail> data = FXCollections.observableArrayList();
  public static void main(String[] args) {
    launch(args);
  }
  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Javafx 完成\"我的電腦\"資本治理器");
    TreeItem<File> rootItem = new TreeItem<>(new File(System.getenv("COMPUTERNAME")));
    for (File file : File.listRoots()) {
      FileTreeItem rootsitem = new FileTreeItem(file);
      rootItem.getChildren().add(rootsitem);
    }
    TreeView<File> tree = new TreeView<File>(rootItem);
    HBox root = new HBox();
    TableView<FileDetail> tableView = new TableView<>(data);
    TableColumn<FileDetail, String> firstColumn = new TableColumn<>("文件");
    firstColumn.setCellValueFactory(new PropertyValueFactory<FileDetail, String>("FileName"));
    firstColumn.setPrefWidth(120);
    TableColumn<FileDetail, String> secondColumn = new TableColumn<>("類型");
    secondColumn.setCellValueFactory(new PropertyValueFactory<FileDetail, String>("type"));
    secondColumn.setPrefWidth(120);
    TableColumn<FileDetail, String> thirdColumn = new TableColumn<>("最初修正");
    thirdColumn.setCellValueFactory(new PropertyValueFactory<FileDetail, String>("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<TreeItem<File>>() {
      @Override
      public void changed(ObservableValue<? extends TreeItem<File>> observable, TreeItem<File> oldValue,
          TreeItem<File> newValue) {
        ObservableList<TreeItem<File>> treelist = newValue.getChildren();
        ObservableList<FileDetail> tablelist = FXCollections.observableArrayList();
        for (TreeItem<File> 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<File> {
   private boolean isLeaf;
   private boolean isFirstTimeChildren = true;
   private boolean isFirstTimeLeaf = true;
  public FileTreeItem(File file) {
    super(file);
  }
  @Override
  public ObservableList<TreeItem<File>> 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<TreeItem<File>> buildChildren(TreeItem<File> TreeItem) {
  File f = TreeItem.getValue();
  if (f != null && f.isDirectory()) {
    File[] files = f.listFiles();
    if (files != null) {
      ObservableList<TreeItem<File>> children = FXCollections.observableArrayList();
      for (File childFile : files) {
        children.add(new FileTreeItem (childFile));
      }
      return children;
    }
  }
  return FXCollections.emptyObservableList();
}
}

4. 運轉後果截圖:

願望本文所述對年夜家的java法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved