- package com.my.ui;
- import com.my.service.EncryptService;
- import
- javax.swing.*
- ;
- import
- java.awt.*
- ;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- public class
- Main
- extends
- JFrame
- implements ActionListener {
- private
- EncryptService encryptService =
- new EncryptService();
- // 设置默认大小
- private static final int
- DEFAULT_WIDTH = 396
- ;
- private static final int
- DEFAULT_HEIGHT = 145
- ;
- // 组件
- private JFileChooser chooser;
- private JButton buttonEncrypt;
- private JButton buttonDecrypt;
- private JButton buttonMakeKey;
- JTextField fileText;
- JTextField keyText;
- // 文件路径
- private String filePath;
- private String keyPath;
- // 初始化加密页面
- public Main() {
- setTitle(
- "文件加密程序"
- );
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
- setResizable(false);
- setLocationRelativeTo(null);
- JPanel panUser
- =
- new JPanel();
- // 创建组件
- fileText =
- new JTextField();
- fileText.setEditable(false);
- keyText
- =
- new JTextField();
- keyText.setEditable(false);
- JButton btnFile
- =
- new
- JButton("...."
- );
- btnFile.setFocusPainted(false);
- JButton btnKey
- =
- new
- JButton("..."
- );
- btnKey.setFocusPainted(false);
- btnKey.setEnabled(false);
- // 布局
- panUser.setLayout(
- new
- GridLayout(2, 3
- ));
- panUser.add(new
- JLabel("源文件路径:"
- ));
- panUser.add(fileText);
- panUser.add(btnFile);
- panUser.add(new
- JLabel("密匙路径:"
- ));
- panUser.add(keyText);
- panUser.add(btnKey);
- buttonEncrypt
- =
- new
- JButton("加密"
- );
- buttonEncrypt.setFocusPainted(false);
- buttonDecrypt
- =
- new
- JButton("解密"
- );
- buttonDecrypt.setFocusPainted(false);
- buttonMakeKey
- =
- new
- JButton("生成新的密匙"
- );
- buttonMakeKey.setFocusPainted(false);
- JPanel panBtn
- =
- new JPanel();
- panBtn.setLayout(new FlowLayout());
- panBtn.add(buttonEncrypt);
- panBtn.add(buttonDecrypt);
- panBtn.add(buttonMakeKey);
- setLayout(new BorderLayout());
- add(panUser, BorderLayout.CENTER);
- add(panBtn, BorderLayout.SOUTH);
- // 注册事件监听
- btnFile.addActionListener(
- this);
- btnKey.addActionListener(this);
- buttonMakeKey.addActionListener(this);
- buttonEncrypt.addActionListener(this);
- buttonDecrypt.addActionListener(this);
- chooser
- =
- new JFileChooser();
- chooser.setCurrentDirectory(new
- File("."
- ));
- }
- public static void main(String[] args) {
- JFrame frame
- =
- new Main();
- frame.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if
- (e.getActionCommand().equals("...."
- )) {
- int
- result = chooser.showOpenDialog(
- null);
- if
- (result ==
- JFileChooser.APPROVE_OPTION) {
- filePath
- =
- chooser.getSelectedFile().getPath();
- fileText.setText(filePath);
- }
- }
- if
- (e.getActionCommand().equals("..."
- )) {
- int
- result = chooser.showOpenDialog(
- null);
- if
- (result ==
- JFileChooser.APPROVE_OPTION) {
- keyPath
- =
- chooser.getSelectedFile().getPath();
- keyText.setText(keyPath);
- }
- }
- if
- (e.getActionCommand().equals("加密"
- )) {
- encryptService.encryptFile(new File(filePath));
- System.out.println(
- "加密成功"
- );
- }
- if
- (e.getActionCommand().equals("解密"
- )) {
- encryptService.decryptFile(new File(filePath));
- System.out.println(
- "解密成功"
- );
- }
- if
- (e.getActionCommand().equals("生成新的密匙"
- )) {
- encryptService.makeKey();
- keyText.setText(new
- File("").getAbsolutePath() + "\\KEY"
- );
- System.out.println(
- "成功生成新的密匙"
- );
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2150569.html