📄 windowsexplore.java
字号:
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.*;
public class WindowsExplore extends JFrame {
public DefaultTreeModel createTreeModel() { // 创建根结点, 只执行一次
/**定义widnows界面**/
Font font = new Font("Dialog", Font.PLAIN, 12);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (UIManager.get(key) instanceof Font) {
UIManager.put(key, font);
}
}
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}catch(Exception el){
System.exit(0);
} /**定义widnows界面**/
ImageIcon ICON_COMPUTER = new ImageIcon("computer.gif");
TreeNodeData mycomputer = new TreeNodeData("我的电脑");
mycomputer.addChileNodes();
return new DefaultTreeModel(mycomputer);
}
JTree tree;
JScrollPane scroll;
JSplitPane splitPane;
PopupMenuDemo popupmenudemo;
// ImageIcon leaf= new ImageIcon("WINDOC.GIF");
public WindowsExplore() {
super("windows资源管理器");
setVisible(true);
setSize(400,500);
tree = new JTree(createTreeModel()); // 返回的是根结点 DefaultTreeModel
DefaultTreeCellRenderer cellRenderer=(DefaultTreeCellRenderer)tree.getCellRenderer();
cellRenderer.setLeafIcon(new ImageIcon("WINDOC.GIF"));//设置叶节点图标
scroll=new JScrollPane(tree);//用tree构造滚动窗口
scroll.setSize(200,500);
popupmenudemo=new PopupMenuDemo();//初始化PopupMenuDemo类对象popupmenudemo
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scroll,popupmenudemo);
splitPane.setSize(800,500);//用scroll1,popupmenudemo构造拆分窗口
getContentPane().add(splitPane,BorderLayout.CENTER);
pack();
addWindowListener(new WindowAdapter() { //窗口试闭器
public void windowClosing(WindowEvent e)
{
System.exit(0); }
});
tree.addTreeExpansionListener(new TreeExpansionListener(){ // 重写JTree的事件监听器
public void treeCollapsed(TreeExpansionEvent e) {
}
public void treeExpanded(TreeExpansionEvent e) {
TreePath path = e.getPath(); // 表示到某节点的一条路径 返回已被扩张 / 折叠的值的路径
FileNode node = (FileNode)path.getLastPathComponent(); // 文件路径 <- (树)路径的最后一个组件
if( ! node.isExplored())
{ DefaultTreeModel model = (DefaultTreeModel)tree.getModel();// 返回提供数据的 TreeModel
node.explore();
model.nodeStructureChanged(node); // 当彻底改变节点的子节点和子节点的子节点时调用该方法,这将导致记入一个 treeStructureChanged 事件
}
}
});
}
public static void main(String args[]) {//主函数,测试程序;
WindowsExplore windowsexplore=new WindowsExplore();
windowsexplore.pack();
}
}
class PopupMenuDemo extends JScrollPane
{
public JPopupMenu popup;//构造监听器
public PopupMenuDemo()
{setSize(200,300);
popup=new JPopupMenu();
ActionListener menuListener=new ActionListener()
{
public void actionPerformed(ActionEvent event){
System.out.println("Popup menu item["+event.getActionCommand()+"[was pressed.");
}
};
//构建弹出菜单
JMenuItem item;
popup.add(item=new JMenuItem("打开",new ImageIcon("OPENFOLD.GIF")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item=new JMenuItem("剪贴",new ImageIcon("CUT.GIF")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item=new JMenuItem("复制",new ImageIcon("COPY.GIF")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item=new JMenuItem("粘贴",new ImageIcon("PASTE.GIF")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.addSeparator();
//设置弹出菜单的样式
popup.setBorder(new BevelBorder(BevelBorder.RAISED));
popup.addPopupMenuListener(new PopupPrintListener());
addMouseListener(new MousePopupListener());
}
//使用内部类来监听鼠标事件
class MousePopupListener extends MouseAdapter{
public void mousePressed(MouseEvent e){checkPopup(e);}
public void mouseClicked(MouseEvent e){checkPopup(e);}
public void mouseReleased(MouseEvent e){checkPopup(e);}
private void checkPopup(MouseEvent e){
if(e.isPopupTrigger()){
popup.show(PopupMenuDemo.this,e.getX(),e.getY());
}
}
}
//使用内部类来显示弹出菜单
class PopupPrintListener implements PopupMenuListener{
public void popupMenuWillBecomeVisible(PopupMenuEvent e){
System.out.println("Popup menu will be visible!");
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e){
System.out.println("Popup menu will be invisible!");
}
public void popupMenuCanceled(PopupMenuEvent e){
System.out.println("Popup menu is hidden!");
}
}
}
class FileNode extends DefaultMutableTreeNode {
private boolean explored = false;
public FileNode(File file) {
setUserObject(file);
}
public String toString() {
File file = (File)getUserObject();
String filename = file.toString();
int index = filename.lastIndexOf(File.separator);
return (index != -1 && index != filename.length()-1) ?
filename.substring(index+1) : filename;
}
public boolean getAllowsChildren()
{ return isDirectory(); }
public boolean isLeaf()
{ return !isDirectory(); }
public File getFile()
{ return (File)getUserObject(); }
public boolean isExplored()
{ return explored; }
public boolean isDirectory() {
File file = getFile(); // 获取文件名
return file.isDirectory(); // 测试当前 File 对象表示的文件是否是一个路径, 还能展开
}
public void explore() {
if(!isDirectory()) // 当前文件路径所对应是系统文件还是文件夹
return ;
if(!isExplored()) { // 该层第一次被打开, 展开当前结点的所有子元素
File file = getFile(); // 该节点的用户对象
File[] children = file.listFiles();
for(int i=0; i < children.length; ++i)
add(new FileNode(children[i])); // 子元素被实例化, 并添加到当前节点下
explored = true;
}
}
}
/* class FileNode extends DefaultMutableTreeNode {
private boolean explored = false;
public FileNode(File file) {
setUserObject(file);
allowsChildren = file.isDirectory();
}
public boolean isLeaf() {
return!isDirectory();
}
public File getFile() {
return (File) getUserObject();
}
public boolean isExplored() {
return explored;
}
public boolean isDirectory() {
return getFile().isDirectory();
}
public String toString() {
File file = (File) getUserObject();
String filename = file.toString();
int index = filename.lastIndexOf(File.separator);
return (index != -1 && index != filename.length() - 1) ?
filename.substring(index + 1) : filename;
}
//explore the node to get children.
//if already explored, do nothing to get higher performance
public void isExplore() {
if (!explored) {
explore();
}
}
//when necessary, e.g. when the files on the hard drive have been changed, force the node to explore.
public void explore() {
removeAllChildren();
File file = getFile();
File[] children = file.listFiles();
if (children == null || children.length == 0) {
return;
}
for (int i = 0; i < children.length; i++) {
if (children[i].isDirectory()) {
add(new FileNode(children[i]));
}
}
explored = true;
}
}
*/
class TreeNodeData extends DefaultMutableTreeNode
{
private File data;
private String title;
public TreeNodeData(String title)
{
super(title);
data = new File(title);
this.title = title;
}
public void addChileNodes()
{
File drivers[] = File.listRoots();
for (int i = 0; i < drivers.length; i++)
{
this.add(new FileNode(drivers[i]));
}
}
public TreeNodeData(File file,String title)
{
super(file.getPath());
data = file;
this.title = title;
File a = new File(title);
FileNode b = new FileNode(a);
add( b );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -