사용자 이름과 암호를 묻는 코드가 있습니다. 그냥 간단한 JOptionPane. 프로그램은 입력을 요청해야하며 사용자가 사용자 이름에 아무런 입력도 쓰지 않으면 오류 메시지를 표시하고 사용자 이름을 묻는 동일한 이전 대화 상자를 표시해야합니다. 그러나, 그것은 그렇게 작동하지 않습니다. 아무 것도 입력하지 않더라도 여전히 암호 대화 상자로 진행됩니다. 제 논리를 확인해주세요. 조금 혼란 스러웠을 수도 있습니다. 또한 showInputDialog
의 입력이 문자열인지 확인하는 방법이 있습니까? 정수의 경우 NumberFormatException
과 다소 비슷합니까? catch 메소드의 예외는 작동하지 않습니다. :) 미리 감사드립니다.JOptionPane. 프로그램에 오류 메시지가 표시되지 않습니다.
public class SwingExercise {
public static void main(String[] args) {
String name = null;
boolean input = true;
try {
while (input) {
while (name == null) {
name = JOptionPane.showInputDialog(null, "Enter username:");
if (name == null) {
JOptionPane.showMessageDialog(null, "No input.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
String pw = JOptionPane.showInputDialog(null, "Enter password:");
input = false;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Invalid input.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
대부분 환영합니다 .--) –
'JOptionPane.showInputDialog (null, "암호 입력 :");'['JPasswordField'] 사용 (http://docs.oracle.com/javase/7 /docs/api/javax/swing/JPasswordField.html)를 사용하여 비밀번호를 얻으십시오. 자세한 내용은 [암호 필드 사용 방법] (http://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html)을 참조하십시오. –
@Layne : 귀하의 코드를 수정했는지 Andrew Thompson이 말했던 것, 매우 진실한 요점을 보았습니다 :-) –