如何从验证访问 JFrame 输入字段对象 Class

How to access JFrame Input field Objects from Validation Class

我创建了一个带有注册字段(名字、姓氏、电子邮件等)的 GUI,我的 GUI class 是 -

public class Registration extends JFrame {

    public static void main(String[] args) {
        try {
            Registration regform = new Registration ();
        } catch (Throwable e) {
            //
        }

      public Registration () {
            firstName = new JTextField();
            latName = new JTextField();
            btnRegistration = newJButton("Register");

            btnSubmit.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent arg0) {
                  //want to do something similar as below
                  //Validatefields Vf = new Validatefields(Registration Rg);
             }
            });

       }
}

现在我想创建一个单独的 class 来验证单击“注册”按钮时的所有字段。我是 Java 编码新手,我无法从验证 Class.

访问所有注册对象 class 的输入字段

请注意Class注册和验证字段在同一个包下。

public Class Validatefields{
    public static void Validattion(Registration Rg){
      //here I want access the text field as below
      //String Name = "Object of Registration Class".firstName.getText();

        
        int validateFlag = 0;
        if(Name.equal("")){
          validateFlag = 1;
        }
        if(validateFlag==0){
            ApiCall APC = new ApiCall();
            APC.RequestAccessToken();
        }
   }
}

在接下来的步骤中,我想使用另一个 class 在成功验证时调用 API - 因此在这种方法中我需要获取所有输入字段值。

public Class ApiCall {
   public static void RequestAccessToken(){
      //Similarly I want to get the individual field value here and pass it in the API
   }
}

我试图从互联网的不同来源阅读类似的例子,但无法弄明白。任何帮助都会很棒。

Now I want to create a separate class to Validate all the fields on clicking on "Register" button.

这是个好主意,您有通用的方法。我认为主要问题是您的语法不太正确。但在此之前,我的建议是将 字段 传递给构造函数而不是整个 Registration 对象:

public static void Validattion(JTextField firstName, JTextField lastName){

现在您通过将字段传递给构造函数来创建一个实例:

Validatefields Vf = new Validatefields(firstName, lastName);

请注意,您在这里只使用变量名,而不是类型。这很可能是您在尝试中遇到并导致错误的主要问题。