0 java regex validation swing lostfocus
我正在使用 LostFocus 事件验证两个文本字段,如下所示:
textRegNo.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
regNo1=textRegNo.getText();
Pattern pattern1 = Pattern.compile("^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$");
Matcher matcher1 = pattern1.matcher(regNo1);
if (!matcher1.find()){
JOptionPane.showMessageDialog(null, "Invalid Vehicle No!!!\n Vehicle no should be of the form MH 03 KS 2131!!");
}
}
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
};
});
textMobNo.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent arg0) {
// TODO Auto-generated method stub
mobNo1=textMobNo.getText();
Pattern pattern2 = Pattern.compile("^[789]\\d{9}$");
Matcher matcher2 = pattern2.matcher(mobNo1);
System.out.println("'"+mobNo1+"'");
if (!matcher2.find()){
JOptionPane.showMessageDialog(null, "Phone no must be a 10 digit number!!");
}
}
@Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
};
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我失去第一个文本字段的焦点并将焦点移至第二个文本字段时,两个字段都会打印错误消息(两个丢失焦点事件下的 IF 块内的消息。)当我向第一个文本字段输入错误的输入并移动焦点时对于第二个字段,它应该只打印第一个文本字段的错误消息。但两者的打印错误。
第一个文本字段是 textRegNo 第二个文本字段是 textMobNo
问题的症状就是围绕这个过程展开的。
textMobNo
获得焦点...textRegNo
失去焦点...textRegNo
已验证并发现无效,textRegNo
现在显示错误消息...textMobNo
失去焦点(因为对话框获得焦点)如果可以的话,您应该避免使用焦点作为验证字段的方法,而是使用InputVerifier
简单的例子
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.JTextComponent;
public class TestInputVerifier {
public static void main(String[] args) {
new TestInputVerifier();
}
public TestInputVerifier() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
JTextField field = new JTextField(20);
field.setInputVerifier(new RegExpInputVerifier("^[A-Z]{2}[ -][0-9]{1,2}(?: [A-Z])?(?: [A-Z]*)? [0-9]{4}$"));
add(field, gbc);
field = new JTextField(20);
field.setInputVerifier(new RegExpInputVerifier("^[789]\\d{9}$"));
add(field, gbc);
}
}
public class RegExpInputVerifier extends InputVerifier {
private String expression;
public RegExpInputVerifier(String expression) {
this.expression = expression;
}
public String getExpression() {
return expression;
}
@Override
public boolean verify(JComponent input) {
boolean verified = false;
if (input instanceof JTextComponent) {
JTextComponent field = (JTextComponent) input;
String regNo1 = field.getText();
Pattern pattern1 = Pattern.compile(expression);
Matcher matcher1 = pattern1.matcher(regNo1);
}
return verified;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8355 次 |
最近记录: |