简体   繁体   English

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

[英]How to access JFrame Input field Objects from Validation Class

I have created a GUI with the registration field (firstname, lastname, email etc.) and my GUI class is -我创建了一个带有注册字段(名字、姓氏、email 等)的 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);
             }
            });

       }
}

Now I want to create a separate class to Validate all the fields on clicking on "Register" button.现在我想创建一个单独的 class 来验证单击“注册”按钮时的所有字段。 I am new with Java coding and I am not able to access all the input fields Object of Registration class from Validation Class.我是 Java 编码的新手,我无法从验证 Class 访问注册 class 的所有输入字段 Object。

please note Class Registration and Validatefields are under same package.请注意 Class Registration 和 Validatefields 在同一个 package 下。

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();
        }
   }
}

in the next steps I want to use another class to call API on successfull validation - so in this method I would be require to get all input fields value.在接下来的步骤中,我想使用另一个 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
   }
}

I tried to read similar example from different source from inte.net but not able to figure it out.我试图从 inte.net 的不同来源读取类似的示例,但无法弄清楚。 any help would be really great.任何帮助都会很棒。

Now I want to create a separate class to Validate all the fields on clicking on "Register" button.现在我想创建一个单独的 class 来验证单击“注册”按钮时的所有字段。

This is a very good idea and you have the general approach.这是一个很好的主意,您有通用的方法。 I think the main problem is that you don't quite have the correct syntax.我认为主要问题是您的语法不太正确。 But before we get to that, my suggestion is that you pass the fields to the constructor instead of the entire Registration object:但在此之前,我的建议是将字段传递给构造函数而不是整个Registration object:

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

Now you create an instance by passing the fields to the constructor:现在您通过将字段传递给构造函数来创建一个实例:

Validatefields Vf = new Validatefields(firstName, lastName);

Note that you only use the variable names here, not the types.请注意,您在这里只使用变量名,而不是类型。 This is most likely the main problem that you encountered and caused an error in your attempt.这很可能是您在尝试中遇到并导致错误的主要问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM