[英]JFrame freezes up when I press a button
我正在制作一個加密程序,由於某種原因,當我按下按鈕時,程序完全停止運行。 我不確定出了什么問題,因為我以前做過很多簡單的 GUI,但我從來沒有遇到過這個問題。 這是按鈕的空白:
private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {
String origMessage = txtDInput.getText();
String encMessage = "";
String revMessage = "";
String extraStg1 = "";
String extraStg2 = "";
char tempChar;
char tempExtraChar;
int tempAscii;
int tempExtraAscii;
for (int i = origMessage.length() - 1; i >= 0; i = i--) //reverses message
{
revMessage = revMessage + origMessage.charAt(i);
}
for (int i = 0; i < revMessage.length(); i = i++)
{
tempChar = revMessage.charAt(i); //stores character in the tempChar variable
tempAscii = (int)tempChar; //converts the character into an Ascii value
tempAscii = tempAscii + 3; //adds 3 to Ascii value
tempChar = (char)tempAscii; //converts Ascii value back into a character value
encMessage = encMessage + tempChar; //adds the new character to the encrypted string and repeats for every character
}
for (int i = 0; i <= 7; i++)
{
tempExtraAscii = (int)Math.round(Math.random()*25+1+96); //generates a random integer between 97 and 122
tempExtraChar = (char)tempExtraAscii; //convert the random integer into a character
extraStg1 = extraStg1 + tempExtraChar; //add the extra character to tempExtraStg1
}
for (int i = 0; i <= 7; i++)
{
tempExtraAscii = (int)Math.round(Math.random()*25+1+96); //generates a random integer between 97 and 122
tempExtraChar = (char)tempExtraAscii; //convert the random integer into a character
extraStg2 = extraStg2 + tempExtraChar; //add the extra character to tempExtraStg2
}
encMessage = extraStg1 + encMessage + extraStg2;
txtEncrypted.setText(encMessage);
}
我是這方面的初學者,所以如果答案盡可能簡單,我將不勝感激。 謝謝。
這就是問題:
for (int i = 0; i < revMessage.length(); i = i++)
i = i++
是一個空操作 - 它遞增i
,但隨后將其設置回原始值,因此您的循環將永遠執行。 只需將其更改為:
for (int i = 0; i < revMessage.length(); i++)
您實際上之前有同樣的問題:
for (int i = origMessage.length() - 1; i >= 0; i = i--)
應該
for (int i = origMessage.length() - 1; i >= 0; i--)
(作為旁注,這並不是真正有用的“加密”,無論如何你都不應該推出自己的加密,但我已經按照要求解決了這個問題。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.