功能较完善的文件管理器java课程设计报告毕业设计(论文)

更新时间:2023-12-24 11:47:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

功能较完善的文件管理器

一、设计任务与目标

实现一个能够对文件和文件夹进行各种基本操作的文件管理器窗口程序。 功能要求:

1)新建、删除、修改、复制、黏贴文件功能 2)新建、删除、修改目录

3)具有对某个文件进行压缩打包的功能 4)要求实现多文档界面

5)要求界面美观、功能布局合理。

二、方案设计与论证

根据题目的要求,我们想通过JTree来实现遍历文件夹并建成目录树,JList来实现多行显示,界面方面我们准备做成双栏式的,左边目录数,右边文件列表,通过阅读javax.swing.JFileChooser类的功能原码来逐步完善,新建、删除、复制等功能我们会通过java.io.File来了解文件的底层操作来完成。

三、程序框图或流程图,程序清单与调用关系

main主函数构造函数scaner()MainFrametreeExpandtreeCollapvalueChang类ed树侦听器sed树折叠ed树值变化 swap交换actionPerfsortElemensortElemenaddlist右边ormed事件监FileNode类ts排序ts排序文件列表听Clock类构造函数MainFrame()mousePressmouseClicked鼠标监听ed鼠标监听MainPanel类IOTest类构造函数构造函数构造函数ZIPUtil类newFolder(delFolder(delFile()))构造函数构造函数createTreeZhantie()Model() addActionListener监听器构造函数delAllFile()

四、全部源程序清单

/////////////// scaner类///////////////////// import javax.swing.*;

import javax.swing.event.*; import javax.swing.tree.*;

import java.awt.*;

import java.awt.event.*; import java.io.*; import java.util.*;

public class scaner extends JFrame

implements ActionListener,TreeExpansionListener,TreeSelectionListener {

/** * */

private static final long serialVersionUID = -6653169472316161951L; //定义常量

JTree jtree = new JTree(createTreeModel()); JPanel JPsub=new JPanel(new GridLayout(0,1)); JPanel road=new JPanel(new BorderLayout()); JFrame f,ff;

JSplitPane JSP; //可滚动 JButton sure,ok;

JLabel status=new JLabel(\); JLabel shijian,size;

JTextField renamest,zipname;

GregorianCalendar time = new GregorianCalendar(); int hour = time.get(Calendar.HOUR_OF_DAY); int min = time.get(Calendar.MINUTE); int second = time.get(Calendar.SECOND); Vector v; JToolBar statusBar; JPopupMenu popupMenu;

JMenuItem popupMenu_Copy, popupMenu_Delete,newfiles,p_zip; JMenuItem copy,del; JMenu m1, m2, m3;

JMenuItem newItem, exitItem, zuozhe, help; JButton btt; JList F_list; PopupMenu menu;

MenuItem b_open, b_copy, b_jiantie, b_del,newfile,b_rename,b_Zip,newfload; Object[] dizhi; FileDialog fd; long sizes; String newname;

//构造函数

public scaner() {

super(\文件管理器\); //设置窗口大小

Container container = getContentPane();///时钟布局 int i = 400; int j = 100;

setBounds(i,j,840,580);

addWindowListener(

new WindowAdapter() {

public void windowClosing(WindowEvent windowevent) {

System.exit(0); } } );

//////////////////////////////////////////////////////////// m1 = new JMenu(\文件\) ; m2 = new JMenu(\编辑\) ; m3 = new JMenu(\帮助\) ;

JMenuBar menuBar = new JMenuBar() ; menuBar.add(m1) ; menuBar.add(m2) ; menuBar.add(m3) ;

newItem = new JMenuItem(\新建文件\) ; exitItem = new JMenuItem(\退出\) ;

// 此时定义完了四个菜单的选项,定义完成之后,增加快捷键 newItem.setMnemonic('N') ; exitItem.setMnemonic('E') ;

newItem.setAccelerator(KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK)) ;

exitItem.setAccelerator(KeyStroke.getKeyStroke('E',java.awt.Event.ALT_MASK)) ;

newItem.addActionListener(this); ///注册监听器 exitItem.addActionListener(this); m1.add(newItem) ; m1.add(exitItem) ;

/////////////////////////////

copy = new JMenuItem(\复制\) ; del = new JMenuItem(\删除\) ;

// 此时定义完了两个菜单的选项,定义完成之后,增加快捷键

copy.setMnemonic('C') ;

copy.setAccelerator(KeyStroke.getKeyStroke('C',java.awt.Event.ALT_MASK)) ;

m2.add(copy) ; m2.add(del) ;

copy.addActionListener(this); del.addActionListener(this);

/////////////////////////

zuozhe = new JMenuItem(\关于\) ; help = new JMenuItem(\帮助\); m3.add(help); m3.add(zuozhe);

zuozhe.addActionListener(this); help.addActionListener(this); ////////////////////////////////

setJMenuBar(menuBar) ; // 菜单是需要通过此方法增加的 setVisible(true) ;

//frame.setSize(700,500) ; setLocation(300,200);

//////////////////////////////////////////////////// // -------------------创建右键弹出菜单 popupMenu = new JPopupMenu();

newfiles=new JMenuItem(\新建文件夹\);

popupMenu_Copy = new JMenuItem(\复制(C)\, 'C');

popupMenu_Delete = new JMenuItem(\删除(D)\, 'D'); p_zip =new JMenuItem(\添加到压缩文件\);

// ---------------向右键菜单添加菜单项和分隔符 popupMenu.addSeparator(); popupMenu.add(newfiles);

popupMenu.add(popupMenu_Copy); popupMenu.add(p_zip);

popupMenu.add(popupMenu_Delete); popupMenu.addSeparator();

// --------------------右键菜单注册事件 popupMenu_Copy.setActionCommand(\复制(C)\); popupMenu_Copy.addActionListener(this);

popupMenu_Delete.setActionCommand(\删除(D)\); popupMenu_Delete.addActionListener(this); newfiles.setActionCommand(\新建文件夹\); newfiles.addActionListener(this); p_zip.addActionListener(this); jtree.add(popupMenu);

///////////////////////////////////////////////////////////// menu = new PopupMenu();

newfload=new MenuItem(\新建文件夹\); newfile=new MenuItem(\新建文件\); b_open=new MenuItem(\打开\); b_copy=new MenuItem(\复制\);

b_Zip=new MenuItem(\添加到压缩文件\); b_del=new MenuItem(\删除\);

b_rename=new MenuItem(\重命名\); menu.addSeparator(); menu.add(b_open); menu.add(newfload); menu.add(newfile); menu.add(b_copy); menu.add(b_Zip); menu.add(b_rename); menu.add(b_del); menu.addSeparator();

b_open.addActionListener(this); newfload.addActionListener(this); b_copy.addActionListener(this); b_del.addActionListener(this); b_Zip.addActionListener(this);

b_rename.addActionListener(this); newfile.addActionListener(this); JPsub.add(menu);

////////////////////////////////////////////////////////////// //水平分割窗口,左scrollPane内放tree,右放JPsub用于显示文件 JSP=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(jtree),new JScrollPane(JPsub));

JSP.setDividerLocation (200);////////设置分隔条的位置。

JSP.setLastDividerLocation(200); /////// 将分隔条所处的最后位置设置为 newLastLocation。

getContentPane().add(JSP);

//背景色为白

JPsub.setBackground(Color.white); //////////////////////////////////////

/////////////////////////////////////////////时间///////////////////////

statusBar = new JToolBar();

shijian = new JLabel(\当前时间:\ + hour + \ + min + \ + second);

statusBar.addSeparator(); statusBar.add(shijian);

statusBar.setVisible(true);

container.add(statusBar, BorderLayout.SOUTH); statusBar.setVisible(true);

////////////////////////////////////////////// //给树添加展开监听器

jtree.addTreeExpansionListener(this); jtree.addTreeSelectionListener(this);

//设置树的外形 ,改变文件夹的图案 try {

UIManager.setLookAndFeel(\AndFeel\);

SwingUtilities.updateComponentTreeUI(this); }

catch (Exception ex) {}

jtree.setShowsRootHandles(true);

