JTextField
의 입력 검증에 대해 조사했지만 입력이 string
, integer
또는 digit
인지 여부 만 확인할 수 있습니다. 누구든지 그 숫자의 특정 가치를 어떻게 확인할 수 있는지 제안 할 수 있습니까? 예를 들어 범위가 0.5 - 1
인지 여부를 나타냅니다. 고맙습니다. 이 작업을 수행하는JTextField에 0.5-1의 값이 포함되어 있는지 확인.
답변
한 가지 방법은 이것이 사실이라면 모듈 1은 0과 같으면 그런 다음 확인할 수 있습니다이
float tfValue = Float.parseFloat(jTextField1.getText());
같은 float로 텍스트 필드의 당신의 가치를 구문 분석하는 것, 값은 정수입니다 .
if (tfValue % 1 == 0) {
System.out.println("is an integer");
}
else System.out.println("is a float");
'JTextField.getText()'에서 리턴 된'String'에 숫자 숫자 이외의 것이 있으면, 구문 분석은'NumberFormatException'을 리턴합니다. 이 방법에 접근하려면 구문 분석하기 전에 RegEx를 사용해야합니다. –
은 어쩌면에 대한 도움의이 시도 당신
JTextField field = new JTextField();
field.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
String text = ((JTextField)evt.getSource()).getText();
if (!(evt.getKeyChar() >= '0' && evt.getKeyChar() <= '9')) {
// it is number
}
else if (!(evt.getKeyChar() >= 'a' && evt.getKeyChar() <= 'Z')) {
// it is char a-Z
}
else {
// it others
}
}
});
하지만 숫자에 대한 가장 좋은 방법은는, 특정 마스크
일반적으로 텍스트 구성 요소에서 기본 키/마우스 이벤트를 듣지 않고 기본 문서에서들을 수 있습니다. –
@Abihabi 87 사용자가 0.5와 1 사이의 입력을 한 경우 일종의 유효성 검사가 필요하다. 그러면 그 이상이 안되는 경우 – Mapet
두 가지 방법 JFormattedTextField
을 사용할 수 있습니다 :
가의 내용을 구문 분석을 JTextField를 double 변수로 변환하고 간단한 if 문을 사용하십시오. try catch 블록을 사용하여 가능한 NumberFormatException을 처리해야합니다 (텍스트를 double로 변환 할 수없는 경우).
예 :
double value = 0;
try {
value = Double.parseDouble (yourTextField.getText());
}
catch (NumberFormatException e) {
// Handle exception ...
}
if (value >= 0.5 && value <= 1) {
// ...
}
else {
// ...
}
또한 텍스트가 주어진 (스마트) 정규 표현식과 일치하는지 여부를 확인할 수 있습니다.
다음 패턴을 고려하십시오 (점 (.0 *)? 0? \ [5-9] [0-9] *)
(점 및 다른 특수 문자를 이스케이프하려면 두 개의 백 슬래시가 필요합니다)이 패턴 (java.util.regex.Pattern 사용) (1) (또는 소수 구분과 0의 발생 개수이어서 1) 0.5 사이의 값 (이 정규식 허용 "0.5") 및 0.99999 ...
예를 수락 :
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "0.4")); // -> false
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "0..5")); // -> false
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "0.111")); // -> false
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "0.5")); // -> true
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "0.5000543")); // -> true
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "0.50+")); // -> false
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "1.0")); // -> true
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "1.0000")); // -> true
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "1")); // -> true
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", "21")); // -> false
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", ".56256")); // -> true
System.out.println (Pattern.matches ("(1(\\.0*)?|0?\\.[5-9][0-9]*)", ".46256")); // -> false
[JFormattedTextField에] 봐 (http://docs.oracle.com/javase/7/docs/api/javax/swing/JFormattedTex를 타고 tField.html),이 [site] (http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html)는 사용 방법을 설명하는 데 유용합니다. –
고마워, 이건 내가 도와 줄 것이다. – Mapet
'SpinnerModel model = new SpinnerNumberModel (.5, .5, 1, .01); JSpinner numberRange = new JSpinner (model); ' –