給你一個。一共三個類。是個資源管理器的代碼
創(chuàng)新互聯(lián)建站"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機版的企業(yè)網(wǎng)站。實現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!創(chuàng)新互聯(lián)建站具備承接各種類型的成都網(wǎng)站設(shè)計、網(wǎng)站制作項目的能力。經(jīng)過十年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評。
// FileList.java
package tl.exercise.swing;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
public class FileList
extends JList {
// PathNode theNode;
FileListModel dataModel;
static final long serialVersionUID = 10;
public FileList() {
dataModel = new FileListModel();
setModel(dataModel);
this.setCellRenderer(new MyCellRenderer());
}
public void fireTreeSelectionChanged(I_fileSystem node) {
// Vector files = node.getFiles();
// theNode = node;
dataModel.setNode(node);
updateUI();
}
}
class FileListModel implements ListModel {
FileList theList;
I_fileSystem node;
char fileType = I_fileSystem.ALL;
public void setNode(I_fileSystem node) {
this.node = node;
}
public Object getElementAt(int index) {
if (node != null) {
return ((I_fileSystem) node).getChild(fileType, index);
} else {
return null;
}
}
public int getSize() {
if (node != null) {
return ((I_fileSystem) node).getChildCount(fileType);
} else {
return 0;
}
}
public void addListDataListener(ListDataListener l) {
}
public void removeListDataListener(ListDataListener l) {
}
}
class MyCellRenderer extends JLabel implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
FolderNode node = (FolderNode) value;
setIcon(node.getIcon());
setText(value.toString());
setBackground(isSelected ? Color.BLUE.darker().darker() : Color.WHITE);
setForeground(isSelected ? Color.WHITE : Color.BLACK);
return this;
}
}
package tl.exercise.swing;
//JExplorer.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.border.BevelBorder;
public class JExplorer {
public static void main(String[] args) {
// JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new UI(frame));
frame.pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int left = (screen.width - frame.getWidth()) / 2;
int top = (screen.height - frame.getHeight()) / 2;
frame.setLocation(left, top);
frame.setVisible(true);
}
}
class UI extends JPanel {
// implements I_menuHandler{
static final long serialVersionUID = 0l;
static int LEFT_WIDTH = 200;
static int RIGHT_WIDTH = 300;
static int WINDOW_HEIGHT = 300;
JFrame frame = null;
public UI(JFrame frame) {
// EmptyBorder eb = new EmptyBorder(1,1,1,1);
this.frame = frame;
setPreferredSize(new Dimension(800, 600));
setBorder(new BevelBorder(BevelBorder.LOWERED));
setLayout(new BorderLayout());
FileList list = new FileList();
FileTree tree = new FileTree(list);
tree.setDoubleBuffered(true);
list.setDoubleBuffered(true);
JScrollPane treeView = new JScrollPane(tree);
treeView.setPreferredSize(
new Dimension(LEFT_WIDTH, WINDOW_HEIGHT));
JScrollPane listView = new JScrollPane(list);
listView.setPreferredSize(
new Dimension(RIGHT_WIDTH, WINDOW_HEIGHT));
JSplitPane pane =
new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeView,
listView);
pane.setDividerLocation(300);
pane.setDividerSize(4);
// pane.setDoubleBuffered(true);
add(pane);
}
}
package tl.exercise.swing;
//FileTree.java
/***********************************************************
* Author: Jason
* email: tl21cen@hotmail.com
* CSDN blog:
***********************************************************/
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 VectorFile all = new VectorFile();
private VectorFile folder = new VectorFile();
/**
* 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);
}
}
jeesite吧
views/models/sys/officeIndex.jsp
里面的js代碼你可以學(xué)習(xí)學(xué)習(xí),直接拿來用就好
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一個數(shù)組的值存入二叉樹中,然后進(jìn)行3種方式的遍歷
*
* 參考資料0:數(shù)據(jù)結(jié)構(gòu)(C語言版)嚴(yán)蔚敏
*
* 參考資料1:
*
* 參考資料2:
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ListNode nodeList = null;
/**
* 內(nèi)部類:節(jié)點
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newData) {
leftChild = null;
rightChild = null;
data = newData;
}
}
public void createBinTree() {
nodeList = new LinkedListNode();
// 將一個數(shù)組的值依次轉(zhuǎn)換為Node節(jié)點
for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}
// 對前l(fā)astParentIndex-1個父節(jié)點按照父節(jié)點與孩子節(jié)點的數(shù)字關(guān)系建立二叉樹
for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {
// 左孩子
nodeList.get(parentIndex).leftChild = nodeList
.get(parentIndex * 2 + 1);
// 右孩子
nodeList.get(parentIndex).rightChild = nodeList
.get(parentIndex * 2 + 2);
}
// 最后一個父節(jié)點:因為最后一個父節(jié)點可能沒有右孩子,所以單獨拿出來處理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 2 + 1);
// 右孩子,如果數(shù)組的長度為奇數(shù)才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 2 + 2);
}
}
/**
* 先序遍歷
*
* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已
*
* @param node
* 遍歷的節(jié)點
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍歷
*
* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已
*
* @param node
* 遍歷的節(jié)點
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightChild);
}
/**
* 后序遍歷
*
* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已
*
* @param node
* 遍歷的節(jié)點
*/
public static void postOrderTraverse(Node node) {
if (node == null)
return;
postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data + " ");
}
public static void main(String[] args) {
BinTreeTraverse2 binTree = new BinTreeTraverse2();
binTree.createBinTree();
// nodeList中第0個索引處的值即為根節(jié)點
Node root = nodeList.get(0);
System.out.println("先序遍歷:");
preOrderTraverse(root);
System.out.println();
System.out.println("中序遍歷:");
inOrderTraverse(root);
System.out.println();
System.out.println("后序遍歷:");
postOrderTraverse(root);
}
}