实验四 GUI界面的设计和运用
20135313吴子怡
一、实验目的
结合项目,为每个密码学算法设计能够提供使用者用户体验的操作界面,实现加解密、求得消息摘要的功能。
二、代码举例(备注:其中所使用的加解密方法均为未调库自己编写的,在此处不粘贴展示,但算法代码在所导入的Algorithm包内。)
1、AES加解密算法
(1)代码
package Main;
import Algorithm.*;
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.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class AESMain extends JFrame implements ActionListener {
private JLabel sourceLabel, aimLabel, keyLabel;
private JTextField sourceText, aimText, keyText;
private JButton encryptBt, decryptBt, clearBt, exitBt, selectSourceBt,
selectAimBt;
private JRadioButton methodRadioButtons = new JRadioButton();
public static void main(String[] args) {
new AESMain("AES");
}
public AESMain(String title) {
super(title);
// 初始化组件
keyLabel = new JLabel("密钥", SwingConstants.RIGHT);
sourceLabel = new JLabel("源字符串", SwingConstants.RIGHT);
aimLabel = new JLabel("目标字符串", SwingConstants.RIGHT);
keyText = new JTextField(20);
sourceText = new JTextField(20);
aimText = new JTextField(20);
encryptBt = new JButton("加密");
decryptBt = new JButton("解密");
clearBt = new JButton("清空");
exitBt = new JButton("退出");
// 划分面板
Container c = this.getContentPane();
c.setLayout(new BorderLayout());
JPanel centerPanel = new JPanel();
JPanel northPanel = new JPanel();
JPanel southPanel = new JPanel();
JPanel eastPanel = new JPanel();
JPanel westPanel = new JPanel();
c.add(eastPanel, BorderLayout.EAST);
c.add(westPanel, BorderLayout.WEST);
northPanel.setLayout(new GridLayout(1, 3));
JPanel northLeftPanel = new JPanel();
JPanel northCenterPanel = new JPanel();
JPanel northRightPanel = new JPanel();
northCenterPanel.setLayout(new FlowLayout());
northPanel.add(northLeftPanel);
northPanel.add(northCenterPanel);
northPanel.add(northRightPanel);
c.add(northPanel, BorderLayout.NORTH);
southPanel.setLayout(new GridLayout(1, 2));
JPanel southLeftPanel = new JPanel(new FlowLayout());
JPanel southRightPanel = new JPanel(new FlowLayout());
southLeftPanel.add(encryptBt);
southLeftPanel.add(decryptBt);
southRightPanel.add(clearBt);
southRightPanel.add(exitBt);
southPanel.add(southLeftPanel);
southPanel.add(southRightPanel);
c.add(southPanel, BorderLayout.SOUTH);
centerPanel.setLayout(new BorderLayout());
JPanel centerEastPanel = new JPanel(new GridLayout(3, 1));
JPanel centerWestPanel = new JPanel(new GridLayout(3, 1));
JPanel centerCenterPanel = new JPanel(new GridLayout(3, 1));
JPanel centerSouthPanel = new JPanel();
JPanel centerNorthPanel = new JPanel();
JPanel panel1 = new JPanel(new FlowLayout());
JPanel panel2 = new JPanel(new FlowLayout());
JPanel panel3 = new JPanel(new FlowLayout());
JPanel panel4 = new JPanel(new FlowLayout());
JPanel panel5 = new JPanel(new FlowLayout());
JPanel panel6 = new JPanel(new FlowLayout());
JPanel panel7 = new JPanel(new FlowLayout());
JPanel panel8 = new JPanel(new FlowLayout());
JPanel panel9 = new JPanel(new FlowLayout());
panel1.add(sourceLabel);
panel2.add(sourceText);
panel4.add(aimLabel);
panel5.add(aimText);
panel7.add(keyLabel);
panel8.add(keyText);
centerWestPanel.add(panel1);
centerWestPanel.add(panel4);
centerWestPanel.add(panel7);
centerCenterPanel.add(panel2);
centerCenterPanel.add(panel5);
centerCenterPanel.add(panel8);
centerEastPanel.add(panel3);
centerEastPanel.add(panel6);
centerEastPanel.add(panel9);
centerPanel.add(centerEastPanel, BorderLayout.EAST);
centerPanel.add(centerWestPanel, BorderLayout.WEST);
centerPanel.add(centerCenterPanel, BorderLayout.CENTER);
centerPanel.add(centerSouthPanel, BorderLayout.SOUTH);
centerPanel.add(centerNorthPanel, BorderLayout.NORTH);
c.add(centerPanel, BorderLayout.CENTER);
// 加入按钮监听
encryptBt.addActionListener(this);
decryptBt.addActionListener(this);
clearBt.addActionListener(this);
exitBt.addActionListener(this);
methodRadioButtons.addActionListener(this);
// 设置关闭按钮
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
// invoke exit in outer class
System.exit(0);
}
});
pack();
// 设置界面大小
setSize(400, 300);
setLocation(0, 0);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(methodRadioButtons)) {
sourceLabel.setText("源字符串");
aimLabel.setText("目标字符串");
selectSourceBt.setEnabled(false);
selectAimBt.setEnabled(false);
clear();
}
if (e.getSource().equals(selectSourceBt)) {
String sourceFile = selectFile();
sourceText.setText(sourceFile);
}
if (e.getSource().equals(selectAimBt)) {
String aimFile = selectFile();
aimText.setText(aimFile);
}
if (e.getSource().equals(encryptBt)) {
encrypt();
}
if (e.getSource().equals(decryptBt)) {
decrypt();
}
if (e.getSource().equals(exitBt)) {
System.exit(0);
}
if (e.getSource().equals(clearBt)) {
clear();
keyText.setText("");
}
}
private String selectFile() {
String rootine = "";
File aFile = null; // 接收文件
int result = 0; // 接收操作状态
JFileChooser fileChooser = new JFileChooser(); // 文件选择框
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
fileChooser.setMultiSelectionEnabled(false); // 不允许一次选择多个文件
fileChooser.setCurrentDirectory(new File("..\\AES")); // 设置默认文件夹路径
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); // 只允许选择文件,不允许选择文件夹
// 设置文件过滤器
result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) // 选择的是确定按钮
{
aFile = fileChooser.getSelectedFile(); // 得到选择的文件
rootine = aFile.getPath();
} else if (result == JFileChooser.CANCEL_OPTION) {
}// 选择取消,文件选择框正常关闭
else {
JOptionPane.showMessageDialog(fileChooser, "文件选择框出现异常!");
}
return rootine;
}
public void encrypt() {
String source = sourceText.getText();
String key = keyText.getText();
try {
String result = new AES().textEncrypt(source, key);
aimText.setText(result);
JOptionPane.showMessageDialog(this, "加密完成! ");
return;
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "加密失败! ");
return;
}
}
public void decrypt() {
String source = sourceText.getText();
String key = keyText.getText();
try {
String result = new AES().textDecrypt(source, key);
aimText.setText(result);
JOptionPane.showMessageDialog(this, "解密完成! ");
return;
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "解密失败! ");
return;
}
}
private void clear() {
sourceText.setText("");
aimText.setText("");
}
}
(2)运行展示
2、MD5消息摘要算法
(1)代码
package Main;
import javax.swing.JOptionPane;
import Algorithm.MD5;
public class MD5Main {
public static void main(String[] args) {
String s1;
s1 = JOptionPane.showInputDialog("请输入明文,将使用MD5算法进行加密.");
String result = new MD5().calcMD5(s1);
JOptionPane.showMessageDialog(null, "消息摘要为:" + result);
}
}
(2)运行展示
三、心得体会
通过这次GUI的自学并应用,我掌握了部分的界面设计技巧和语句。并且能够应用上一些显示输入输出提示、选择等功能的方法,作出了一些简单的界面。因为是面向代码去自学,去寻找实现方式,因此所学到的东西毕竟少,也比较片面,希望在以后的学习中能够掌握更多的界面设计技巧,将界面做的更加丰富,比如带图片,或者包含更多选择功能、提示功能。