我正在尝试制作一个程序,用户需要一个帐户才能访问它的其他部分。我希望这样设置,如果用户的 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();
}
}
}