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

How to retrieve string value call from existing specific JButton variable in java?

    private void jButton_1ActionPerformed(java.awt.event.ActionEvent evt) {                                                

    String text = "";

    texto = RandomStringUtils.randomAlphanumeric(12);
    System.out.println(text);
    JOptionPane.showMessageDialog(null, text);

    }

我需要检索变量 String text

    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.");

               }
        }
    }

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

非常感谢

您可以使用 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.");

      }
    }
}

我通过在 public class.

中创建变量解决了这个问题
private String code = ""; 

所以我可以得到生成的代码并保存它以供以后比较,虽然我害怕任何人都可以看到生成的代码。因为我会用它来加密以下信息。