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

huangapple go评论74阅读模式
英文:

How to access JFrame Input field Objects from Validation 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();
        lastName = new JTextField();
        btnRegistration = new JButton("Register");

        btnRegistration.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Validatefields.Validattion(Registration.this);
            }
        });

        // Other GUI setup code...
    }
}

public class Validatefields {
    public static void Validattion(Registration Rg) {
        String firstName = Rg.firstName.getText();
        String lastName = Rg.lastName.getText();
        
        int validateFlag = 0;
        if (firstName.equals("") || lastName.equals("")) {
            validateFlag = 1;
        }
        if (validateFlag == 0) {
            ApiCall APC = new ApiCall();
            APC.RequestAccessToken(firstName, lastName);
        }
    }
}

public class ApiCall {
    public void RequestAccessToken(String firstName, String lastName) {
        // Use firstName and lastName to make API call...
    }
}

Please note that I've made a few corrections in your code to ensure proper functionality. Make sure you've defined the variables firstName, lastName, and btnRegistration as fields in the Registration class. Additionally, there were some typos in your code, such as latName should be corrected to lastName, and newJButton should be corrected to new JButton.

英文:

I have created a GUI with the registration field (firstname, lastname, email etc.) and my GUI class is -

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. I am new with Java coding and I am not able to access all the input fields Object of Registration class from Validation Class.

please note Class Registration and Validatefields are under same 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.

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 internet but not able to figure it out. any help would be really great.

答案1

得分: 3

现在我想要创建一个单独的类,在点击“Register”按钮时验证所有字段。

这是一个非常好的想法,你已经有了一般的方法。我认为主要问题是你的语法不太正确。但在我们深入讨论之前,我的建议是你将字段传递给构造函数,而不是整个Registration对象:

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

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

Validatefields Vf = new Validatefields(firstName, lastName);

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

英文:

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

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:

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.

huangapple
  • 本文由 发表于 2020年8月31日 21:54:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63672140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定