2013-11-14 37 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会显示一条错误消息。