当前位置:   article > 正文

java之简单的加密与解密系统(图形用户界面)_加解密界面

加解密界面

java之简单的加密与解密系统(图形用户界面)

实验思路:
(1)先想象有什么组件,然后用代码描述出来
(2)看看将这些组件怎么布局比较好
(3)选择什么样的加密方式去加密
(4)怎么去实现鼠标的控制事件
(5)编程实现这些预想功能

具体实现内容:
(1)输入:可以输入任意字符,对其均可进行加密操作
(2)加密:采用的是将java的String类型强制转化为char类型,在分别在ASCII码上加上2,然后在输出加密后的字符
(3)解密:将输入的字符串给一个变量,点击解密按钮在给予输出
(4)输出:点击加密按钮输出的就是加密后的字符,点击解密输出的就是解密后的代码
(5)退出:点击可以退出该程序并关闭该软件

实现代码:

package demo1;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public  class Test2 extends JFrame   {
	public static void main(String[] args) {
		Test2 test=new Test2();
	}
	  JButton button1=new JButton("加密");
	  JButton button2=new JButton("退出");
	  JTextField textfield=new JTextField();
	  JLabel usernamelabel1 =new JLabel("输入");
	  JLabel usernamelabel2=new JLabel("输出");
	  JTextField textfield2=new JTextField();
	public Test2() {
		  super();
		  setVisible(true);
		  setBounds(650, 300, 500, 500);
		  setTitle("加密与解密");
		  setDefaultCloseOperation(3);
		  setLayout(null);

		  usernamelabel1.setBounds(150,00,200,200);//标签用户名的位置及大小
		  add(usernamelabel1);
		  

		  textfield.setBounds(200,89, 200, 30); //文本域的大小和位置
		  add(textfield);
		  

		  usernamelabel2.setBounds(150, 65, 200, 200);
		  add(usernamelabel2);
		  
		  
		  textfield2.setBounds(200, 150, 200, 30);
		  add(textfield2);

		  button1.setBounds(225, 250, 60, 35);
		  add(button1);

		  button2.setBounds(300, 250, 60, 35);
		  add(button2); 
			 
		 button2.addActionListener(new ActionListener() {//退出按钮的监听事件
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});

	button1.addActionListener(new ActionListener() {//加密按钮的监听事件

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String str=textfield.getText();
			String result="";
			char str2[]=str.toCharArray();//将String类型通过函数转化为char类型
			JButton button=(JButton)e.getSource();
			String buttonName=e.getActionCommand();
			if(buttonName.equals("加密")) {
					for(char s:str2) {
						result+=(char)(s+2);	
						textfield2.setText(result);
						button.setText("解密");
					}
			}
			else {
				textfield2.setText(str);//原样输出之前输入的字符
						button.setText("加密");
			}
			
				
         }
		
	});
	
	}
}
		 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

实现效果:

点击加密之后的效果图
点击解密之后的效果图
这是本人自己做的简单的加密与解密小程序,大家可以相互交流一下,也希望大佬能不吝赐教。

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号