簡體   English   中英

如何從驗證中訪問 JFrame 輸入字段對象 Class

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

我創建了一個帶有注冊字段(名字、姓氏、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);
             }
            });

       }
}

現在我想創建一個單獨的 class 來驗證單擊“注冊”按鈕時的所有字段。 我是 Java 編碼的新手,我無法從驗證 Class 訪問注冊 class 的所有輸入字段 Object。

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

在接下來的步驟中,我想使用另一個 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
   }
}

我試圖從 inte.net 的不同來源讀取類似的示例,但無法弄清楚。 任何幫助都會很棒。

現在我想創建一個單獨的 class 來驗證單擊“注冊”按鈕時的所有字段。

這是一個很好的主意,您有通用的方法。 我認為主要問題是您的語法不太正確。 但在此之前,我的建議是將字段傳遞給構造函數而不是整個Registration object:

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

現在您通過將字段傳遞給構造函數來創建一個實例:

Validatefields Vf = new Validatefields(firstName, lastName);

請注意,您在這里只使用變量名,而不是類型。 這很可能是您在嘗試中遇到並導致錯誤的主要問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM