2013-11-14 40 views
-4

我剛剛創建這個具體的,但我記錄這一點有點困惑。我只是停留在解釋什麼最後的幾行做:Java - 輸入驗證器

class MyVerifier extends InputVerifier { 

public boolean verify(JComponent input) { 

    if (input==id) { 
    return validId(); 

} 

else if (input==name) { 
    return validName(); 

} 

return false; 
} 

    public boolean validId() { 
     boolean status; 
     String theID = id.getText(); 
     Pattern pattern = Pattern.compile("\\d{8}"); 
     Matcher matcher = pattern.matcher(theID); 
     if (matcher.matches()) { 
      status = true; 
     } 
     else { 
      status = false; 
     } 
     return status; 
    } 
    public boolean validName() { 
     boolean status; 
     String theName = name.getText(); 
     Pattern pattern = Pattern.compile("[A-za-z0-9 ]+"); 
     Matcher matcher = pattern.matcher(theName); 
     if (matcher.matches()) { 
      status = true; 
     } 
     else { 
      status = false; 
     } 
     return status; 
    } 
} 

你能解釋這些特定行這裏逐一?

/** 
* @param o the object corresponding to the user's selection 
*/ 
@Override 
public void tell(Object o) { -- Where has this come from ? 
    deptCode.setText(o.toString()); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == submit) { 
     MyVerifier test = new MyVerifier(); 

     if (Staff.getStaff(id.getText()) == null && test.verify(id) && 
       test.verify(name)) { 
      System.out.println("YAY");-- What is this doing 
     } 
     else if (!(Staff.getStaff(id.getText()) == null)) { 
      String errorMessage = "ID EXISTS: " + Staff.getStaff(id.getText()).toString(); -- What is this doing 

      JOptionPane.showMessageDialog(theFrame, errorMessage, "Error", 
       JOptionPane.WARNING_MESSAGE);-- What is this doing 
     } 
     else { 
      System.out.println("Woops."); 
     } 
    } 

    else if (e.getSource() == clear) { 
     id.setText(null); 
     deptCode.setText(null); 
     name.setText(null); 
    } 
} 

public static void main(String[] args) { 
    Registration test = new Registration(); 
} 
} 
+2

如果你自己寫的,你怎麼不知道他們是什麼意思?你爲什麼寫他們? –

+2

@MaurícioLinharesAliens – Rogue

+1

'System.out.println(「YAY」);';我們是否真的需要解釋這是做什麼? –

回答

0

現在你明白你想用這個程序來完成,從一張白紙開始(使用你的第一次嘗試作爲必要時的例子)的東西。重新開始通常比修復程序更容易。

0

看來你的public void tell(Object o)方法是通過傳遞的對象的值設置一個字符串。因爲您沒有向我們展示您使用它的方式,所以我們無法確定地知道。在另一方面,你的其他問題是相當清楚的:

System.out.println("YAY");

看來,Staff.getStaff(id.getText)正在檢查String或姓名和ID的列表的文本文件。此聲明僅在以前沒有使用提供的idname創建職員時打印「YAY」。但是既然你也沒有向我們展示那些變量的位置,這只是我最好的猜測。

JOptionPane.showMessageDialog(theFrame, errorMessage, "Error", JOptionPane.WARNING_MESSAGE);

這顯示JOptionPane警告信息,如果已經有一個工作人員給定idname。顯然,你不能創建別人擁有的帳戶,所以如果確實如此,這個JOptionPane會顯示一條錯誤消息。