我试图创建一个程序,用户需要一个帐户才能访问其他部分。我想要这样设置,以便如果用户2确认密码不匹配,他们必须重新输入信息。此外,如果用户留下任何空白,他们必须重新输入所有信息。我该怎么做才能继续循环,直到用户输入正确的信息?
if (e.getSource() == okButton) {
if(!passString.equals(passStringConfirm) || userName.equals(null) || passString.equals(null) || passStringConfirm.equals(null)){
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
}
}
这是我到目前为止所做的,如果只适用于一次迭代。我试着这样做,它会不断打印出我试图在JOptionPane中打印的警告信息。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class CreateAccount extends JFrame implements ActionListener {
JLabel username = new JLabel("Enter your username");
JTextField enterUsername = new JTextField(null, 15);
JLabel password = new JLabel("Enter your password");
JPasswordField enterPassword = new JPasswordField(null, 15);
JLabel passwordConfirm = new JLabel("Confirm your password.");
JPasswordField enterConfirmPassword = new JPasswordField(null, 15);
JButton okButton = new JButton("OK");
String userName;
double initialDeposit;
public CreateAccount() {
add(username);
add(enterUsername);
add(password);
add(enterPassword);
add(passwordConfirm);
add(enterConfirmPassword);
add(okButton);
okButton.addActionListener(this);
setTitle("New Bank Account Creation");
setVisible(true);
setLocationRelativeTo(null);
setSize(270, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
@Override
public void actionPerformed(ActionEvent e) {
char[] pass = enterPassword.getPassword();
String passString = new String(pass);
char[] passConfirm = enterConfirmPassword.getPassword();
String passStringConfirm = new String(passConfirm);
userName = enterUsername.getText();
if (e.getSource() == okButton) {
if(userName == null || userName.isEmpty() || passString == null || passString.isEmpty() || !passString.equals(passStringConfirm)) {
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
Border redLine = BorderFactory.createLineBorder(Color.red);
enterUsername.setBorder(redLine);
enterPassword.setBorder(redLine);
enterConfirmPassword.setBorder(redLine);
repaint();
}
}
super.dispose();
int response = 0;
String firstDesposit = JOptionPane.showInputDialog("Welcome " + userName + ". Enter your initial deposit.");
initialDeposit = Double.parseDouble(firstDesposit);
if (response == JOptionPane.OK_OPTION) {
new Menu();
}
}
}
答案 0 :(得分:0)
您的if
测试无法正确。如果您的任何String
({1}} null
,您将获得NullPointerException
。我想你想要
if (userName == null || userName.isEmpty() || passString == null
|| passString.isEmpty()
|| !passString.equals(passStringConfirm)) {
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
}
然后您的UI代码应该在允许用户继续之前检查它们是否为空。最后,在上面的代码中,我相信您可以使用setBorder()
为这些字段添加红色边框。
if (userName == null || userName.isEmpty() || passString == null
|| passString.isEmpty()
|| !passString.equals(passStringConfirm)) {
Border redLine = BorderFactory.createLineBorder(Color.red);
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
enterUsername.setBorder(redLine);
enterPassword.setBorder(redLine);
enterConfirmPassword.setBorder(redLine);
}
修改强>
根据您提供的代码,但您需要在其他地方使用它!
if(userName == null || userName.isEmpty() || passString == null
|| passString.isEmpty() || !passString.equals(passStringConfirm)) {
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
Border redLine = BorderFactory.createLineBorder(Color.red);
enterUsername.setBorder(redLine);
enterPassword.setBorder(redLine);
enterConfirmPassword.setBorder(redLine);
repaint();
} else { // <-- add this
super.dispose();
int response = 0;
String firstDesposit = JOptionPane.showInputDialog(
"Welcome " + userName + ". Enter your initial deposit.");
initialDeposit = Double.parseDouble(firstDesposit);
if (response == JOptionPane.OK_OPTION) {
new Menu();
}
}
答案 1 :(得分:-1)
假设e.getSource()是一个阻塞调用。
while(true){
if (e.getSource() == okButton) {
if(!passString.equals(passStringConfirm) || userName.equals(null) || passString.equals(null) || passStringConfirm.equals(null)){
enterUsername.setText("");
enterPassword.setText("");
enterConfirmPassword.setText("");
}
}
else
break; //correct password
}