Java Swing:自定义压缩工具
Posted 你是小KS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Swing:自定义压缩工具相关的知识,希望对你有一定的参考价值。
1. 声明
当前内容主要为使用Swing来实现一个文件压缩器,借助Apache Commons来实现,内容借鉴:官方文档
主要涉及以下Swing组件
- JTable中自定义CellRenderer,CellEditor,隐藏列
- JProgressorBar的使用
- JDialog的使用
- 实现拖拽文件到table中
基本pom依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.9</version>
</dependency>
2. 界面展示
这里压缩后的文件是可以使用rar进行解压的
3. 压缩的实体的解释
对于压缩文件:aaa/aaa.txt,其中aaa就表示一个文件夹,aaa.txt就是该文件夹中的文件,但是在压缩的时候是只有一个实体名称aaa/aaa.txt,使用的却是aaa文件夹和aaa.txt文件,所以对于这个需要特殊的处理操作!
所以对于压缩文件来讲,只需要考虑文件的名称即可(文件夹在解压时会自动创建)
对于文件aaa.txt是放在根目录,那么只需要使用aaa.txt作为实体名称即可
4. 代码
1. 主程序:
package com.hy.test;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import com.hy.test.progressbar.SimpleProgressBarPanel;
import com.hy.test.progressbar.SimpleTask;
import com.hy.test.zip.compress.CompressCallback;
import com.hy.test.zip.compress.ZipCompressor;
import com.hy.test.zip.compress.impl.GzipCompressor;
import com.hy.test.zip.compress.impl.SevenZCompressor;
import com.hy.test.zip.compress.impl.ZipCompressorImpl;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import javax.swing.JRadioButton;
import java.awt.Font;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.EventObject;
import java.util.List;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.JPopupMenu;
import java.awt.event.MouseAdapter;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
/**
*
* @author hy
* @createTime 2022-10-09 12:44:50
* @description 用于实现压缩文件(目标,将多个文件添加,并压缩) 使用拖拽的方式实现列出需要压缩的文件的操作 选中,点击选中,就可以压缩
*
*/
public class ZipperFrame extends JFrame
private JPanel contentPane;
private JTable table;
private ButtonGroup radioButtonGroup;
// private ButtonGroup checkButtonGroup;
enum PressType
ZIP("zip"), JAR("jar"), GZIP("gzip"), SEVEN_ZIP("7z");
private String pressTypeName;
private PressType(String pressTypeName)
this.pressTypeName = pressTypeName;
public String getPressTypeName()
return pressTypeName;
/**
* Launch the application.
*/
public static void main(String[] args)
EventQueue.invokeLater(new Runnable()
public void run()
try
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
.getInstalledLookAndFeels())
if ("Nimbus".equals(info.getName()))
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
catch (ClassNotFoundException ex)
java.util.logging.Logger.getLogger(ZipFileExployerJFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null, ex);
catch (InstantiationException ex)
java.util.logging.Logger.getLogger(ZipFileExployerJFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null, ex);
catch (IllegalAccessException ex)
java.util.logging.Logger.getLogger(ZipFileExployerJFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null, ex);
catch (javax.swing.UnsupportedLookAndFeelException ex)
java.util.logging.Logger.getLogger(ZipFileExployerJFrame.class.getName())
.log(java.util.logging.Level.SEVERE, null, ex);
try
ZipperFrame frame = new ZipperFrame();
frame.setVisible(true);
catch (Exception e)
e.printStackTrace();
);
/**
* Create the frame.
*/
public ZipperFrame()
setTitle("文件压缩器-By Hy");
setBackground(Color.WHITE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 637, 454);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel topPanel = new JPanel();
topPanel.setBackground(Color.WHITE);
JLabel pressTypeLbl = new JLabel("压缩类型");
pressTypeLbl.setFont(new Font("宋体", Font.PLAIN, 15));
pressTypeLbl.setBackground(Color.WHITE);
topPanel.add(pressTypeLbl);
PressType[] values = PressType.values();
radioButtonGroup = new ButtonGroup();
for (PressType pressType : values)
JRadioButton zipRadioButton = new JRadioButton(pressType.pressTypeName);
zipRadioButton.setFont(new Font("宋体", Font.PLAIN, 15));
zipRadioButton.setBackground(Color.WHITE);
zipRadioButton.setSelected(true);
radioButtonGroup.add(zipRadioButton);
topPanel.add(zipRadioButton);
JButton pressButton = new JButton("压缩");
pressButton.setFont(new Font("宋体", Font.PLAIN, 15));
pressButton.setFocusable(false);
pressButton.addActionListener(this::onClickZipButton);
// pressButton.setBackground(Color.WHITE);
topPanel.add(pressButton);
contentPane.add(topPanel, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setFont(new Font("宋体", Font.PLAIN, 15));
scrollPane.setBackground(Color.WHITE);
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable()
@Override
public boolean isCellEditable(int row, int column)
return column==0;
;
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setFont(new Font("宋体", Font.PLAIN, 15));
table.setFillsViewportHeight(true);
scrollPane.setViewportView(table);
DropTarget dropTarget = new DropTarget(table, DnDConstants.ACTION_COPY_OR_MOVE, new TableDropTargetListener());
table.setDropTarget(dropTarget);
DefaultTableModel dataModel = buildTableModel();
// 修改表为不可编辑的
table.setModel(dataModel);
JPopupMenu popupMenu = new JPopupMenu();
addPopup(table, popupMenu);
JMenuItem removeMenuItem = new JMenuItem("移除");
removeMenuItem.addActionListener(this::onClickRemoveMenuItem);
popupMenu.add(removeMenuItem);
JTableHeader tableHeader = table.getTableHeader();
tableHeader.getColumnModel().getColumn(0).setPreferredWidth(50);
tableHeader.getColumnModel().getColumn(0).setWidth(50);
table.getColumnModel().getColumn(0).setPreferredWidth(50);
table.getColumnModel().getColumn(0).setWidth(50);
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
tableHeader.setResizingAllowed(false);
tableHeader.setReorderingAllowed(false);
int columnCount = table.getColumnCount();
/*
* table.getColumnModel().getColumn(1).setCellRenderer(new
* FileNameCellRender());
*/
table.getColumnModel().getColumn(0).setCellRenderer(new CheckBoxCellRender());
table.removeColumn(table.getColumnModel().getColumn(4)); // 隐藏file这一列
table.getColumnModel().getColumn(0).setCellEditor(new BooleanEditor());
// checkButtonGroup = new ButtonGroup();
PressType useCompressType = null;
// 处理当前的点击事件,点击压缩按钮触发的操作
private void onClickZipButton(ActionEvent e)
useCompressType = null;
Enumeration<AbstractButton> elements = radioButtonGroup.getElements();
while (elements.hasMoreElements())
JRadioButton radionButton = (JRadioButton) elements.nextElement();
if (radionButton.isSelected())
String text = radionButton.getText();
if (text.equals("7z"))
useCompressType = PressType.SEVEN_ZIP;
break;
text = text.toUpperCase();
useCompressType = PressType.valueOf(text);
break;
if (useCompressType == null)
JOptionPane.showMessageDialog(null, "未选择任何压缩类型", "错误", JOptionPane.ERROR_MESSAGE);
return;
System.out.printlnJava Swing 如何在我的自定义 ColorChooserPanel 中创建颜色样本?