Javafx简单实现【我的电脑资源管理器】效果
程序员文章站
2024-03-06 17:00:08
本文实例讲述了javafx简单实现【我的电脑资源管理器】效果。分享给大家供大家参考。具体如下:
1. java代码:
package ttt;
import...
本文实例讲述了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程序设计有所帮助。