📄 huffmanframe.java
字号:
package org.galaxy_OPEN.www.datastructures.huffman;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import java.io.*;
import java.util.*;
public class HuffmanFrame extends JFrame implements ActionListener {
private FilterFileChooser fileChooser = null;
private JTextField filePathField = new JTextField(60);
private JButton jbOpen = new JButton("打开");
private JButton jbCompress = new JButton("压缩");
private JButton jbDecompress = new JButton("解压");
private JTextArea message = new JTextArea();
private File file = null;
// construct the frame
public HuffmanFrame() {
setTitle("文本压缩 - Galaxy-OPEN");
setSize(450, 350);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container content = getContentPane();
filePathField.setEditable(false);
filePathField.setBackground(Color.WHITE);
filePathField.setForeground(Color.BLACK);
jbOpen.addActionListener(this);
JPanel northPane = new JPanel();
northPane.setLayout(new FlowLayout());
northPane.add(filePathField);
northPane.add(jbOpen);
content.add(northPane, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(message);
setMessage("文件压缩程序,请选择文件导入。");
message.setEditable(false);
message.setWrapStyleWord(true);
content.add(scrollPane, BorderLayout.CENTER);
jbCompress.addActionListener(this);
jbDecompress.addActionListener(this);
JPanel southPane = new JPanel();
southPane.setLayout(new FlowLayout());
southPane.add(jbCompress);
southPane.add(jbDecompress);
content.add(southPane, BorderLayout.SOUTH);
// center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
// set look and feel
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "错误!",
"系统错误,即将退出。请与制作人员联系/n----GalaxyOPEN@gmail.com",
JOptionPane.ERROR);
// e.printStackTrace();
}
setResizable(false);
}
public void setMessage(String text) {
String date = (new Date()).toString();
date = date.substring(11, 19) + "-->";
message.insert(date + text + "\n", 0);
}
public boolean isOutputFileExists(String outPath) {
File outputFile = new File(outPath);
return outputFile.exists() ? true : false;
}
public String getOperate() {
String operate = null;
if (file != null) {
String name = file.getName();
int length = name.length();
if ((name.endsWith(".huf") || name.endsWith(".HUF"))
&& name.charAt(length - 4) == '.') {
operate = "decompress";
} else {
operate = "compress";
}
}
return operate;
}
private void openFile(String op) {
fileChooser = new FilterFileChooser(op);
int choice = fileChooser.showOpenDialog(this);
if (choice == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
if (file == null) {
return;
}
String filePath = file.getPath();
setMessage("已选择文件:" + filePath);
filePathField.setText(filePath);
}
}
private void compressFile() {
if (file == null) {
openFile("compress");
}
String operate = getOperate();
if (operate == null) {
return;
} else if (operate.equals("compress")) {
jbCompress.setEnabled(false);
String inPath = file.getPath();
String outPath = getPath("compress");
if (isOutputFileExists(outPath)) {
JOptionPane.showMessageDialog(this, "文件已经存在!", "警告",
JOptionPane.WARNING_MESSAGE);
setMessage(outPath + " 文件已经存在!");
jbCompress.setEnabled(true);
filePathField.setText("");
file = null;
return;
}
setMessage("准备压缩,请稍候……");
// compress
try {
Compress com = new Compress(this, inPath, outPath);
com.encode();
} catch (IOException exc) {
JOptionPane.showMessageDialog(this, "压缩文件\n" + outPath
+ "\n过程发生错误!", "I/O错误", JOptionPane.ERROR_MESSAGE);
}
jbCompress.setEnabled(true);
filePathField.setText("");
file = null;
}
}
private void decompressFile() {
if (file == null) {
openFile("decompress");
}
String operate = getOperate();
if (operate == null) {
return;
} else if (operate.equals("decompress")) {
jbDecompress.setEnabled(false);
String inPath = file.getPath();
String outPath = getPath("decompress");
if (isOutputFileExists(outPath)) {
JOptionPane.showMessageDialog(this, "文件已经存在!", "警告",
JOptionPane.WARNING_MESSAGE);
setMessage(outPath + " 文件已经存在!");
jbCompress.setEnabled(true);
filePathField.setText("");
file = null;
return;
}
setMessage("准备解压缩,请稍候……");
// decompress
try {
Decompress dec = new Decompress(this, inPath, outPath);
dec.decode();
} catch (IOException exc) {
exc.printStackTrace();
JOptionPane.showMessageDialog(this, "解压缩文件\n" + outPath
+ "\n过程发生错误!", "I/O错误", JOptionPane.ERROR_MESSAGE);
} catch (ClassNotFoundException exc) {
JOptionPane.showMessageDialog(this, "解压缩文件\n" + outPath
+ "\n过程发生错误!文件已被损坏!", "错误", JOptionPane.ERROR_MESSAGE);
}
jbDecompress.setEnabled(true);
filePathField.setText("");
file = null;
} else if (operate.equals("compress")) {
JOptionPane.showMessageDialog(null, "该文件尚未压缩,请选择“压缩”!", "错误!",
JOptionPane.ERROR_MESSAGE);
}
}
private String getPath(String op) {
String fileExt = null;
if (op.equals("compress")) {
fileExt = ".huf";
} else if (op.equals("decompress")) {
fileExt = "";
}
String filePath = null;
if (file != null) {
String fileName = file.getName();
filePath = file.getPath();
int extIndex = fileName.indexOf('.');
int nameIndex = filePath.indexOf(fileName);
fileName = fileName.substring(0, extIndex) + fileExt;
filePath = filePath.substring(0, nameIndex) + fileName;
}
return filePath;
}
public void actionPerformed(ActionEvent e) {
JButton jb = (JButton) e.getSource();
if (jb == jbOpen) {
openFile("all");
} else if (jb == jbCompress) {
compressFile();
} else if (jb == jbDecompress) {
decompressFile();
}
}
public static void main(String[] args) {
try {
HuffmanFrame mf = new HuffmanFrame();
mf.setVisible(true);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"系统内部错误,程序即将退出!请与制作人员联系/n----GalaxyOPEN@gmail.com", "系统错误",
JOptionPane.ERROR_MESSAGE);
}
}
public File getFile() {
return file;
}
}
// custom file chooser, only choose picture files
class FilterFileChooser extends JFileChooser {
private FileFilter[] fileFilter = new FileFilter[] { new HuffmanFileFilter(
"待解压文件(*.huf)", new String[] { ".huf", ".HUF" }) };
public FilterFileChooser(final String op) {
if (op.equals("decompress")) {
addChoosableFileFilter(fileFilter[0]);
}
setMultiSelectionEnabled(false);
}
// file filter
private class HuffmanFileFilter extends javax.swing.filechooser.FileFilter {
protected String desc;
protected String[] extensions;
public HuffmanFileFilter(final String desc, final String[] extensions) {
this.desc = desc;
this.extensions = (String[]) extensions.clone();
}
public boolean accept(final File f) {
if (f.isDirectory() == true) {
return true;
}
final String name = f.getName();
final int length = name.length();
for (int i = 0; i < extensions.length; i++) {
final String ext = extensions[i];
if (name.endsWith(ext)
&& name.charAt(length - ext.length()) == '.') {
return true;
}
}
return false;
}
public String getDescription() {
return desc;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -