增加按钮的监听事件,并实现jar文件内的文件名和大小。代码如下:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class JarTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 2851420353664542764L;
JFileChooser j = new JFileChooser();// 文件选择器
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JLabel l = new JLabel("文件:", JLabel.CENTER);
JTextField t = new JTextField(20);
JButton b = new JButton("选择");
String[] columns = new String[] { "文件名", "文件大小" };
JFileChooser jfc = new JFileChooser();// 文件选择器
DefaultTableModel dtm = new DefaultTableModel(null, columns);
JTable table = new JTable(dtm);
public JarTest() {
setTitle("截取Jar文件的文件属性");
Container c = getContentPane();
c.setLayout(new BorderLayout());// 边界布局管理
p1.setLayout(new FlowLayout(FlowLayout.CENTER));// p1的对齐方式为居中对齐
c.add(p1, BorderLayout.NORTH); // 将数据面板p1以边界布局方式添加到内容窗格的上方
p1.add(l);
p1.add(t);
p1.add(b);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int flag = jfc.showOpenDialog(null);
if (flag == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
t.setText(selectedFile.getAbsolutePath());
String[][] tableContent = getJarContent(selectedFile);
dtm.setDataVector(tableContent, columns);
}
}
});
p2.setLayout(new GridLayout());
c.add(p2, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(table);
p2.add(scrollPane);
}
/**
* 获取给定jar文件内的文件夹和文件名字、大小
* @param selectedFile
* @return
*/
private String[][] getJarContent(File selectedFile) {
String content[][] = null;
HashMap<String, Long> nameSizeMap = new HashMap<String, Long>();
try {
JarFile jarFile = new JarFile(selectedFile);
JarEntry jarEntry = null;
Enumeration<JarEntry> enume = jarFile.entries();
while (enume.hasMoreElements()) {
jarEntry = (JarEntry) enume.nextElement();
nameSizeMap.put(jarEntry.getName(), jarEntry.getSize());
}
List<Map.Entry<String, Long>> nameList = new ArrayList<Map.Entry<String, Long>>(
nameSizeMap.entrySet());
Collections.sort(nameList,
new Comparator<Map.Entry<String, Long>>() {
public int compare(Map.Entry<String, Long> o1,
Map.Entry<String, Long> o2) {
return (o1.getKey()).toString().compareTo(
o2.getKey());
}
});
content = new String[nameList.size()][2];
Entry<String, Long> entry = null;
for (int i = 0; i < content.length; i++) {
entry = nameList.get(i);
content[i][0]= entry.getKey();
content[i][1]= entry.getValue().toString();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return content;
}
public static void main(String[] args) {
JarTest jt = new JarTest();
jt.setSize(400, 400);
jt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jt.setLocationRelativeTo(null);
jt.setVisible(true);
}
}
测试结果: