2017-08-01 74 views
-2

如何通過hashmap傳遞「toEncrypt」文本並將其顯示在「encrypted」標籤上?這一直困擾着我。我只能得到用戶想要加密的內容,而不需要加密。如何通過HashMap發送文本?

@FXML 
Button encrypt; 
@FXML 
Label encrypted; 
@FXML 
TextField toEncrypt; 

@FXML 
private void encryptButton(ActionEvent event) { 
encrypted.setText(toEncrypt.getText()); 
} 

@FXML 
private void secretMessage (ActionEvent event) { 

    HashMap<String,String> secret = new HashMap<String, String>(); 


    //*****UPPER CASE LETTERS***** 
    secret.put("A","9"); 
    secret.put("B","J"); 
    secret.put("C","Q"); 
    secret.put("D","1"); 
    secret.put("E","T"); 
    secret.put("F","B"); 
    secret.put("G","Z"); 
    secret.put("H","R"); 
    secret.put("I","t"); 
    secret.put("J","m"); 
    secret.put("K","q"); 
    secret.put("L","p"); 
    secret.put("M","o"); 
    secret.put("N","3"); 
    secret.put("O","v"); 
    secret.put("P","y"); 
    secret.put("Q","X"); 
    secret.put("R","z"); 
    secret.put("S","l"); 
    secret.put("T","S"); 
    secret.put("U","5"); 
    secret.put("V","w"); 
    secret.put("W","C"); 
    secret.put("X","K"); 
    secret.put("Y","8"); 
    secret.put("Z","n"); 
    //********************** 


@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
}  
+0

'通過HashMap發送文本'沒有意義,請澄清你的問題。 – EJP

回答

1

很高興看到你已經嘗試過;所以我不僅僅提供了一個完整的工作答案,我只是想指出一些應該讓你去做的概念。

對於「通過HashMap」步驟中,您可以在String遍歷char就像這樣:

final String toEncryptString = toEncrypt.getText(); 
final char[] encrypted = new char[toEncryptString.length()]; 
for (int i = 0; i < toEncryptString.length(); i++) { 
    final char c = toEncryptString.charAt(i); 
    final char e; 
    // TODO - Set 'e' from getting the value from the HashMap 
    encrypted[i] = e; 
} 
return new String(encrypted); 

我會假設你設置這個地方,但你不在您共享的代碼中設置encryptButton的邏輯。 ButtonsetOnAction,你應該能夠與像這裏使用:

encrypt.setOnAction(this::encryptButton); 

你只需要找個地方設置此將總是運行您嘗試使用Button之前。

最後,我沒有提及將HashMap構建爲方法的一部分似乎很奇怪(每次嘗試加密某些東西時都必須這樣做),這將是我的疏忽。有了這一套作爲字段(或類似 - static場,隱藏在實用程序類,通過static char getEncrypted(final char given) { return map.get(given); }訪問)將是更高性能的(儘管確保你在你去任何溶液處理潛在null小號

希望。那會讓你開始吧