20135313_exp4
实验四 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的自学并应用,我掌握了部分的界面设计技巧和语句。并且能够应用上一些显示输入输出提示、选择等功能的方法,作出了一些简单的界面。因为是面向代码去自学,去寻找实现方式,因此所学到的东西毕竟少,也比较片面,希望在以后的学习中能够掌握更多的界面设计技巧,将界面做的更加丰富,比如带图片,或者包含更多选择功能、提示功能。
20135313_exp4的更多相关文章
随机推荐
- 『ACM C++』 PTA 天梯赛练习集L1 | 027-028
死亡周二,今天去看惊奇队长了!!!真的很佩服国外的后期特效大片技术,要是我们国内也能实现这样的技术能力就好了~ 羡慕max -------------------------------------- ...
- java通过get或post方式传到PHP的某控制器的某方法下
[java]package test4;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStr ...
- Python-type函数使用
- Cloudera Manager 安装集群遇到的坑
Cloudera Manager 安装集群遇到的坑 多次安装集群,但每次都不能顺利,都会遇到很多很多的坑,今天就过去踩过的坑简单的总结一下,希望已经踩了的和正在踩的童鞋能够借鉴一下,希望对你们能有所帮 ...
- (杭电 2054)A==B?(这真是个巨坑)
A == B ? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 任意flex控件导出图片
任意flex控件导出图片 flex导出图片功能通常是: 思路1:客户端将UIComponent转化为BitmapData,再转为ByteArray,将ByteArray上传到服务端,服务端发送文件 ...
- 20155310《Java程序设计》实验五(网络编程与安全)实验报告
20155310<Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 •任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上 ...
- 20155316 实验四 《Android程序设计》
实验1 实验内容 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第二十四章: ...
- 20155321 2016-2017-2 《Java程序设计》第四周学习总结
20155321 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 第六.七章 继承 多态 接口 相应的语法细节 继承 关键字 extends 格式 class ...
- gitlab在push代码的时候报错
一.问题报错 gitlab在执行git pull origin master,拉取代码的时候报如下错误. $ git pull origin master remote: Counting objec ...