GUI简单实现凯撒加密法

   凯撒加密法:消息中每个字母换成向后三个字母的字母,例如,明文ATUL变成密文DWXO。

   MainKaisa.java
 1 import javax.swing.JFrame;
 2 
 3 public class MainKaisa {
 4 
 5     /**
 6      * @param nonles
 7      */
 8     public static void main(String[] args) {
 9         //实例化一个窗体
10         KaisaFrame kaisaFrame = new KaisaFrame();
11         kaisaFrame.setVisible(true);
12         kaisaFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13 
14     }
15 
16 }
  
    Kaisa.java
 1 mport java.awt.event.ActionEvent;
 2 import java.awt.event.ActionListener;
 3 import javax.swing.*;
 4 
 5 public class KaisaFrame extends JFrame {
 6     
 7     JLabel jlDackText = new JLabel();
 8     JButton btnBrightText = new JButton();
 9     JPasswordField jpf = new JPasswordField();
10     JTextField jtf2 = new JTextField();
11     char[] buf;
12     
13     
14     public KaisaFrame() {
15         this.setSize(300,200);    //设置窗体大小
16         this.setTitle("凯撒加密法");
17         this.setResizable(false);
18         
19         jbInit();
20     }
21     
22     private void isLetter() {
23         //判断输入的内容是否为字母
24         buf = jpf.getPassword();
25         for(char c:buf) {
26             if(Character.isLetter(c) == false) {
27                 JOptionPane.showMessageDialog(this"不能为非字符""Error", JOptionPane.ERROR_MESSAGE);
28                 jpf.setText("");
29                 jtf2.setText("");
30                 return;
31             } else {
32                 makeBrightText();
33             }
34         }
35     }
36     
37     private void makeBrightText() {
38         // 产生明文    
39         char[] arr = new char[buf.length];
40         int index=0,temp;
41         for(char c:buf) {
42             temp = c+3;    //字母后移三位
43             if( (temp>90 && temp<97|| temp>122 ) {    
44                 //若ASCII码在此区间则减去26(使字符XYZ,xyz循环到XAB,xab)
45                 temp = temp - 26;
46                 arr[index++= (char)temp;    
47             } else {
48                 arr[index++= (char)temp;
49             }
50         } 
51         
52         String str = new String(arr);            
53         jtf2.setText(str);
54         
55     }
56     
57     private void jbInit() {
58         //    设置窗体内容        
59         this.setLayout(null);
60         jlDackText.setText("输入密文:"); 
61         jlDackText.setBounds(30208030);
62         btnBrightText.setText("生成明文:");
63         btnBrightText.setBounds(309010030);
64         jpf.setBounds(160208030);
65         jpf.setEchoChar('*');
66         jtf2.setBounds(160908030);
67         jtf2.setEditable(false);
68     
69         this.add(jlDackText);
70         this.add(jpf);
71         this.add(btnBrightText);
72         this.add(jtf2);
73         
74         btnBrightText.addActionListener(new ActionListener(){
75 
76             @Override
77             public void actionPerformed(ActionEvent e) {
78                 isLetter();    //判断输入的内容是否为字母        
79             }
80 
81         });
82     }
83     
84 }
85 
    
    现实图解:   

      往文本框中输入密文(只限字符),点击按钮,即生成相应密文。                           
       
                                  
      若输入为非字符,则弹出错误框~
     

     简单的一个程序,说明都不用了。。。

posted on 2008-09-04 15:51 nonels 阅读(826) 评论(1)  编辑  收藏 所属分类: J2SE

评论

# re: GUI简单实现凯撒加密法 [未登录] 2009-12-18 15:13 nick

可以請教你一些有關於這格程式的問題嗎??
拜託 加我MSN >\\\< ~~~b51ak47@hotmail.com  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(2)

随笔分类(16)

随笔档案(16)

搜索

最新评论

阅读排行榜

评论排行榜