1

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

2 回答 2

0

你的if测试不可能是正确的。如果你的任何一个String(s)是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);
}

编辑

根据您提供的代码,但您在 else 中需要它!

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();
    }
}
于 2014-11-13T22:41:36.517 回答
-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

}
于 2014-11-13T22:32:41.660 回答