在java中,强制用户不要在jTextField中输入数字条目
如何为用户锁定键盘,以便用户无法在在java中,强制用户不要在jTextField中输入数字条目,java,Java,如何为用户锁定键盘,以便用户无法在JTextField中输入任何数值?您可以用于此目的,也可以用于此目的 适用于大多数简单的任务 这是我前几天弄掉的一个: public class TexFieldValidator extends InputVerifier { String regex; String errorMsg; JDialog popup; public TexFieldValidator(String regex, String errorMsg) {
JTextField
中输入任何数值?您可以用于此目的,也可以用于此目的
适用于大多数简单的任务
这是我前几天弄掉的一个:
public class TexFieldValidator extends InputVerifier {
String regex;
String errorMsg;
JDialog popup;
public TexFieldValidator(String regex, String errorMsg) {
this.regex = regex;
this.errorMsg = errorMsg;
}
@Override
public boolean verify(JComponent input) {
boolean verified = false;
String text = ((JTextField) input).getText();
if (text.matches(regex)) {
input.setBackground(Color.WHITE);
if (popup != null) {
popup.dispose();
popup = null;
}
verified = true;
} else {
if (popup == null) {
popup = new JDialog((Window) input.getTopLevelAncestor());
input.setBackground(Color.PINK);
popup.setSize(0, 0);
popup.setLocationRelativeTo(input);
Point point = popup.getLocation();
Dimension dim = input.getSize();
popup.setLocation(point.x - (int) dim.getWidth() / 2, point.y + (int) dim.getHeight() / 2);
popup.getContentPane().add(new JLabel(errorMsg));
popup.setUndecorated(true);
popup.setFocusableWindowState(false);
popup.getContentPane().setBackground(Color.PINK);
popup.pack();
}
popup.setVisible(true);
}
return verified;
}
}
从……偷来的
使用示例:
iDTextField.setInputVerifier(new TexFieldValidator("[a-zA-Z0-9]{3}", "ID must be 3 alphanumerics."));
适用于大多数简单的任务
这是我前几天弄掉的一个:
public class TexFieldValidator extends InputVerifier {
String regex;
String errorMsg;
JDialog popup;
public TexFieldValidator(String regex, String errorMsg) {
this.regex = regex;
this.errorMsg = errorMsg;
}
@Override
public boolean verify(JComponent input) {
boolean verified = false;
String text = ((JTextField) input).getText();
if (text.matches(regex)) {
input.setBackground(Color.WHITE);
if (popup != null) {
popup.dispose();
popup = null;
}
verified = true;
} else {
if (popup == null) {
popup = new JDialog((Window) input.getTopLevelAncestor());
input.setBackground(Color.PINK);
popup.setSize(0, 0);
popup.setLocationRelativeTo(input);
Point point = popup.getLocation();
Dimension dim = input.getSize();
popup.setLocation(point.x - (int) dim.getWidth() / 2, point.y + (int) dim.getHeight() / 2);
popup.getContentPane().add(new JLabel(errorMsg));
popup.setUndecorated(true);
popup.setFocusableWindowState(false);
popup.getContentPane().setBackground(Color.PINK);
popup.pack();
}
popup.setVisible(true);
}
return verified;
}
}
从……偷来的
使用示例:
iDTextField.setInputVerifier(new TexFieldValidator("[a-zA-Z0-9]{3}", "ID must be 3 alphanumerics."));
请参见此处:有关文档过滤器的示例请参见此处:有关文档过滤器的示例,是否希望用户获取错误对话框通知错误输入?是否希望用户获取错误对话框通知错误输入?