一直有很多小伙伴私信我要社团管理系统的源码,这个暂时无法分享,根据社团管理系统文章的思路完全是可以自己写出来的哦~
最近一个小伙伴私聊我需要一个java小程序,现将之前写的一个Windows资源管理器分享给大家
首先来看下运行效果
程序整体比较简单就不做赘述
这是四个新建的文档和一个文件夹,可删除,暂时不支持批量删除
可显示文件夹属性
可打开图片音乐视频等格式文件
源代码如下
package explorer;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
public class File_operations {
//文件的复制
public static void copyfile(String scrPath, String destPath) {
MyFrame frame = new MyFrame();
File scrpath1 = new File(scrPath);
File destpath1 = new File(destPath);
try{
destpath1.createNewFile();
FileInputStream in = new FileInputStream(scrpath1);
FileOutputStream out = new FileOutputStream(destpath1);
byte[] date =new byte[1024];
int rs = -1;
while(in.read(date)!=-1){
out.write(date);
}
in.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
//复制文件夹
public static void copydir(String scrPath, String destPath) {
System.out.println("树上的粘贴"+scrPath);
File scrpath1 = new File(scrPath);
File despath2 = new File(destPath);
String str2 = scrPath+File.separator;
String str3 = destPath+File.separator;
if(str3.regionMatches(false, 0, str2, 0, str2.length())){
JOptionPane.showMessageDialog(null,"不能建立在自己的下面","警告信息",JOptionPane.ERROR_MESSAGE);
return;
}
System.out.println("scrPath"+scrpath1.getPath());
System.out.println("destPath"+despath2.getParent());
FileInputStream in = null;
FileOutputStream out = null;
String[] pathlist =scrpath1.list();
try{
despath2.mkdir();
File temp=null;
for(int i=0;i<pathlist.length;i++){
if (scrPath.endsWith(File.separator)){
temp = new File(scrPath+pathlist[i]);
}else{
temp = new File(scrPath+File.separator+pathlist[i]);
}
if(temp.isFile()){
in = new FileInputStream(temp);
out =new FileOutputStream(destPath+File.separator+temp.getName().toString());
byte[] b = new byte[1024];
while(in.read(b)!=-1){
out.write(b);
}
out.flush();
out.close();
in.close();
}
if(temp.isDirectory()){
copydir(scrPath+File.separator+pathlist[i],destPath+File.separator+pathlist[i]);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
//删除文件
public static boolean deletef(String filePath){
System.out.println("删除的文件的路径"+filePath);
File df= new File(filePath);
if(!df.isFile()){//判断文件是否存在
return false;
}
if (df.delete()){
return true;
}else {
return false;
}
}
//删除目录
public static boolean deleteD(String dir){
//如果目录不以文件分隔符结尾,则自动添加文件分隔符
if (!dir.endsWith(File.separator)){
dir = dir + File.separator;
}
File dirFile = new File(dir);
//如果目录对应的文件不存在,或者不是一个文件夹,则退出
if(!dirFile.exists()||(!dirFile.isDirectory())){
System.out.println(dir + "目录不存在");
return false;
}
boolean flag = true;
//删除文件夹下的所有文件,包括子目录
File[] files = dirFile.listFiles();
for (int i = 0 ;i<files.length;i++){
//删除子文件
if(files[i].isFile()){
flag = deletef(files[i].getAbsolutePath());
if(!flag){
break;
}
//删除子目录
}else if (files[i].isDirectory()){
flag = deleteD(files[i].getAbsolutePath());
if(!flag){
break;
}
}
}
if (!flag){
System.out.println("删除目录失败");
return false;
}
if (dirFile.delete()){
System.out.println(dir + "目录删除成功");
return true;
}else
return false;
}
//查看文件属性
public long getSize(File file) {
//判断文件是否存在
if (file.exists()) {
//如果是目录则递归计算其内容的总大小,如果是文件则直接返回其大小
if (!file.isFile()) {
//获取文件大小
File[] fl = file.listFiles();
long ss = 0;
for (File f : fl)
ss += getSize(f);
return ss;
} else {
long ss = (long) file.length();
return ss;
}
}
return 0;
}
public String setsize(long size){
if(size>=0&&size<1024){
String str = String.valueOf(size)+"字节";
return str;
}else if(size>=1024&&size<(1024*1024)){
String str = String.valueOf(size/1024)+"KB";
return str;
}else if(size>=(1024*1024)&&size<(1024*1024*1024)){
String str = String.valueOf(size/1024/1024)+"M";
return str;
}else if(size>=(1024*1024*1024)){
String str = String.valueOf(size/1024/1024/1024)+"G";
return str;
}
return null;
}
public static void property(String filePatch){
File file = new File(filePatch);
long filesize = 0;
String size = "";
filesize = new File_operations().getSize(file);
System.out.println(filesize);
size = new File_operations().setsize(filesize);
JFrame shuxing = new JFrame("属性");
shuxing.setResizable(false);
shuxing.setBounds(200, 50, 300, 400);
JLabel jlable1= new JLabel();
Calendar cal = Calendar.getInstance();
long time = file.lastModified();
cal.setTimeInMillis(time);
shuxing.add(jlable1);
jlable1.setText("<html>"+"----------------------------------------------------------------------"
+" <br>"+" <br>"+" <br>"+". 文件名称:"+file.getName()
+" <br>"+" <br>"+" <br>"+". 文件的相对路径: "+file.getParent()
+" <br>"+" <br>"+" <br>"+". 是否是可执行文件: "+file.canExecute()
+" <br>"+" <br>"+" <br>"+". 是否是隐藏文件:"+file.isHidden()
+" <br>"+" <br>"+" <br>"+". 文件大小:" + size
+" <br>"+" <br>"+" <br>"+". 文件最后的修改时间: "+cal.getTime().toLocaleString()
+" <br>"+" <br>"+"----------------------------------------------------------------------");
shuxing.setVisible(true);
}
//新建文件
public static boolean createfile(String destfilename) throws IOException {
File file = new File(destfilename);
//创建目标文件
if(destfilename.endsWith(".txt")){
file.createNewFile();
System.out.println(destfilename + "创建成功");
}else if(destfilename.endsWith(".doc")){
file.createNewFile();
System.out.println(destfilename + "创建成功");
}else if(destfilename.endsWith(".xls")){
file.createNewFile();
System.out.println(destfilename + "创建成功");
}else if(destfilename.endsWith(".accdb")){
file.createNewFile();
System.out.println(destfilename + "创建成功");
}else if(file.getName().equals("")){
JOptionPane jsp = new JOptionPane();
jsp.showMessageDialog(jsp, "必须输入文件名");
return false;
}else if(file.exists()){
JOptionPane jsp = new JOptionPane();
jsp.showMessageDialog(jsp, "重复命名,创建失败");
return false;
}else if(destfilename.endsWith("")&!file.getName().equals("")){
JOptionPane jsp = new JOptionPane();
jsp.showMessageDialog(jsp, "没有后缀名可能导致文件不可用");
file.createNewFile();
return false;
}
return false;
}
/*
*创建目录
*/
public static void creatdir(String destdirname){
File dir = new File(destdirname);
if(!destdirname.endsWith(File.separator)){//???
destdirname = destdirname + File.separator;
}
//创建目标文件
if(dir.mkdirs()){
System.out.println(destdirname+"目录创建成功");
return ;
}else if(dir.exists()){
JOptionPane jsp = new JOptionPane();
jsp.showMessageDialog(jsp, "重复命名,创建失败");
return ;
}else {
JOptionPane jsp = new JOptionPane();
jsp.showMessageDialog(jsp, "文件名不能包含下列任何字符:?/\\*<>.?;");
return;
}
}
//打开文件或者程序
public static boolean word (File file) throws IOException{
if(file.isFile()){
try{
Desktop.getDesktop().open(file); //使用系统中的默认的方式打开
}catch (Exception ss){
JOptionPane jinggao = new JOptionPane();
jinggao.showMessageDialog(jinggao, "没有默认打开的程序");
}
}
return false;
}
public String zhengze(String str){
char[] strs = null ;
strs = str.toCharArray();
for(int i= 0;i<strs.length;i++){
if(strs[i]=='?'||strs[i]=='/'||strs[i]=='\\'||strs[i]=='?'||strs[i]=='.'||strs[i]=='<'||strs[i]=='>'||strs[i]=='*'||strs[i]=='|'){
JOptionPane jsp = new JOptionPane();
jsp.showMessageDialog(jsp, "文件名不能包含下列任何字符:?/\\*<>.?;");
break;
}else return str;
}
return null;
}
public boolean panduan (String strid){
if(strid.equals("")){
JOptionPane jop1 = new JOptionPane("重命名");
jop1.showMessageDialog(null, "名字不能为空");
return false;
}
if (strid.equals(null)){
//JOptionPane.showMessageDialog(null,"请输入文件名","提示信息",JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
}
package explorer;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
class IconRenderer extends JLabel implements ListCellRenderer {
public IconRenderer(){
setOpaque(true); //设置透明
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// TODO 自动生成的方法存根
Object node = value;
setIcon(((MyNode) node).geticon(((MyNode) node).file));
setText(value.toString());
setBackground(isSelected ? Color.getHSBColor(255, 90, 255):Color.white);
setForeground(isSelected? Color.BLACK:Color.black);
return this;
}
}
package explorer;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
public class MyFrame extends JFrame {
JToolBar jtb;//工具栏
JOptionPane jop;
TreePath path;
JFrame fr;// 窗体
JMenuBar MyJMenuBar;// 菜单
JPopupMenu popMenu;
JPopupMenu treepop;
JList myList;
JTextField lujing;
int lujingcount;//目录现文件的数量
JTextField search;//搜索栏
JButton refurbish;//刷新按钮
MyNode mynode;
MouseListener listmouse;
Searchfile searchfile = new Searchfile();
File_operations fileopeara = new File_operations();
ArrayList<String> oldpath = new ArrayList<String>();
ArrayList<String> nextpath = new ArrayList<String>();
public final static int W = 850;
public final static int H = 600;
private static final Object NORTH = null;
private static final Object CENTER = null;
private static final Object SOUTH = null;
static int a = 0;
MyJlistmodel model = new MyJlistmodel();
MyJtreemodel treemodel = new MyJtreemodel();
Object listob = null;
Object treeob = null;
Object listobparent = null;
int listobindex ;
String copyqian;
String copyname;
String treecopyqian;
String treecopyname;
File oldsfile;
JScrollPane jsp2;
JTree tree;
JMenu POPanew;
JMenu JBanew;
JMenuItem POPdelete;
JMenuItem JBdelete;
JMenuItem treepopdelete;
JMenuItem POPcopy;
JMenuItem JBcopy;
JMenuItem treepopcopy;
JMenuItem POPpaste;
JMenuItem JBpaste;
JMenuItem treepoppaste;
JMenuItem POPjianqie;
JMenuItem JBjianqie;
JMenuItem treepopjianqie;
JMenuItem POP1dir;
JMenuItem JB1dir;
JMenuItem treepopdir;
JMenuItem POP1rename;
JMenuItem treepoprename;
JMenuItem POP1property;
public void launch() {
fr = new MyFrame();
tree = new JTree();
fr.setBounds(100, 70, W, H);
fr.setTitle("资源管理器");
/*
* 设置菜单
*/
MyJMenuBar = new JMenuBar();// 新建菜单栏
JMenu JBfile = new JMenu("文件");
JMenu JBedit = new JMenu("编辑");
JMenu JBlook = new JMenu("查看");
JMenu JBtool = new JMenu("工具");
JMenu JBhelp = new JMenu("帮助");
JMenuItem JB1close = new JMenuItem("关闭");
JMenuItem JBpingpu=new JMenuItem("平铺");
JMenuItem JBxiangxi=new JMenuItem("详细信息");
JMenuItem JB1rename = new JMenuItem("重命名");
POP1rename = new JMenuItem("重命名");
treepoprename = new JMenuItem("重命名");
JMenuItem JB1property = new JMenuItem("属性");
POP1property = new JMenuItem("属性");
POPanew = new JMenu("新建");
JBanew = new JMenu("新建");
POPdelete = new JMenuItem("删除(D)");
POPdelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,InputEvent.CTRL_MASK));
treepopdelete = new JMenuItem("删除");
JBdelete = new JMenuItem("删除");
POPcopy = new JMenuItem("复制(C)");
POPcopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
JBcopy = new JMenuItem("复制");
treepopcopy = new JMenuItem("复制");
POPpaste = new JMenuItem("粘贴(V)");
POPpaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
JBpaste = new JMenuItem("粘贴");
treepoppaste = new JMenuItem("粘贴");
POP1dir = new JMenuItem("文件夹");
JB1dir = new JMenuItem("文件夹");
treepopdir= new JMenuItem("文件夹");
JMenuItem POPtxtafile = new JMenuItem("文本文档");
JMenuItem POPdocafile = new JMenuItem("Microsoft Word 文档");
JMenuItem POPexcelafile = new JMenuItem("Microsoft Excel 工作表");
JMenuItem POPaccessafile = new JMenuItem("Microsoft Access 数据库");
JMenuItem JBafile = new JMenuItem("文件");
POPjianqie = new JMenuItem("剪切");
JBjianqie = new JMenuItem("剪切");
treepopjianqie = new JMenuItem("剪切");
POPanew.add(POP1dir);
POPanew.add(POPtxtafile);
POPanew.add(POPdocafile);
POPanew.add(POPexcelafile);
POPanew.add(POPaccessafile);
JBanew.add(JBafile);
JBanew.add(JB1dir);
JBfile.add(JBanew);
JBfile.add(JBdelete);
JBfile.add(JB1rename);
JBfile.add(JB1property);
JBfile.add(JB1close);
JBedit.add(JBjianqie);
JBedit.add(JBcopy);
JBedit.add(JBpaste);
JBlook.add(JBpingpu);
JBlook.add(JBxiangxi);
MyJMenuBar.add(JBfile);
MyJMenuBar.add(JBedit);
MyJMenuBar.add(JBlook);
MyJMenuBar.add(JBtool);
MyJMenuBar.add(JBhelp);
JB1close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(-1);
fr.dispose();
}
});
//查看
ActionListener xiangxixinxi = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
myList.setLayoutOrientation(JList.VERTICAL);
}
};
JBxiangxi.addActionListener(xiangxixinxi);
ActionListener pingpulistener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
myList.setLayoutOrientation(JList.VERTICAL_WRAP );//设置Mylist的布局
}
};
JBpingpu.addActionListener(pingpulistener);
// 删除文件
ActionListener deletelistener =new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File lujingfile = new File(lujing.getText());
if(e.getSource().equals(POPdelete)||e.getSource().equals(JBdelete)){
String s = ((MyNode) listob).file.getPath();
File df = new File(s);
System.out.println(df);
System.out.println("菜单上的");
if (df.isFile()) {
fileopeara.deletef(s);
myList.clearSelection();
myList.updateUI();
model.setNode(new MyNode(lujingfile));
} else if (df.isDirectory()) {
System.out.println("删掉吧");
fileopeara.deleteD(s);
myList.clearSelection();
myList.updateUI();
tree.updateUI();
model.setNode(new MyNode(lujingfile));
treemodel.setNode(new MyNode(lujingfile));
}
}else if(e.getSource().equals(JBdelete)){
}else if(e.getSource().equals(treepopdelete)){
String t = ((MyNode) treeob).file.getPath();
File file = new File(t);
System.out.println("树上的"+t);
System.out.println("树上的");
fileopeara.deleteD(t);
myList.clearSelection();
myList.updateUI();
tree.updateUI();
lujing.setText(file.getParent());
model.setNode(new MyNode(file.getParentFile()));
}
}
};
POPdelete.addActionListener(deletelistener);
JBdelete.addActionListener(deletelistener);
treepopdelete.addActionListener(deletelistener);
// 新建文件
ActionListener listenernew =new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
File lujingfile = new File(lujing.getText());
if(e.getSource()==POPtxtafile||e.getSource().equals(JBafile)){
int shumu = 1;
String str =lujing.getText()+File.separator+"新建文本文档.txt";
JOptionPane jop1 = new JOptionPane("重命名");
if(!new File(str).exists()){
str=lujing.getText()+File.separator+"新建文本文档.txt";
}else{
while (new File(str).exists()){
shumu++;
str = lujing.getText()+File.separator+"新建文本文档"+String.valueOf(shumu)+".txt";
}
}
File file =new File(str);
String inputValue = JOptionPane.showInputDialog("请输入文件夹名字",file.getName());
if(!fileopeara.panduan(inputValue)){
return;
}
String ss = fileopeara.zhengze(inputValue);
try {
fileopeara.createfile(lujing.getText()+File.separator+ss);
} catch (IOException e1) {
// TODO Auto-generated catch block
return;
}
}else if(e.getSource()==POPdocafile){
int shumu = 1;
String str =lujing.getText()+File.separator+"新建word文档.doc";
JOptionPane jop1 = new JOptionPane("重命名");
if(!new File(str).exists()){
str=lujing.getText()+File.separator+"新建word文档.doc";
}else{
while (new File(str).exists()){
shumu++;
str = lujing.getText()+File.separator+"新建word文档"+String.valueOf(shumu)+".doc";
}
}
File file =new File(str);
String inputValue = JOptionPane.showInputDialog("请输入Word文档名字",file.getName());
if(!fileopeara.panduan(inputValue)){
return;
}
String ss = fileopeara.zhengze(inputValue);
try {
fileopeara.createfile(lujing.getText()+File.separator+ss);
} catch (IOException e1) {
// TODO Auto-generated catch block
return;
}
}else if(e.getSource()==POPexcelafile){
int shumu = 1;
String str =lujing.getText()+File.separator+"新建excle工作表.xls";
JOptionPane jop1 = new JOptionPane("重命名");
if(!new File(str).exists()){
str=lujing.getText()+File.separator+"新建excle工作表.xls";
}else{
while (new File(str).exists()){
shumu++;
str = lujing.getText()+File.separator+"新建excle工作表"+String.valueOf(shumu)+".xls";
}
}
File file =new File(str);
String inputValue = JOptionPane.showInputDialog("请输入Excle文档名字",file.getName());
if(!fileopeara.panduan(inputValue)){
return;
}
String ss = fileopeara.zhengze(inputValue);
try {
System.out.println("新建啦"+ss);
fileopeara.createfile(lujing.getText()+File.separator+ss);
} catch (IOException e1) {
// TODO Auto-generated catch block
return;
}
}else if(e.getSource().equals(POPaccessafile)){
int shumu = 1;
String str =lujing.getText()+File.separator+"新建access数据库.accdb";
JOptionPane jop1 = new JOptionPane("重命名");
if(!new File(str).exists()){
str=lujing.getText()+File.separator+"新建access数据库.accdb";
}else{
while (new File(str).exists()){
shumu++;
str = lujing.getText()+File.separator+"新建access数据库"+String.valueOf(shumu)+".accdb";
}
}
File file =new File(str);
String inputValue = JOptionPane.showInputDialog("请输入Access文档名字",file.getName());
if(!fileopeara.panduan(inputValue)){
return;
}
String ss = fileopeara.zhengze(inputValue);
try {
fileopeara.createfile(lujing.getText()+File.separator+ss);
} catch (IOException e1) {
// TODO Auto-generated catch block
return;
}
}
myList.updateUI();
model.setNode(new MyNode(lujingfile));
}
};
POPtxtafile.addActionListener(listenernew);
POPdocafile.addActionListener(listenernew);
POPexcelafile.addActionListener(listenernew);
POPaccessafile.addActionListener(listenernew);
JBafile.addActionListener(listenernew);
//新建文件夹
ActionListener newdir = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int shumu = 1;
String str =lujing.getText()+File.separator+"新建文件夹";
JOptionPane jop1 = new JOptionPane("重命名");
if(!new File(str).exists()){
str=lujing.getText()+File.separator+"新建文件夹";
}else{
while (new File(str).exists()){
shumu++;
str = lujing.getText()+File.separator+"新建文件夹"+String.valueOf(shumu);
}
}
File file =new File(str);
String inputValue = JOptionPane.showInputDialog("请输入字符串",file.getName());
try{
if(inputValue.equals("") ){
JOptionPane.showMessageDialog(null,"请输文档名字","提示信息",JOptionPane.ERROR_MESSAGE);
return;
}
}catch(Exception bb){
return;
}
String ss = fileopeara.zhengze(inputValue);
if(!ss.equals(null)){
fileopeara.creatdir(lujing.getText()+File.separator+ss);
}
File lujingfile = new File(lujing.getText());
myList.updateUI();
tree.updateUI();
model.setNode(new MyNode(lujingfile));
treemodel.setNode(new MyNode(lujingfile));
}
};
POP1dir.addActionListener(newdir);
JB1dir.addActionListener(newdir);
treepopdir.addActionListener(newdir);
// 设置文件属性
ActionListener listenerproperty = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
listob = myList.getSelectedValue();
String s = ((MyNode) listob).file.getPath();
if(s == null){
return;
}else{
fileopeara.property(s);
}
}
};
JB1property.addActionListener(listenerproperty);
POP1property.addActionListener(listenerproperty);
// 复制文件
ActionListener listenercopy = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(POPcopy)||e.getSource().equals(JBjianqie)||e.getSource().equals(POPjianqie)||e.getSource().equals(JBcopy)){
copyqian = ((MyNode) listob).file.getPath();
System.out.println("copyqian"+copyqian);
copyname = ((MyNode) listob).file.getName();
if(e.getSource().equals(POPcopy)){
a=0;
POPpaste.setEnabled(true);
}else if(e.getSource().equals(POPjianqie)){
a=1;
POPpaste.setEnabled(true);
}else if(e.getSource().equals(JBjianqie)){
POPpaste.setEnabled(true);
a=1;
}else if(e.getSource().equals(JBcopy)){
a=0;
POPpaste.setEnabled(true);
}
}else if(e.getSource().equals(treepopcopy)||e.getSource().equals(treepopjianqie)){
treecopyqian = ((MyNode) treeob).file.getPath();
System.out.println("copyqian"+copyqian);
treecopyname = ((MyNode) treeob).file.getName();
if(e.getSource().equals(treepopcopy)){
a=3;
POPpaste.setEnabled(true);
}else if(e.getSource().equals(treepopjianqie)){
a=2;
POPpaste.setEnabled(true);
}
}
}
};
POPcopy.addActionListener(listenercopy);
JBjianqie.addActionListener(listenercopy);
POPjianqie.addActionListener(listenercopy);
JBcopy.addActionListener(listenercopy);
treepopcopy.addActionListener(listenercopy);
treepopjianqie.addActionListener(listenercopy);
// 粘贴文件
ActionListener listenerpaste = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(a!=2&&a!=3){
File lujingfile = new File(lujing.getText());
File qianfile = new File(copyqian);
if(qianfile.isFile()){
File file =new File(lujing.getText()+File.separator+copyname);
if(file.exists()){
JOptionPane jinggao = new JOptionPane();
jinggao.showMessageDialog(jinggao, "文件已存在");
return;
}
fileopeara.copyfile(copyqian, lujing.getText()+File.separator+copyname);
if(a==1){
fileopeara.deletef(copyqian);
}
myList.updateUI();
model.setNode(new MyNode(lujingfile));
}else if(qianfile.isDirectory()){
File file =new File(lujing.getText()+File.separator+copyname);
if(file.exists()){
JOptionPane jinggao = new JOptionPane();
jinggao.showMessageDialog(jinggao, "文件已存在");
return;
}
fileopeara.copydir(copyqian, lujing.getText()+File.separator+copyname);
if(a==1){
fileopeara.deleteD(copyqian);
}
myList.updateUI();
tree.updateUI();
model.setNode(new MyNode(lujingfile));
treemodel.setNode(new MyNode(lujingfile));
}
}
else if(a==3){
System.out.println("树上的粘贴");
File treelujingfile = new File(lujing.getText());
System.out.println(treelujingfile);
File treeqianfile = new File(treecopyqian);
File file =new File(lujing.getText()+File.separator+treecopyname);
if(file.exists()){
JOptionPane jinggao = new JOptionPane();
jinggao.showMessageDialog(jinggao, "文件已存在");
return;
}
fileopeara.copydir(treecopyqian, lujing.getText()+File.separator+treecopyname);
myList.updateUI();
tree.updateUI();
model.setNode(new MyNode(treelujingfile));
treemodel.setNode(new MyNode(treelujingfile));
}else if(a==2){
System.out.println("树上的粘贴");
File treelujingfile = new File(lujing.getText());
System.out.println(treelujingfile);
File treeqianfile = new File(treecopyqian);
File file =new File(lujing.getText()+File.separator+treecopyname);
if(file.exists()){
JOptionPane jinggao = new JOptionPane();
jinggao.showMessageDialog(jinggao, "文件已存在");
return;
}
fileopeara.copydir(treecopyqian, lujing.getText()+File.separator+treecopyname);
if(a==2){
fileopeara.deleteD(treecopyqian);
}
myList.updateUI();
tree.updateUI();
model.setNode(new MyNode(treelujingfile));
treemodel.setNode(new MyNode(treelujingfile));
}
}
};
POPpaste.addActionListener(listenerpaste);
JBpaste.addActionListener(listenerpaste);
treepoppaste.addActionListener(listenerpaste);
//重命名文件
ActionListener rename = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
File lujingfile = new File(lujing.getText());
if(e.getSource().equals(POP1rename)||e.getSource().equals(JB1rename)){
String s = ((MyNode)listob).file.getPath();
String name = ((MyNode)listob).file.getName();
JOptionPane jop3 = new JOptionPane("重命名");
String inputValue = JOptionPane.showInputDialog("请输入字符串",name);
if(inputValue == null) {
return;
}else if (inputValue.equals("")){
JOptionPane.showMessageDialog(null,"请输文档名字","提示信息",JOptionPane.ERROR_MESSAGE);
return;
}
String ss = fileopeara.zhengze(inputValue);
File file = new File(s);
/*if(file.exists()){
JOptionPane.showMessageDialog(null,"文件已存在","提示信息",JOptionPane.ERROR_MESSAGE);
return;
}*/
if(!ss.equals("")){
file.renameTo(new File(lujing.getText()+File.separator+ss));
}
myList.updateUI();
tree.updateUI();
model.setNode(new MyNode(lujingfile));
treemodel.setNode(new MyNode(lujingfile));
}else if(e.getSource().equals(treepoprename)){
String s = ((MyNode)treeob).file.getPath();
String name = ((MyNode)treeob).file.getName();
JOptionPane jop3 = new JOptionPane("重命名");
String inputValue = JOptionPane.showInputDialog("请输入字符串",name);
if(inputValue == null) {
return;
}else if (inputValue.equals("")){
JOptionPane.showMessageDialog(null,"请输文档名字","提示信息",JOptionPane.ERROR_MESSAGE);
return;
}
String ss = fileopeara.zhengze(inputValue);
File file = new File(s);
/*if(file.exists()){
JOptionPane.showMessageDialog(null,"文件已存在","提示信息",JOptionPane.ERROR_MESSAGE);
return;
}*/
if(!ss.equals("")){
file.renameTo(new File(file.getParent()+File.separator+ss));
}
myList.updateUI();
tree.updateUI();
model.setNode(new MyNode(lujingfile));
treemodel.setNode(new MyNode(lujingfile));
System.out.println("路径是这个"+file.getParent()+File.separator+ss);
lujing.setText(file.getParent()+File.separator+ss);
}
}
};
POP1rename.addActionListener(rename);
JB1rename.addActionListener(rename);
treepoprename.addActionListener(rename);
/*
* 设置工具栏
*/
jtb = new JToolBar();
jtb.setFloatable(false);
JButton next = new JButton(" → ");//上一层
JButton exit = new JButton(" ← ");//下一层
JButton Laststep =new JButton("上一步");//上一步
JButton nextStep = new JButton("下一步");//下一步
//上一步的监听
Laststep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(oldpath.isEmpty()){
Laststep.setEnabled(false);
return;
}else{
for(int i = 1;i<oldpath.size();i++){
System.out.println("后退"+oldpath.get(0));
}
for(int i = 1;i<oldpath.size();i++){
System.out.println("后sss退"+oldpath.get(i));
}
if((oldpath.size()-1)==0){
return;
}
File shangyibufile= new File(oldpath.remove(oldpath.size()-1));
nextpath.add(shangyibufile.getPath());
//nextpath.add(shangyibufile.getPath());
System.out.println("--------------------------");
lujing.setText(shangyibufile.getPath());
model.setNode(new MyNode(shangyibufile));
myList.updateUI();
}
}
});
nextStep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(nextpath.isEmpty()){
return;
}else {
File xiayibufile= new File(nextpath.remove(nextpath.size()-1));
for(int i = 0;i<nextpath.size();i++){
System.out.println("nextpath中的路径"+nextpath.get(i));
}
lujing.setText(xiayibufile.getPath());
model.setNode(new MyNode(xiayibufile));
myList.updateUI();
}
}
});
// 工具栏上→按钮的监听时间 进入下一层
next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(myList.isSelectionEmpty()){
next.setForeground(Color.CYAN);
return;
}
if(((MyNode) listob).file.isDirectory()){
myList.updateUI();
model.setNode((MyNode) listob);
}
}
});
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(Pattern.matches("[A-Z]\\:\\\\", lujing.getText())){
myList.updateUI();
model.setNode(new MyNode(MyNode.root));
lujing.setText("我的电脑");
return;
}else if(myList.isSelectionEmpty()){
exit.setForeground(Color.cyan);
return;
}else if(lujing.getText().equals("我的电脑")){
myList.updateUI();
model.setNode(new MyNode(MyNode.root));
return;
}
else if(lujing.getText().equals("")){
System.out.println();
return;
}else {
String path = ((MyNode) listob).file.getParent();
File dir = new File(lujing.getText());
myList.updateUI();
model.setNode(new MyNode(dir.getParentFile()));
lujing.setText(dir.getParent());
}
}
});
//搜索
lujing = new JTextField();
KeyListener listenercome = new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if(lujing.getText().equals("")){
myList.updateUI();
model.setNode(new MyNode(MyNode.root));
}else {
oldpath.add(lujing.getText());
Object s = lujing.getText();
System.out.println("ooooooooooooooo"+s.toString());
File file = new File(s.toString());
oldpath.add(lujing.getText());
model.setNode(new MyNode(file));
myList.updateUI(); }}
}
};
lujing.addKeyListener(listenercome);
//刷新按钮的监听
ActionListener refurbishlistener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(lujing.getText().equals("我的电脑")){
System.out.println("我的电脑");
myList.updateUI();
tree.updateUI();
model.setNode(new MyNode(MyNode.root));
}else if(lujing.getText().equals("")){
return;
}else{
System.out.println("zzzz电脑");
File file = new File(lujing.getText());
myList.updateUI();
tree.updateUI();
model.setNode(new MyNode(file));
}
}
};
//根据路径进入的监听
search= new JTextField();//搜索栏
//搜索的键盘监听
KeyListener listenersearch = new KeyListener() {
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("33333333333333333333333333333333");
String dir = ((MyNode)listob).file.getParent();
Searchfile.listAllFiles(dir);
System.out.println(dir);
String s = search.getText();
FilenameFilter myFilenameFilter = new Searchfile.MyFilenameFilter(s);
Searchfile.listFilesByFilenameFilter(myFilenameFilter, dir);
new Searchfile().intsss();
}
}
};
search.addKeyListener(listenersearch);
jtb.add(Laststep);
jtb.add(nextStep);
jtb.add(exit);//上一个路径
jtb.add(next);//下一个路径
jtb.add(lujing);
refurbish = new JButton("刷新");
refurbish.addActionListener(refurbishlistener);
jtb.add( refurbish);//进入按钮
jtb.add(search);
/*
* jlist设置弹出式菜单
*/
popMenu = new JPopupMenu();
popMenu.add(POPanew);
popMenu.add(POPcopy);
popMenu.add(POPpaste);
popMenu.add(POPjianqie);
popMenu.add(POPdelete);
popMenu.add(POP1rename);
popMenu.add(POP1property);
POPpaste.setEnabled(false);
/*
* jtree设置弹出式菜单
*/
treepop = new JPopupMenu();
treepop.add(treepopdelete);
treepop.add(treepopdir);
treepop.add(treepopcopy);
treepop.add(treepopjianqie);
treepop.add(treepoppaste);
treepop.add(treepoprename);
treepop.setEnabled(false);
/*
* 创建Jlist 显示文件系统根
*/
myList = new JList();
myList.setModel(model);
myList.setFixedCellHeight(25);
myList.setSize(myList.getPreferredScrollableViewportSize());
myList.setCellRenderer(new IconRenderer());
jsp2 = new JScrollPane(myList);
myList.add(popMenu);
tree.add(treepop);
// 设置Jtree
tree.setModel(new MyJtreemodel());// 设置树的模型
tree.setCellRenderer(new MyRenderer());
tree.setShowsRootHandles(true);
tree.setEditable(true);//设置是否可以编辑
JScrollPane jsp = new JScrollPane(tree);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSplitPane under = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,jsp, jsp2);
JSplitPane center = new JSplitPane(JSplitPane.VERTICAL_SPLIT,MyJMenuBar, under);
JSplitPane all = new JSplitPane(JSplitPane.VERTICAL_SPLIT,jtb,center);
//添加监听
myList.addMouseListener(new mypopListener());
tree.addMouseListener(new mytreepoplistener());
// 设置分隔条的大小
under.setDividerSize(3);
center.setDividerSize(3);
all.setDividerSize(0);
// 分隔条是否能移动
under.setEnabled(true);
center.setEnabled(false);
all.setEnabled(false);
fr.add(all);
fr.setVisible(true);
// 给树加监听,当树中选择发生改变时执行
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
treeob = tree.getLastSelectedPathComponent();// 返回当前选择的第一个节点中的最后一个路径组件。
myList.updateUI();// 重置 ListUI 属性,将其设置为当前外观所提供的值
model.setNode((MyNode) treeob);
System.out.println("BBBBBBBBBBBBBBBBBB" + ((MyNode) treeob).file.getName().equals("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"));
if(((MyNode) treeob).file.getName().equals("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")){
lujing.setText("我的电脑");
}else
lujing.setText(((MyNode)treeob).file.getPath());
oldpath.add(lujing.getText());
}
});
// 给Jlist添加监听 当选中的文件发生改变时执行
myList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
listob = myList.getSelectedValue();
System.out.println("----------11-----------"+listob);
listobparent = myList.getParent().getName();
}
});
MouseListener treemouse = new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(treeob==null){
return;
}
if(((MyNode) treeob).file.isDirectory()&&e.getButton() == MouseEvent.BUTTON3){
treepop.show(e.getComponent(), e.getX(), e.getY());
}
}
};
tree.addMouseListener(treemouse);
listmouse =new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseClicked(MouseEvent e) {
if(listob==null){
return;
}
if(((MyNode) listob).file.isDirectory()&&e.getButton() == MouseEvent.BUTTON3){
POPanew.setEnabled(false);
POPpaste.setEnabled(false);
POPcopy.setEnabled(true);
POPdelete.setEnabled(true);
POPjianqie.setEnabled(true);
POP1rename.setEnabled(true);
POP1property.setEnabled(true);
popMenu.show(e.getComponent(), e.getX(), e.getY());
}else if(((MyNode) listob).file.isFile()&&e.getButton() == MouseEvent.BUTTON3){
POPanew.setEnabled(false);
POPpaste.setEnabled(false);
POPcopy.setEnabled(true);
POPdelete.setEnabled(true);
POPjianqie.setEnabled(true);
POP1rename.setEnabled(true);
POP1property.setEnabled(true);
popMenu.show(e.getComponent(), e.getX(), e.getY());
}
if (e.getClickCount() == 2&& ((MyNode) listob).file.isDirectory()) {
oldpath.add(lujing.getText());
lujing.setText(((MyNode) listob).file.getPath());
myList.updateUI();
System.out.println("列表中的目录 "+lujing.getText());
model.setNode((MyNode) listob);
File file = new File(((MyNode) listob).file.getPath());
lujingcount = new MyNode(file).childcount;
System.out.println("lujingcount"+lujingcount);
myList.clearSelection();
} else if (e.getClickCount() == 2&& ((MyNode) listob).file.isFile()) {
System.out.println("列表中的文件"+lujing.getText());
File file = new File(((MyNode) listob).file.getPath());
try {
fileopeara.word(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("错了+++++++++++++++++++");
}
}
}
};
myList.addMouseListener(listmouse);
}
// 右键弹出式菜单鼠标事件
class mypopListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if( myList.getMinSelectionIndex()<0){
if(e.getButton() == MouseEvent.BUTTON3){
POPanew.setEnabled(true);
POPcopy.setEnabled(false);
POPdelete.setEnabled(false);
POPjianqie.setEnabled(false);
POP1rename.setEnabled(false);
POP1property.setEnabled(false);
popMenu.show(e.getComponent(), e.getX(), e.getY());
}
return;
}
try{
if(myList.getMinSelectionIndex()<0&&myList.getMinSelectionIndex()<0){
if(e.getButton() == MouseEvent.BUTTON3){
POPanew.setEnabled(true);
POPcopy.setEnabled(false);
POPdelete.setEnabled(false);
POPjianqie.setEnabled(false);
POP1rename.setEnabled(false);
POP1property.setEnabled(false);
popMenu.show(e.getComponent(), e.getX(), e.getY());
}
myList.clearSelection();
return;
}else if(myList.getMinSelectionIndex()==0&&myList.getMinSelectionIndex()==0){
System.out.println("ooooooooooooooo");
if(!myList.getCellBounds( myList.getMinSelectionIndex(), myList.getMaxSelectionIndex()).contains(e.getPoint())){
myList.clearSelection();
}
return;
}else if(!myList.getCellBounds( myList.getMinSelectionIndex(), myList.getMaxSelectionIndex()).contains(e.getPoint())){
myList.clearSelection();
if(e.getButton() == MouseEvent.BUTTON3){
popMenu.show(e.getComponent(), e.getX(), e.getY());
}
return;
}
}catch (Exception ee){
ee.printStackTrace();
return;
}
if (e.getButton() == MouseEvent.BUTTON3) {// 右键
popMenu.show(e.getComponent(), e.getX(), e.getY());
} else if (e.getButton() == MouseEvent.BUTTON1){// 点击鼠标左键
popMenu.setVisible(false);// 弹出菜单
}
int index = myList.locationToIndex(e.getPoint());
myList.setSelectedIndex(index);
}
}
class mytreepoplistener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {// 右键
treepopdir.setEnabled(true);
treepopcopy.setEnabled(true);
treepopdelete.setEnabled(true);
treepopjianqie.setEnabled(true);
treepoprename.setEnabled(true);
treepoppaste.setEnabled(false);
treepop.show(e.getComponent(), e.getX(), e.getY());
System.out.println("出不来?");
} else if (e.getButton() == MouseEvent.BUTTON1){// 点击鼠标左键
treepop.setVisible(false);// 弹出菜单消失
}
int index = tree.getClosestRowForLocation(e.getX(), e.getY());
tree.setSelectionRow(index) ;
}
}
public static void main(String[] args) {
new MyFrame().launch();
}
}
package explorer;
import java.io.File;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class MyJlistmodel implements ListModel{
MyNode node = new MyNode(MyNode.root);
public void setNode(MyNode node){
this.node=node;
}
@Override
public void addListDataListener(ListDataListener l) {
// TODO 自动生成的方法存根
}
public Object getRoot() {//返回树根
// TODO 自动生成的方法存根
return new MyNode();
}
@Override
public Object getElementAt(int s) {
// TODO 自动生成的方法存根
if(node!=null&&node.files.length!=0){
return new MyNode(node.files[s]);
}else{
return null;
}
}
@Override
public int getSize() {
// TODO 自动生成的方法存根
if(node!=null){
return node.getchildcount();
}else{
return 0;
}
}
@Override
public void removeListDataListener(ListDataListener l) {
// TODO 自动生成的方法存根
}
}
package explorer;
import java.io.File;
import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class MyJtreemodel implements TreeModel {
MyNode node;
public void setNode(MyNode node){
this.node=node;
}
@Override
public void addTreeModelListener(TreeModelListener l) {
// TODO 自动生成的方法存根
}
@Override
public Object getChild(Object parent, int index) {// 返回父节点的子数组中索引 index 位置的 parent 的子节点。
// TODO 自动生成的方法存根
// ((MyNode)parent).treelist.indexOf(parent);
return new MyNode(((MyNode)parent).dircounts.get(index));
}
@Override
public int getChildCount(Object parent) {//返回 parent 的子节点数。
// TODO 自动生成的方法存根
return ((MyNode)parent).getdircount();
}
@Override
public int getIndexOfChild(Object parent, Object child) {//返回父节点中子节点的索引。
// TODO 自动生成的方法存根
return 0;
}
@Override
public Object getRoot() {//返回树根
// TODO 自动生成的方法存根
return new MyNode();
}
@Override
public boolean isLeaf(Object node) {//如果 node 为叶节点,则返回 true。
// TODO 自动生成的方法存根
if(((MyNode)node).file.isDirectory()){
return false;
}
return true;
}
@Override
public void removeTreeModelListener(TreeModelListener arg0) {
// TODO 自动生成的方法存根
}
@Override
public void valueForPathChanged(TreePath path, Object newValue) {
// TODO 自动生成的方法存根
}
public void TreeCellRenderer(){
}
}
package explorer;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class MyMenuBar extends JMenuBar {
JMenu JBfile = new JMenu("文件");
}
package explorer;
import java.io.*;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.util.ArrayList;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;
public class MyNode {
static FileSystemView fstv = FileSystemView.getFileSystemView();
static File root = fstv.getFiles(fstv.getHomeDirectory(), true)[0];
File file;
ArrayList<File> dircounts = new ArrayList<File>();
File[] files;
int childcount;
int dircount;
public MyNode(){
this.file= root ;
// System.out.println(root.toString());
int j = 0;
files = fstv.getFiles(root, true);
for (int i = 0 ;i<files.length;i++){
if(files[i].isDirectory()){
dircounts.add(files[i]);
}
}
childcount=files.length;
dircount = dircounts.size();
}
public MyNode(File file) {
this.file = file;
files = fstv.getFiles(file, true);
for (int i = 0 ;i<files.length;i++){
if(files[i].isDirectory()){
dircounts.add(files[i]);
}
}
childcount=files.length;
dircount = dircounts.size();
}
public String toString(){
return fstv.getSystemDisplayName(file);
}
public int getchildcount(){
return childcount;
}
public int getdircount(){
return dircount;
}
public Icon geticon (File file){
if(file!= null&& file.exists()){
FileSystemView fstv = FileSystemView.getFileSystemView();
return (fstv.getSystemIcon(file));
}
return null;
}
}
package explorer;
import java.awt.Color;
import java.awt.Component;
import java.beans.FeatureDescriptor;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
class MyRenderer extends DefaultTreeCellRenderer{
public Component getTreeCellRendererComponent(JTree tree, Object object,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
MyNode node=(MyNode)object;
Icon icon = node.geticon(node.file);
setOpenIcon(icon);
setLeafIcon(icon);
setClosedIcon(icon);
return super.getTreeCellRendererComponent(tree, object, selected, expanded,leaf, row, hasFocus);
}
}
package explorer;
import java.io.*;
import java.io.FilenameFilter;
import java.io.File;
import javax.annotation.processing.Filer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
public class Searchfile {
static JList searchlist;
/*过滤器*/
static class MyFilenameFilter implements FilenameFilter{
private String suffix ="";//文件后缀
public MyFilenameFilter(String suffix){
this.suffix = suffix;
}
public boolean accept(File dir,String name){
//如果文件名后最与suffix指定的后最相同,就返回TRUE 否则返回False
if(new File(dir, name).isFile()){
return name.endsWith(suffix);
}else{//如果是文件
return true;
}
}
}
/*
* 列出目录下的所有文件,包括子目录的文件路径
*/
public static void listAllFiles(String dirname){
//如果文件不以文件分隔符结尾,则自动添加文件分隔符
if (!dirname.endsWith(File.separator)){
dirname = dirname + File.separator;
}
File dirfile =new File(dirname);
//如果dir对应的文件不存在,或者不是一个文件夹,则退出
if (!dirfile.exists()||(!dirfile.isDirectory())){//如果文件不存在或者目录不存在
//System.out.println("找不到目录"+dirname);
return;
}
//列出源文件夹下的所有文件
File[] files = dirfile.listFiles();
for (int i = 0;i<files.length;i++){
if(files[i].isFile()){
System.out.println(files[i].getAbsolutePath()+"是文件");
}else if(files[i].isDirectory()){
System.out.println(files[i].getAbsolutePath()+"是目录");
Searchfile.listAllFiles(files[i].getAbsolutePath());
}
}
}
/*
* 列出目录中通过文件过滤器过滤后的文件
*/
public static void listFilesByFilenameFilter(FilenameFilter filter,String dirname){
searchlist = new JList();
DefaultListModel model = new DefaultListModel<>();
searchlist.setModel(model);
//如果dir不以文件分隔符结尾,则自动添加文件分隔符
if(!dirname.endsWith(File.separator)){
dirname = dirname + File.separator;
}
File dirfile = new File(dirname);//??
//如果dir对应的文件不存在,或者不是一个文件夹,则退出
if (!dirfile.exists()||(!dirfile.isDirectory())){
System.out.println("找不到目录"+dirname);
return;
}
//列出原文件夹下所有能通过过滤器的文件包括子目录
File[] files = dirfile.listFiles(filter);
for (int i = 0;i<files.length;i++){
if(files[i].isFile()){
model.add(i, files[i]);
System.out.println(files[i].getAbsolutePath()+"11111是文件");
}else if(files[i].isDirectory()){
System.out.println(files[i].getAbsolutePath()+"是目录");
Searchfile.listFilesByFilenameFilter(filter, files[i].getAbsolutePath());
}
}
}
public static void intsss (){
JFrame searchfr = new JFrame("搜索");
searchfr.add(searchlist);
searchfr.setBounds(200, 100, 500, 400);
searchfr.setVisible(true);
}
}
- Jtree和Jlist,没有分清主次,到后面的时候导致程序出现了各种各样的问题。
- 开始没有考虑到全局,跟着自己的感觉做,导致后期写出来的代码很乱,一个方法里面写了好几百行,没有把类分好,后面扩充起来很难。
- 做程序前没有仔细的去思考和观察“我的电脑”,做出来的很多都是为了方便自己写代码而没有为用户考虑,出现了狠多细节上的不完善。
- 代码命名不够规范,直接用汉语拼音命名
- 以上代码是早期写的,很多地方不够规范,希望小伙伴学习的时候注意以上几点问题
- 链接https://download.csdn.net/download/qq_17284055/13507903