一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java實現的Windows資源管理器實例

Java實現的Windows資源管理器實例

2019-12-27 13:08華宰 JAVA教程

這篇文章主要介紹了Java實現的Windows資源管理器,實例分析了基于java實現windows資源管理器的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Java實現的Windows資源管理器。分享給大家供大家參考。具體如下:

FileTree.java文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// FileTree.java
/***********************************************************
 *  Author: Jason
 *   email: [email protected]
 * CSDN blog: http://blog.csdn.net/UnAgain/
 ***********************************************************/
package tl.exercise.swing;
import java.awt.Component;
import java.io.File;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class FileTree extends JTree {
  static final long serialVersionUID = 0;
  private FileList theList;
  public FileTree(FileList list) {
    theList = list;
    setModel(new FileSystemModel(new FolderNode()));
    this.setCellRenderer(new FolderRenderer());
    addTreeSelectionListener(new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent tse) {
      }
    });  
    this.setSelectionRow(0);
  }
  public void fireValueChanged(TreeSelectionEvent tse) {
    TreePath tp = tse.getNewLeadSelectionPath();
    Object o = tp.getLastPathComponent();
    // theList.fireTreeSelectionChanged((PathNode)o);
    theList.fireTreeSelectionChanged((FolderNode) o);
  }
  public void fireTreeCollapsed(TreePath path) {
    super.fireTreeCollapsed(path);
    TreePath curpath = getSelectionPath();
    if (path.isDescendant(curpath)) {
      setSelectionPath(path);
    }
  }
  public void fireTreeWillExpand(TreePath path) {
    System.out.println("Path will expand is " + path);
  }
  public void fireTreeWillCollapse(TreePath path) {
    System.out.println("Path will collapse is " + path);
  }
  class ExpansionListener implements TreeExpansionListener {
    FileTree tree;
    public ExpansionListener(FileTree ft) {
      tree = ft;
    }
    public void treeCollapsed(TreeExpansionEvent tee) {
    }
    public void treeExpanded(TreeExpansionEvent tee) {
    }
  }
}
class FileSystemModel implements TreeModel {
  I_fileSystem theRoot;
  char fileType = I_fileSystem.DIRECTORY;
  public FileSystemModel(I_fileSystem fs) {
    theRoot = fs;
  }
  public Object getRoot() {
    return theRoot;
  }
  public Object getChild(Object parent, int index) {
    return ((I_fileSystem) parent).getChild(fileType, index);
  }
  public int getChildCount(Object parent) {
    return ((I_fileSystem) parent).getChildCount(fileType);
  }
  public boolean isLeaf(Object node) {
    return ((I_fileSystem) node).isLeaf(fileType);
  }
  public int getIndexOfChild(Object parent, Object child) {
    return ((I_fileSystem) parent).getIndexOfChild(fileType, child);
  }
  public void valueForPathChanged(TreePath path, Object newValue) {
  }
  public void addTreeModelListener(TreeModelListener l) {
  }
  public void removeTreeModelListener(TreeModelListener l) {
  }
}
interface I_fileSystem {
  final public static char DIRECTORY = 'D';
  final public static char FILE = 'F';
  final public static char ALL = 'A';
  public Icon getIcon();
  public I_fileSystem getChild(char fileType, int index);
  public int getChildCount(char fileType);
  public boolean isLeaf(char fileType);
  public int getIndexOfChild(char fileType, Object child);
}
/**
 * A data model for a JTree. This model explorer windows file system directly.
 *
 * <p>
 * Perhaps there is a fatal bug with this design. For speed, each of instances
 * of this model contains file objects of subdirectory, up to now, there isn't
 * any method to release them until program be end. I'm afraid that the memory
 * would be full of if the file system is large enough and JVM memery size
 * setted too small.
 *
 * <p>
 * I won't pay more attention to solve it. it isn't goal of current a exercise.
 *
 * @author Jason
 */
class FolderNode implements I_fileSystem {
  // private static FolderNode theRoot;
  private static FileSystemView fsView;
  private static boolean showHiden = true;;
  private File theFile;
  private Vector<File> all = new Vector<File>();
  private Vector<File> folder = new Vector<File>();
  /**
   * set that whether apply hiden file.
   *
   * @param ifshow
   */
  public void setShowHiden(boolean ifshow) {
    showHiden = ifshow;
  }
  public Icon getIcon() {
    return fsView.getSystemIcon(theFile);
  }
  public String toString() {
    // return fsView.
    return fsView.getSystemDisplayName(theFile);
  }
  /**
   * create a root node. by default, it should be the DeskTop in window file
   * system.
   *
   */
  public FolderNode() {
    fsView = FileSystemView.getFileSystemView();
    theFile = fsView.getHomeDirectory();
    prepareChildren();
  }
  private void prepareChildren() {
   File[] files = fsView.getFiles(theFile, showHiden);
    for (int i = 0; i < files.length; i++) {
      all.add(files[i]);
      if (files[i].isDirectory()
          && !files[i].toString().toLowerCase().endsWith(".lnk")) {
        folder.add(files[i]);
      }
    }
  }
  private FolderNode(File file) {
    theFile = file;
    prepareChildren();
  }
  public FolderNode getChild(char fileType, int index) {
    if (I_fileSystem.DIRECTORY == fileType) {
      return new FolderNode(folder.get(index));
    } else if (I_fileSystem.ALL == fileType) {
      return new FolderNode(all.get(index));
    } else if (I_fileSystem.FILE == fileType) {
      return null;
    } else {
      return null;
    }
  }
  public int getChildCount(char fileType) {
    if (I_fileSystem.DIRECTORY == fileType) {
      return folder.size();
    } else if (I_fileSystem.ALL == fileType) {
      return all.size();
    } else if (I_fileSystem.FILE == fileType) {
      return -1;
    } else {
      return -1;
    }
  }
  public boolean isLeaf(char fileType) {
    if (I_fileSystem.DIRECTORY == fileType) {
      return folder.size() == 0;
    } else if (I_fileSystem.ALL == fileType) {
      return all.size() == 0;
    } else if (I_fileSystem.FILE == fileType) {
      return true;
    } else {
      return true;
    }
  }
  public int getIndexOfChild(char fileType, Object child) {
    if (child instanceof FolderNode) {
      if (I_fileSystem.DIRECTORY == fileType) {
        return folder.indexOf(((FolderNode) child).theFile);
      } else if (I_fileSystem.ALL == fileType) {
        return all.indexOf(((FolderNode) child).theFile);
      } else if (I_fileSystem.FILE == fileType) {
        return -1;
      } else {
        return -1;
      }
    } else {
      return -1;
    }
  }
}
class FolderRenderer extends DefaultTreeCellRenderer {
  private static final long serialVersionUID = 1L;
  public Component getTreeCellRendererComponent(JTree tree, Object value,
      boolean sel, boolean expanded, boolean leaf, int row,
      boolean hasFocus) {
    I_fileSystem node = (I_fileSystem) value;
    Icon icon = node.getIcon();
    setLeafIcon(icon);
    setOpenIcon(icon);
    setClosedIcon(icon);
    return super.getTreeCellRendererComponent(tree, value, sel, expanded,
       leaf, row, hasFocus);
  }
}

希望本文所述對大家的java程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99热99re | 好大好硬好深好爽想要吃奶 | 91国语自产拍在线观看 | 精品在线免费观看视频 | 扒开女人下面使劲桶屁股动漫 | 呜呜别塞了啊抽插 | 亚洲v日韩v欧美在线观看 | 日韩精品成人a在线观看 | 男同精品视频免费观看网站 | 狠狠撸在线播放 | 视频一区国产精戏刘婷 | 亚洲国产天堂久久综合网站 | 日本一区二区不卡久久入口 | 日本美女视频韩国视频网站免费 | 小sao货水好多真紧h的视频 | 日本福利片国产午夜久久 | 日产精品一卡2卡三卡4乱码久久 | 国产一级精品高清一级毛片 | 亚洲国产精品线在线观看 | 全是女性放屁角色的手游 | 麻生希无码 | 无人区尖叫之夜美女姐姐视频 | 2021国产麻豆剧传媒新片 | 亚洲va天堂va国产va久久 | 双性np玩烂了np欲之国的太子 | 扒开双腿羞辱调教play视频 | 日产精品卡一卡2卡三卡乱码工厂 | 国产精品热久久毛片 | 亚飞与亚基高清国语在线观看 | 久久青青草原精品国产软件 | 国产一区二区三区福利 | 青草国内精品视频在线观看 | 成人免费体验区福利云点播 | bt国产 | 国产成人综合亚洲一区 | 草莓视频在线免费观看 | 国内小情侣一二三区在线视频 | 国产精品久久久久久久午夜片 | 无套啪啪 | 午夜国产精品影院在线观看 | 亚洲狼人香蕉香蕉在线28 |