2017-06-01 21 views
-2

步驟1.選擇虛擬鍵。 (蘋果)
第2步。從虛擬密鑰中刪除重複項。 (aple)Java swing中的單字母加密/解密

第3步。其餘字母連接在一起,以便從最後一個字母表沒有 重複。此時,總長度爲 26個字符。如果'z'已滿,則從'a'開始。

結果:aplefghijkmnoqrstuvwxyzbcd

固定到步驟2,我想在文本字段中使用.getText()。
我不知道如何從第3步開始。
我該怎麼辦?

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowListener; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.LinkedHashSet; 
import java.util.Set; 

import javax.swing.*; 
import javax.swing.border.LineBorder; 
import javax.swing.border.TitledBorder; 

import com.jgoodies.forms.factories.DefaultComponentFactory; 

public class monoalphabetic_Cipher extends JFrame implements ActionListener { 

    private JPanel p1, p2, p3;  // default, staging job, job selection 
    private JLabel title, l1, l2, l3, l4; // title, virtual key input, de-duplicated virtual key, encryption, decryption 
    private JButton b1, b2, b3;   // Encryption, decryption, deduplication 
    private JTextField t1, t2, t3, t4; 
    int i,j; 

    public monoalphabetic_Cipher() { 

     super("monoalphabetical cipher"); 

     p1 = new JPanel(); 
     p1.setForeground(new Color(0, 0, 0)); 
     p1.setBackground(UIManager.getColor("Button.background")); 

     title = new JLabel("<html><h1>monoalphabetical cipher</h1><hr></html>"); 
     title.setFont(new Font("맑은 고딕", Font.PLAIN, 12)); 
     title.setBounds(204, 20, 175, 57); 
     p1.add(title); 

     b2 = new JButton("encryption"); 
     b2.setFont(new Font("맑은 고딕", Font.PLAIN, 12)); 
     b2.setBackground(UIManager.getColor("Button.background")); 
     b2.setBounds(67, 210, 119, 23); 
     p1.add(b2); 

     b3 = new JButton("decryption"); 
     b3.setFont(new Font("맑은 고딕", Font.PLAIN, 12)); 
     b3.setBackground(UIManager.getColor("Button.background")); 
     b3.setBounds(67, 252, 119, 23); 
     p1.add(b3); 

     t3 = new JTextField(20); 
     t3.setBounds(260, 212, 297, 21); 
     p1.add(t3); 

     t4 = new JTextField(20); 
     t4.setBounds(260, 254, 297, 21); 
     p1.add(t4); 

     p1.setLayout(null); 

     t2 = new JTextField(10); 
     t2.setBounds(395, 123, 128, 21); 
     p1.add(t2); 

     b1 = new JButton("overlap remove"); 
     b1.setFont(new Font("맑은 고딕", Font.PLAIN, 12)); 
     b1.setBounds(264, 122, 119, 23);   
     p1.add(b1); 

     l1 = new JLabel("virtual key"); 
     l1.setFont(new Font("맑은 고딕", Font.PLAIN, 12)); 
     l1.setBounds(55, 126, 57, 15); 
     p1.add(l1); 

     t1 = new JTextField(10); 
     t1.setBounds(124, 123, 128, 21); 
     p1.add(t1); 



     p3 = new JPanel(); 
     p3.setBorder(new TitledBorder(new LineBorder(Color.lightGray), " selection", TitledBorder.LEADING, TitledBorder.TOP, new Font("맑은 고딕", Font.PLAIN, 12), new Color(0, 0, 0))); 
     p3.setBounds(34, 176, 184, 122); 
     p1.add(p3); 

     l2 = new JLabel("\u2460"); 
     l2.setFont(new Font("맑은 고딕", Font.PLAIN, 16)); 
     l2.setBounds(230, 211, 28, 23); 
     p1.add(l2); 

     l3 = new JLabel("\u2461 "); 
     l3.setFont(new Font("맑은 고딕", Font.PLAIN, 16)); 
     l3.setBounds(230, 252, 28, 23); 
     p1.add(l3); 


     p2 = new JPanel(); 
     p2.setBorder(new TitledBorder(new LineBorder(Color.lightGray), "ready", TitledBorder.LEADING, TitledBorder.TOP, new Font("맑은 고딕", Font.PLAIN, 12), new Color(0, 0, 0))); 
     p2.setBounds(34, 97, 523, 69); 
     p1.add(p2); 
     getContentPane().add(p1); 
     setSize(600,355); 
     setResizable(false);         
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     setVisible(true); 


     // duplication remove 
     b1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       int vk_1 = t1.getText().length(); 

       Set chk = new LinkedHashSet(); 

       for(i=0;i<vk_1;i++) { 
        // Convert all lowercase letters to uppercase and lowercase letters 
        chk.add(t1.getText().toLowerCase().charAt(i)); 
       } 
       /// Remove brackets and commas in the [result, result] format, which is the default output method of LinkedHashSet() 
       String result = Arrays.toString(chk.toArray()).replace("[", "").replace(",", "").replace("]", ""); 

       /* With the usual replaceAll ("", "") or trim() 
        * If spaces are not removed, use regular expressions 
        * /tText(result.replaceAll("\\p{Z}", "")); 

       if(t1.getText().equals("")) { 
        t2.setText("no input key"); 
       } 
      } 
     }); 

     // encryption 
     b2.addActionListener(new ActionListener() { 

      final String curString = t2.getText(); 

      final char startChar = curString.charAt(curString.length()-1); 

      public void actionPerformed(ActionEvent e) { 

       StringBuilder sb = new StringBuilder(); 
       sb.append(curString); 

       char c = nextChar(startChar); 

       while (sb.length() < 26) { 

        while (hasChar(sb, c)) { 
         c = nextChar(c); 
        }     
        sb.append(c); 


        c = nextChar(c); 
       } 

       System.out.printf("Final String %28s has length of %3d", sb.toString(), sb.length()); 


      } 
      private char nextChar(int idx) { 
       ++idx; 
        if (idx > 'z') { 
         idx = 'a'; 
        } 

        return (char)idx; 
      } 
      private boolean hasChar(StringBuilder sb, char c) 
      { 
       for (int i = 0; i < sb.length(); ++i) { 
        if (sb.charAt(i) == c) { 
         return true; 
        } 
       } 

       return false; 
      } 
     }); 

     // Decrypt 
     b3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       if(t2.getText().equals("")) { 
        t4.setText("no input key"); 
       } 
      } 
     }); 
    } 

    public static void main(String[] args) {  
     new monoalphabetic_Cipher(); 
    } 
} 
+0

長度考慮到字符 'A' 是65,和一個可以用'%26'到一個人的優勢。 – KevinO

+2

請不要破壞你的帖子。一旦你發佈了一個問題,你已經將內容授權給了Stack Overflow社區(在CC-by-SA許可下)。如果您想取消關聯此帳戶與您的帳戶關聯,請參閱[解除請求的正確途徑是什麼?](http://meta.stackoverflow.com/questions/323395/what-is-the-proper-route-換一個 - 解離 - 請求)。 – FelixSFD

+0

@FelixSFD如果我取消鏈接我的帖子和帳戶,我可以刪除該問題嗎?我不想接觸任何打算參加這個班的人(針對這個問題)。 – mminaa

回答

2

隨着該假設:輸入字符串爲dedupped,這是一個適當的長度的,字符添加是「a」到「Z」以下,不字符應重複,最終字符串應長度爲26個字符,以下提供了一個快速解決方案。需要額外的錯誤檢查和更好的變量命名。

public static void main(String[] args) 
{ 
    // the example string 
    final String curString = "aple"; 

    // get the starting character, which is the end of the string 
    // in the example, will be 'e' 
    final char startChar = curString.charAt(curString.length() - 1); 

    // construct our compilation; this could also be List<Character> 
    // which would make somethings easier, but would have to assemble 
    // at the end 
    StringBuilder sb = new StringBuilder(); 
    sb.append(curString); 

    // we want the character after the one on the end of the string to start 
    char c = nextChar(startChar);   

    // we want 26 total entries 
    while (sb.length() < 26) { 

     // we cannot append a character that already exists, so spin 
     // until we find one not in the compilation 
     while (hasChar(sb, c)) { 
      c = nextChar(c); 
     } 

     // we now have the one we want, so append it 
     sb.append(c); 

     // we know we need at least the next character after the one we 
     // just appended, so get it and loop; loop will terminate if 
     // we've added enough 
     c = nextChar(c); 
    } 


    // display the result 
    System.out.printf("Final String %28s has length of %3d", sb.toString(), 
      sb.length()); 
} 


private static boolean hasChar(StringBuilder sb, char c) 
{ 
    for (int i = 0; i < sb.length(); ++i) { 
     if (sb.charAt(i) == c) { 
      return true; 
     } 
    } 

    return false; 
} 

private static char nextChar(int idx) 
{ 
    ++idx; 
    if (idx > 'z') { 
     idx = 'a'; 
    } 

    return (char)idx; 
} 

輸出:

最終的字符串aplefghijkmnoqrstuvwxyzbcd具有26

+0

感謝您的好解答。但是,會發生以下異常處理錯誤。 線程「main」中的異常java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:-1 – mminaa

+0

Final String curString =「aple」; 我想用textfield.getText()而不是上面的語句。 – mminaa

+0

如假設所述,它確定它不處理'a'和'z'之間的字符串中的任何ASCII字符。 – KevinO