欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java制作可视化的简易压缩工具

程序员文章站 2024-03-24 22:44:22
...

在上一个博客上我们介绍了eclipse中windowbuilder的安装,接下来我们用这个工具做出一个简易的压缩工具界面:

在一个项目里的包中点击新建——>其他——>windowbuilder——>swing designer——>application wiodow

java制作可视化的简易压缩工具

java制作可视化的简易压缩工具

点击下一步输入一个类名

java制作可视化的简易压缩工具

点击完成后会自动生成如下窗口:

java制作可视化的简易压缩工具

点击左侧的frame 左下方会有这个窗口的属性如下:

java制作可视化的简易压缩工具

在title中输入这个窗口的名字点击回车如下:

java制作可视化的简易压缩工具

接下来我就把这个工具慢慢做完给出代码:

页面制作如下:

java制作可视化的简易压缩工具

下面完成各按钮的功能

点击开始压缩  如下:

java制作可视化的简易压缩工具

完整代码如下:

import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

public class Compression {

	private JFrame frmJava;
	private JTextField textField;
	public String path;
	public String name;
	private JTextField textField_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Compression window = new Compression();
					window.frmJava.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Compression() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmJava = new JFrame();
		frmJava.getContentPane().setFont(new Font("隶书", Font.BOLD, 20));
		frmJava.setTitle("java\u538B\u7F29\u5DE5\u5177");
		frmJava.setBounds(100, 100, 704, 262);
		frmJava.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel label = new JLabel("\u9009\u62E9\u8981\u538B\u7F29\u7684\u6587\u4EF6\uFF1A");
		label.setFont(new Font("隶书", Font.BOLD, 20));
		
		textField = new JTextField();
		textField.setColumns(10);
		JLabel label_1 = new JLabel("\u538B\u7F29\u540E\u7684\u6587\u4EF6\u540D\u5B57\uFF1A");
		label_1.setFont(new Font("隶书", Font.BOLD, 20));
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		JButton button = new JButton("\u9009\u62E9\u6587\u4EF6");
		button.setFont(new Font("隶书", Font.BOLD, 20));
		
		JButton button_1 = new JButton("\u5F00\u59CB\u538B\u7F29");
		button_1.setFont(new Font("隶书", Font.BOLD, 20));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser filechooser = new JFileChooser();
				int returnValue = filechooser.showOpenDialog(filechooser);
				{
					
				if(returnValue == JFileChooser.APPROVE_OPTION)
				{
					File file = filechooser.getSelectedFile();
					
					path=file.getParent();
					name = file.getName();
					textField.setText(path);
				}
			}
			}
		});
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					byte[] buffer = new byte[1024];
					String name1=textField_1.getText();
					FileOutputStream fos = new FileOutputStream(path+ "/"+ name1);
					ZipOutputStream zos = new ZipOutputStream(fos);//创建。。。类的对象
					ZipEntry ze = new ZipEntry(name);//压缩的入口处
					zos.putNextEntry(ze);
					//开始一个新的zipentry
					FileInputStream in = new FileInputStream(path+"/"+ name);//创建。。。对象
					
					int len;
					while ((len = in.read(buffer)) > 0) {
						zos.write(buffer, 0, len);
						//当文件压缩到底的时候
					}
					in.close();//关闭流
					zos.closeEntry();//关闭压缩入口
					zos.close();//关闭
					JOptionPane.showMessageDialog(null, "提示消息:", "压缩完成",JOptionPane.PLAIN_MESSAGE);  
					
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}

			
		});
		
		
		GroupLayout groupLayout = new GroupLayout(frmJava.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(label)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, 260, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(259)
							.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 151, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 260, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(63, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(76)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(button))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
					.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		frmJava.getContentPane().setLayout(groupLayout);
	}
}