📄 archiverframe.java
字号:
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.*;
public class ArchiverFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JMenuBar menubar;
private JMenu menu;
private JMenuItem menuIF;
private JMenuItem menuIA;
private JMenuItem menuIQ;
private CompressFilePane cfpane=new CompressFilePane();
private JPanel btnPane=new JPanel();
private JButton chooseBtn =new JButton("Choose File");
private JButton compressBtn=new JButton("Compress");
private String path=null;
public void iniMenu()//initial menu"Select a File","About","Exit"
{
menubar=new JMenuBar();
setJMenuBar(menubar);
menu=new JMenu("Menu");
menuIF=new JMenuItem("Select a File",KeyEvent.VK_F);
menuIA=new JMenuItem("About",KeyEvent.VK_A);
menuIQ=new JMenuItem("Quit",KeyEvent.VK_Q);
menubar.add(menu);
menu.add(menuIF);
menu.add(menuIA);
menu.add(menuIQ);
menu.setMnemonic(KeyEvent.VK_M);
}
public void iniBtnPane()
{
btnPane.add(chooseBtn);
btnPane.add(compressBtn);
add(btnPane,BorderLayout.PAGE_END);
}
public ArchiverFrame()
{
super("MyArchiver");
iniMenu();
iniBtnPane();
addEvent();
this.add(cfpane);
}
class CloseActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
}
public void addEvent()
{
CloseActionListener closeAL=new CloseActionListener();
menuIF.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileChooser();
}
});
menuIA.addActionListener(new ActionListener()//about dialog
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "Author: XU Wenyi");
/*if(aboutDlg==null)
aboutDlg=new AboutDialogue(WordCalcFrame.this);
else
{
aboutDlg.pack();
aboutDlg.setLocationRelativeTo(null);
aboutDlg.setVisible(true);
}*/
}
});
menuIQ.addActionListener(closeAL);
chooseBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fileChooser();
}
});
compressBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
arhiveDir();
}
});
}
public void fileChooser()//choose the file
{
JFileChooser fChooser=new JFileChooser();// JFileChooser.DIRECTORIES_ONLY;
fChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal=fChooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
// System.out.println("You chose to open this file: " +
// fChooser.getSelectedFile().getName()+"\n"+fChooser.getSelectedFile().getPath());
}
path=fChooser.getSelectedFile().getPath();
File exf=new File(path+".zip");
if(exf.exists()){
Object[] options={"Continue","Cancel"};
int opt=JOptionPane.showOptionDialog(null, "Archive file already exsits,click Continue to overwrite","Warning",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,options,options[0]);
if(opt==0){//going to rewrite
cfpane.setPath(path);
cfpane.repaint();
}
}
else{
cfpane.setPath(path);
cfpane.repaint();
}
}
public void arhiveDir()
{
if(path==null)
System.out.println("Please choose a unzip file");
else{
try {
ZipDirectory zd=new ZipDirectory(path);
System.out.println(zd.getZipPath()+" "+zd.getZipName());
zd.zip();
System.out.println("compressed:"+zd.getCompressedSize());
System.out.println("orginal: "+zd.getOriginalSize());
System.out.println("Rate: "+zd.getCompressedRate()+"%");
cfpane.setCompressedSize(zd.getCompressedSize());
cfpane.setOriginalSize(zd.getOriginalSize());
cfpane.setCompressedRate(zd.getCompressedRate());
cfpane.repaint();
path=null;
} catch (Exception e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "The Directory You Choose Cannot Be Open ", "Alert", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -