如何从java中现有的特定JButton变量中检索字符串值调用?


private void jButton_1ActionPerformed(java.awt.event.ActionEvent evt) {                                                
String text = "";
texto = RandomStringUtils.randomAlphanumeric(12);
System.out.println(text);
JOptionPane.showMessageDialog(null, text);
}

我需要检索变量Stringtext

private void jButton_2ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
String code, text = "";
text = *??????*;   /*<-----This variable I need to retrieve the previous generated button*/
code = txt_code.getText().trim();
if (code.equals("")) {
jLabel_codeerror.setText("This information is required.");
} else {
if (code.equals(text)) {
jLabel_codeerror.setText("Equal code.");
} else {
jLabel_codeerror.setText("The confirmation has failed, please try again.");
}
}
}

我不知道如何从其他按钮中提取变量,我需要,因为每个按钮都有不同的变量,后来我必须在最后一个按钮中收集它们以更新它

Thank you so much

我通过在我的公共类中创建变量来解决这个问题。

private String code = ""; 

所以我可以获得生成的代码并保存它以供以后比较,尽管我担心任何人都可以看到生成的代码。因为我要用它来加密下面的信息。

您可以使用getter/setter来检索您的值。

private String yourText;
public void setYourText(String yourText) {
this.yourText = yourText;
}
public String getYourText() {
return this.yourText ;
}
private void jButton_1ActionPerformed(java.awt.event.ActionEvent evt) {                                                

String text = "";
texto = RandomStringUtils.randomAlphanumeric(12);
System.out.println(text);
JOptionPane.showMessageDialog(null, text);
setYourText(text);
}
private void jButton_2ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
String code, text = "";
text = getYourText();
code = txt_code.getText().trim();
if (code.equals("")) {
jLabel_codeerror.setText("This information is required.");
} else {
if (code.equals(text)) {
jLabel_codeerror.setText("Equal code.");
} else {
jLabel_codeerror.setText("The confirmation has failed, please try again.");
}
}
}

最新更新