getContentPane().add(road,BorderLayout.NORTH); road.add(status,BorderLayout.WEST); ///西边....左边 Clock clock=new Clock();////线程 clock.start();

////////////////////////////////////////////////// f = new JFrame(\重命名\) ;

f.setSize(230,80) ; // 设置组件的大小

f.setBackground(Color.WHITE) ; // 将背景设置成白色 f.setLocation(300,200) ; // 设置组件的显示位置 renamest=new JTextField(); sure=new JButton(\确定\);

f.add(renamest,BorderLayout.CENTER); f.add(sure,BorderLayout.SOUTH); sure.addActionListener(this);

////////////////////////////////////////////////// ff = new JFrame(\压缩文件名\) ;

ff.setSize(230,80) ; // 设置组件的大小 ff.setBackground(Color.WHITE) ; // 将背景设置成白色 ff.setLocation(300,200) ; // 设置组件的显示位置 zipname=new JTextField(); ok=new JButton(\确定\);

ff.add(zipname,BorderLayout.CENTER); ff.add(ok,BorderLayout.SOUTH); ok.addActionListener(this); }

////////////实现复制,黏贴功能//////////////////////////// class MainFrame extends JFrame { /** * */

private static final long serialVersionUID = 1L;

public MainFrame() {

super(\);

setDefaultCloseOperation(HIDE_ON_CLOSE); setSize(500,260);

setLocation(300,200) ;

MainPanel pan = new MainPanel(); setContentPane(pan); setLayout(null); }

}

class MainPanel extends JPanel {

/** * */

private static final long serialVersionUID = 1L; JTextField txt;

JTextArea txtInFile; JTextArea txtOutFile; JFileChooser open; String strFilePath;

String strFilePathCopyTo; public MainPanel() {

JLabel lblIn = new JLabel(\源文件:\); lblIn.setBounds(30, 65, 50, 20); if(F_list.isSelectionEmpty())

txtInFile = new JTextArea(zhantie()); else

txtInFile = new JTextArea(zhantie()+F_list.getSelectedValue()); JScrollPane scrint = new JScrollPane(txtInFile) ; scrint.setBounds(80,65,265,40);

JButton btnSelect = new JButton(\选择文件或目录\); btnSelect.setBounds(350, 65, 120, 20);

open = new JFileChooser();/////////////////////

txtInFile = new JTextArea(zhantie()+F_list.getSelectedValue()); open.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); open.setCurrentDirectory(new File(zhantie())); btnSelect.addActionListener(new ActionListener() {

@SuppressWarnings(\)

public void actionPerformed(ActionEvent e) {

open.showOpenDialog(null);

strFilePath = open.getSelectedFile().getPath();///源文件路径 ((JButton)e.getSource()).setLabel(\已选\); txtInFile.setText(strFilePath);

System.out.println(txtInFile.getText());

System.out.println(\打开文件\); } } );

JLabel lblOut = new JLabel(\复制到:\); lblOut.setBounds(30, 125, 50, 20); txtOutFile = new JTextArea();

txtOutFile.setBounds(80,60,100,20);

JScrollPane scrout = new JScrollPane(txtOutFile) ; scrout.setBounds(80,125,265,40);

JButton btnSelectCopyTo = new JButton(\选择目标路径\); btnSelectCopyTo.setBounds(350, 125, 120, 20);

btnSelectCopyTo.addActionListener(new ActionListener() {

@SuppressWarnings(\)

public void actionPerformed(ActionEvent e) {

if(F_list.isSelectionEmpty())

open.setSelectedFile(new File(zhantie())); else

open.setSelectedFile(new

File(zhantie()+F_list.getSelectedValue())); open.showSaveDialog(null);

strFilePathCopyTo = open.getSelectedFile().getPath(); ((JButton)e.getSource()).setLabel(\已选\); txtOutFile.setText(strFilePathCopyTo);

System.out.println(txtOutFile.getText());

System.out.println(\打开文件\); } } );

JButton btnCopy = new JButton(\开始复制\); btnCopy.setBounds(130, 180, 90, 20);

btnCopy.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

本文来源:https://www.bwwdw.com/article/no35.html

Top