📄 compressframe.java~32~
字号:
package zipCompress;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.io.File;
import java.sql.Timestamp;
import java.io.*;
import java.util.zip.ZipFile;
import java.util.zip.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class CompressFrame
extends JFrame {
//自定义变量:
String currentDirectory="." ; //当前工作目录
ArrayList fileList = new ArrayList(20); //存储当前目录下的所有文件的对应文件对象
FileTableModel tableModel;
JTable fileTable;
File workDirectory;
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
JToolBar jToolBar = new JToolBar();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
ImageIcon image1;
ImageIcon image2;
ImageIcon image3;
JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JMenuItem jMenuItemCD = new JMenuItem();
JMenuItem jMenuItemDelete = new JMenuItem();
JMenuItem jMenuItemRename = new JMenuItem();
JMenu jMenuCommand = new JMenu();
JMenuItem jMenuItemCompress = new JMenuItem();
JMenuItem jMenuItemExtract = new JMenuItem();
JMenuItem jMenuItemInfor = new JMenuItem();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JPanel jPanel2 = new JPanel();
BorderLayout borderLayout3 = new BorderLayout();
JTextField directoryFeild = new JTextField();
JButton jButton4 = new JButton();
JPanel jPanel3 = new JPanel();
BorderLayout borderLayout4 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
JButton jButton7 = new JButton();
JButton jButton3 = new JButton();
JButton jButton8 = new JButton();
JButton jButton9 = new JButton();
//Construct the frame
public CompressFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Java Compress Utility");
statusBar.setToolTipText("");
statusBar.setText(" 欢迎使用java压缩工具");
jMenuFile.setText("文件");
jMenuFileExit.setText("退出");
jMenuFileExit.addActionListener(new
ComptessFrame_jMenuFileExit_ActionAdapter(this));
jMenuHelp.setText("帮助");
jMenuHelpAbout.setActionCommand("about");
jMenuHelpAbout.setText("关于...");
jMenuHelpAbout.addActionListener(new
ComptessFrame_jMenuHelpAbout_ActionAdapter(this));
jButton1.setIcon(new ImageIcon(CompressFrame.class.getResource("cd5.png")));
jButton1.addActionListener(new ComptessFrame_jButton1_actionAdapter(this));
jButton1.setToolTipText("更换工作目录");
jButton2.setIcon(new ImageIcon(CompressFrame.class.getResource("delete.png")));
jButton2.setText("");
jButton2.addActionListener(new ComptessFrame_jButton2_actionAdapter(this));
jButton2.setMaximumSize(new Dimension(45, 43));
jButton2.setToolTipText("删除选定文件");
jMenuItemCD.setToolTipText("改变工作目录");
jMenuItemCD.setActionCommand("cd");
jMenuItemCD.setText("工作目录");
jMenuItemCD.addActionListener(new ComptessFrame_jMenuItemCD_actionAdapter(this));
jMenuItemDelete.setToolTipText("删除选定的定文件");
jMenuItemDelete.setActionCommand("delete");
jMenuItemDelete.setText("删除");
jMenuItemDelete.addActionListener(new ComptessFrame_jMenuItemDelete_actionAdapter(this));
jMenuItemRename.setToolTipText("重命名选定的文件");
jMenuItemRename.setActionCommand("rename");
jMenuItemRename.setText("重命名");
jMenuItemRename.addActionListener(new ComptessFrame_jMenuItemRename_actionAdapter(this));
jMenuCommand.setToolTipText("");
jMenuCommand.setActionCommand("");
jMenuCommand.setText("命令");
jMenuItemCompress.setToolTipText("压缩选定的文件");
jMenuItemCompress.setActionCommand("compress");
jMenuItemCompress.setText("压缩");
jMenuItemCompress.addActionListener(new CompressFrame_jMenuItemCompress_actionAdapter(this));
jMenuItemExtract.setToolTipText("解压选定的zip文件");
jMenuItemExtract.setActionCommand("extract");
jMenuItemExtract.setText("解压缩");
jMenuItemExtract.setArmed(false);
jMenuItemExtract.addActionListener(new CompressFrame_jMenuItemExtract_actionAdapter(this));
jMenuItemInfor.setToolTipText("察看压缩比等信息i");
jMenuItemInfor.setActionCommand("infor");
jMenuItemInfor.setText("信息");
jMenuItemInfor.addActionListener(new CompressFrame_jMenuItemInfor_actionAdapter(this));
jPanel1.setLayout(borderLayout2);
jPanel2.setLayout(borderLayout3);
jButton4.setToolTipText("返回上一级目录");
jButton4.setActionCommand("jButton4");
jButton4.setIcon(new ImageIcon(CompressFrame.class.getResource(
"arrow-up.png")));
jButton4.setText("");
jButton4.addActionListener(new ComptessFrame_jButton4_actionAdapter(this));
jPanel3.setLayout(borderLayout4);
directoryFeild.setToolTipText("当前工作目录");
directoryFeild.setEditable(false);
directoryFeild.setText("");
jButton5.setAlignmentY( (float) 0.5);
jButton5.setToolTipText("压缩选定文件");
jButton5.setIcon(new ImageIcon(CompressFrame.class.getResource("ark2.png")));
jButton5.setText("");
jButton5.addActionListener(new ComptessFrame_jButton5_actionAdapter(this));
jButton6.setMaximumSize(new Dimension(45, 43));
jButton6.setMinimumSize(new Dimension(45, 43));
jButton6.setToolTipText("解压缩指定文件");
jButton6.setIcon(new ImageIcon(CompressFrame.class.getResource(
"extract2.png")));
jButton6.setMargin(new Insets(2, 14, 2, 14));
jButton6.setText("");
jButton6.addActionListener(new CompressFrame_jButton6_actionAdapter(this));
jButton7.setToolTipText("查看压缩文件信息");
jButton7.setIcon(new ImageIcon(CompressFrame.class.getResource("info.png")));
jButton7.setText("");
jButton7.addActionListener(new CompressFrame_jButton7_actionAdapter(this));
jButton3.setToolTipText("重命名文件");
jButton3.setHorizontalTextPosition(SwingConstants.TRAILING);
jButton3.setIcon(new ImageIcon(CompressFrame.class.getResource("rename.png")));
jButton3.setText("");
jButton3.addActionListener(new ComptessFrame_jButton3_actionAdapter(this));
jButton8.setToolTipText("帮助");
jButton8.setIcon(new ImageIcon(CompressFrame.class.getResource("help.png")));
jButton8.setText("");
jButton8.addActionListener(new ComptessFrame_jButton8_actionAdapter(this));
jButton9.setToolTipText("刷新");
jButton9.setIcon(new ImageIcon(CompressFrame.class.getResource("reload.png")));
jButton9.setText("");
jButton9.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
jButton9.addActionListener(new CompressFrame_jButton9_actionAdapter(this));
jMenuFile.add(jMenuItemCD);
jMenuFile.addSeparator();
jMenuFile.add(jMenuItemDelete);
jMenuFile.add(jMenuItemRename);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuCommand);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(jToolBar, BorderLayout.NORTH);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jPanel2, BorderLayout.NORTH);
jPanel2.add(directoryFeild, BorderLayout.CENTER);
jPanel2.add(jButton4, BorderLayout.WEST);
jPanel1.add(jPanel3, BorderLayout.CENTER);
jPanel3.add(jScrollPane1, BorderLayout.CENTER);
/***********************FileTable初始化*******************************/
initializeFileList(); //首先初始化ArrayList
tableModel = new FileTableModel();
fileTable = new JTable(tableModel);
//设置双击则打开文件夹事件:
fileTable.addMouseListener(new ComptessFrame_fileTable_mouseAdapter(this));
jScrollPane1.getViewport().add(fileTable, null);
jMenuCommand.add(jMenuItemCompress);
jMenuCommand.add(jMenuItemExtract);
jMenuCommand.addSeparator();
jMenuCommand.add(jMenuItemInfor);
jToolBar.add(jButton5, null);
jToolBar.add(jButton6, null);
jToolBar.add(jButton7, null);
jToolBar.add(jButton9, null);
jToolBar.add(jButton1);
jToolBar.add(jButton3, null);
jToolBar.add(jButton2);
jToolBar.add(jButton8, null);
}
/*************************初始化ArrayList*******************************/
private void initializeFileList() {
fileList.clear();
workDirectory = new File(currentDirectory);
this.directoryFeild.setText(workDirectory.getAbsolutePath());
this.currentDirectory = workDirectory.getAbsolutePath();
File[] children = workDirectory.listFiles();
for (int i = 0; i < children.length; i++) {
fileList.add(children[i]);
}
}
/**********用于与CompressDialog和ExtractDialog交互*****************/
public File[] getFilesSelected(){
int[] rowsSelected=fileTable.getSelectedRows();
File[] temp=new File[rowsSelected.length];
for(int i=0;i<rowsSelected.length;i++){
temp[i]=(File)fileList.get(rowsSelected[i]);
}
return temp;
}
public String getCurrentDirectry(){
return this.currentDirectory;
}
public void reDisplay(){
this.tableModel.fireTableDataChanged();
}
//File | Exit action performed
public void jMenuFileExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
//Help | About action performed
public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
CompressFrame_AboutBox dlg = new CompressFrame_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation( (frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.pack();
dlg.show();
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
jMenuFileExit_actionPerformed(null);
}
}
//表格数据模型类,利用主类的ArrayList做缓冲:
private class FileTableModel
extends AbstractTableModel {
public String getColumnName(int column) {
if (column == 0) {
return "文件名";
}
else if (column == 1) {
return "类型";
}
else if (column == 2) {
return "大小";
}
else { //column=3
return "最后修改时间";
}
}
public int getColumnCount() {
return 4;
}
public int getRowCount() {
return fileList.size();
}
public Object getValueAt(int row, int column) {
File file = (File) fileList.get(row);
if (column == 0) {
return file.getName();
}
if (column == 1) {
return file.isDirectory() ? "文件夹" : "文件";
}
if (column == 2) {
if (file.isDirectory()) {
return "";
}
long size = file.length();
long kb = size / 1024;
if (kb < 1024) {
return kb + "KB";
}
return kb / 1024 + "MB";
}
if (column == 3) {
long ms = file.lastModified();
Timestamp timestamp = new Timestamp(ms);
return timestamp;
}
return null;
}
}
void jButton4_actionPerformed(ActionEvent e) {
currentDirectory = workDirectory.getAbsolutePath();
int lastIndex = currentDirectory.lastIndexOf('\\');
if (lastIndex != -1 && lastIndex != currentDirectory.length()) {
currentDirectory = currentDirectory.substring(0, lastIndex);
}
if (currentDirectory.indexOf('\\') == -1) { //到达根目录
currentDirectory += "\\";
}
this.initializeFileList();
tableModel.fireTableDataChanged();
}
void fileTable_mouseClicked(MouseEvent e) {
if (e.getClickCount() != 2) { //不是双击,则返回
return;
}
int rowClicked = fileTable.getSelectedRow(); //双击时行被默认选中
File fileMapped = (File) fileList.get(rowClicked); //利用表格与ArrayList内容的对应关系
if (fileMapped.isDirectory() == false) { //未双击文件夹,返回
return;
}
currentDirectory = fileMapped.getAbsolutePath();
this.initializeFileList();
this.tableModel.fireTableDataChanged();
}
private void deleteFiles(){
int[] rowSelected = fileTable.getSelectedRows();
if (rowSelected == null || rowSelected.length == 0) {
return;
}
int choice = JOptionPane.showConfirmDialog(this, "确认删除所选文件吗?", "",
JOptionPane.OK_CANCEL_OPTION);
File temp;
if (choice == JOptionPane.OK_OPTION) {
for (int i = 0; i < rowSelected.length; i++) {
temp = (File) fileList.get(rowSelected[i]);
if (!temp.delete()) {
JOptionPane.showMessageDialog(this,
"无法删除文件:\n" + temp.getAbsolutePath() +
"\n请确认文件夹非空或文件未被使用",
"删除文件出错", JOptionPane.ERROR_MESSAGE);
}
}
this.initializeFileList();
tableModel.fireTableDataChanged();
}
}
void jButton2_actionPerformed(ActionEvent e) {
this.deleteFiles();
}
void jMenuItemDelete_actionPerformed(ActionEvent e) {
this.deleteFiles();
}
private void changeDirectory(){
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(workDirectory);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setMultiSelectionEnabled(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -