📄 frame1.java~35~
字号:
package zipfiles;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.io.*;import java.util.*;import java.util.zip.*;public class Frame1 extends JFrame { JPanel contentPane; VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout(); Button button1 = new Button(); Button button2 = new Button(); Label label1 = new Label(); File source = null , target = null; /**Construct the frame*/ public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); button1.setLabel("选择文件"); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); } }); contentPane.setLayout(verticalFlowLayout1); this.setResizable(false); this.setSize(new Dimension(350, 151)); this.setTitle("压缩文件演示程序"); button2.setLabel("输出压缩文件"); button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button2_actionPerformed(e); } }); label1.setText("所选择的文件:"); verticalFlowLayout1.setAlignment(VerticalFlowLayout.MIDDLE); verticalFlowLayout1.setHgap(15); verticalFlowLayout1.setVgap(15); contentPane.add(button1, null); contentPane.add(button2, null); contentPane.add(label1, null); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void button1_actionPerformed(ActionEvent e) { JFileChooser jfc = new JFileChooser(); String filepath = null; int returnVal = jfc.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION){ source = jfc.getSelectedFile(); filepath = source.getPath(); label1.setText("所选择的文件:"+filepath); } } void button2_actionPerformed(ActionEvent e) { JFileChooser jfc = new JFileChooser(); //ExtensionFileFilter filter = new ExtensionFileFilter(); //FileFilter filter = new FileFilter(); //filter.addExtension("zip"); //filter.setDescription("ZIP Files"); //jfc.setFileFilter(filter); String filepath = null; int returnVal = jfc.showSaveDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION){ target = jfc.getSelectedFile(); filepath = target.getPath(); label1.setText("正在压缩文件:"+filepath+"..."); zipFile(target); } } private void zipFile(File target){ if(source !=null){ try{ FileOutputStream f = new FileOutputStream(target); ZipOutputStream out = new ZipOutputStream(new DataOutputStream(f)); DataInputStream in = new DataInputStream(new FileInputStream(source)); out.putNextEntry(new ZipEntry(source.getPath())); int c; while((c = in.read()) != -1) out.write(c); in.close(); out.close(); label1.setText("文件压缩成功:"+target.getPath()); }catch(Exception e){ label1.setText("文件压缩失败:"+target.getPath()); } } else label1.setText("请先选择一个需要压缩的文件"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -