GUI簡(jiǎn)單實(shí)現(xiàn)凱撒加密法

             凱撒加密法:消息中每個(gè)字母換成向后三個(gè)字母的字母,例如,明文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         //實(shí)例化一個(gè)窗體
          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);    //設(shè)置窗體大小
          16         this.setTitle("凱撒加密法");
          17         this.setResizable(false);
          18         
          19         jbInit();
          20     }
          21     
          22     private void isLetter() {
          23         //判斷輸入的內(nèi)容是否為字母
          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         // 產(chǎn)生明文    
          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碼在此區(qū)間則減去26(使字符XYZ,xyz循環(huán)到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         //    設(shè)置窗體內(nèi)容        
          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();    //判斷輸入的內(nèi)容是否為字母        
          79             }
          80 
          81         });
          82     }
          83     
          84 }
          85 
              
              現(xiàn)實(shí)圖解:   

                往文本框中輸入密文(只限字符),點(diǎn)擊按鈕,即生成相應(yīng)密文。                           
                 
                                            
                若輸入為非字符,則彈出錯(cuò)誤框~
               

               簡(jiǎn)單的一個(gè)程序,說明都不用了。。。

          posted on 2008-09-04 15:51 nonels 閱讀(872) 評(píng)論(1)  編輯  收藏 所屬分類: J2SE

          評(píng)論

          # re: GUI簡(jiǎn)單實(shí)現(xiàn)凱撒加密法 [未登錄] 2009-12-18 15:13 nick

          可以請(qǐng)教你一些有關(guān)於這格程式的問題嗎??
          拜託 加我MSN >\\\< ~~~b51ak47@hotmail.com  回復(fù)  更多評(píng)論   

          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆分類(16)

          隨筆檔案(16)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 沙洋县| 明水县| 西城区| 西平县| 玉山县| 定结县| 肥西县| 大石桥市| 西乌珠穆沁旗| 东海县| 汕尾市| 邵东县| 芦山县| 云林县| 弥勒县| 昌平区| 波密县| 丹棱县| 慈利县| 汤原县| 通辽市| 柞水县| 陆川县| 平江县| 龙泉市| 扎囊县| 万州区| 乌拉特前旗| 玛多县| 黎川县| 新源县| 嘉禾县| 屯门区| 宝清县| 广水市| 丘北县| 房产| 三明市| 内乡县| 临邑县| 泸水县|