簡體   English   中英

為什么我不能登錄?

[英]Why can't I log in?

我試圖創建一個非常簡單的銀行系統。 到目前為止,我有3個班級,分別是GuiMainValidate

我的問題似乎是當我嘗試登錄時。我創建並存儲在Validate類中的用戶/密碼。 我輸入了錯誤的密碼或用戶名,為什么? 怎么了?

要獲取登錄彈出窗口(JOptionPane),必須單擊“登錄”按鈕。

這是所有三個類的代碼:

主要類別:

public class Main{

    public static void main(String[] args){

        Validate validate = new Validate();
        Gui gui = new Gui(validate);
        //create Accounts. Send accounts to gui+validate.
    }       

}

驗證類:

public class Validate {

Map<String,String> userMap;

public Validate(){

    userMap = new HashMap<String,String>();

    //Skapa ett inloggning för mig
    //accounts.createUser();
    userMap.put("john","smith");

}

public boolean validateUser(String username, String password){

    if(userMap.isEmpty() != true){
        if(userMap.containsKey(username)){
            String passTest = userMap.get(username);
            if(password.equals(passTest)){
                return true;
                //accounts.logInUser();
            }else{
                return false;
            }   
        }else{
            return false;
        }
    }else{
        return false;
    }

}


}

GUI類:

public class Gui extends JFrame implements ActionListener{

JPanel loggning, center, botten;
JButton loggaIn, loggaUt;
JTextField text1, text2;
JLabel lb1, lb2;
JMenuBar mbar;
JMenu about,system;
JMenuItem aboutBank, exit;
Font font;
Validate validate;

public Gui(Validate validate){

    super("BANK");
    this.validate = validate;

    //Skapa menun.
    mbar = new JMenuBar();
    system = new JMenu("System");
    mbar.add(system);
    about = new JMenu("About");
    mbar.add(about);
    aboutBank = new JMenuItem("Bank Info");
    about.add(aboutBank);
    aboutBank.addActionListener(this);
    exit = new JMenuItem("Exit");
    exit.addActionListener(this);
    system.add(exit);
    add(mbar, BorderLayout.NORTH);

    //skapa vänstra panelen. Här loggar man in/ut.
    loggning = new JPanel();
    loggning.setLayout(new GridLayout(20,1,0,5));
    text1 = new JTextField("");
    text2 = new JTextField("");
    lb1 = new JLabel("  ANGE ANVÄNDARNAMN:   ");
    lb2 = new JLabel("       ANGE LÖSENORD:  ");
    Border border = BorderFactory.createLineBorder(Color.gray, 1);
    lb1.setBorder(border);
    lb2.setBorder(border);
    //loggning.add(lb1);
    //loggning.add(text1);
    //loggning.add(lb2);
    //loggning.add(text2);
    loggaIn = new JButton("Logga in");
    loggaUt = new JButton("Logga ut");
    loggaUt.setEnabled(false);
    loggaIn.setPreferredSize(new Dimension(100,10));
    loggaUt.setPreferredSize(new Dimension(100,10));
    loggaIn.addActionListener(this);
    loggaUt.addActionListener(this);
    loggning.add(loggaIn);
    loggning.add(loggaUt);
    add(loggning, BorderLayout.WEST);

    //Skapa mittpanelen. Här finns information om dig och dina konton.
    center = new JPanel();

    add(center, BorderLayout.CENTER);

    //skapa botten panelen. Här kan man ändra i sina konton. Skicka pengar, Dra ut pengar, ändra info? osv.


    //JFrame fixeringar
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setSize(900,900);
}






@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == loggaIn){
        JTextField user = new JTextField();
        JTextField pass = new JTextField();
        Object[] message = {"Enter your username:", pass, "Enter your password:", user};  
        int option = JOptionPane.showConfirmDialog(this, message, "Enter all your values", JOptionPane.OK_CANCEL_OPTION);  
        if (option == JOptionPane.OK_OPTION)  
        {  
            String pass1 = pass.getText();
            String user1 = user.getText();
            Boolean valid = validate.validateUser(user1,pass1);
            if(valid == true){
                loggaIn.setEnabled(false);
                loggaUt.setEnabled(true);
                showLogInInfo(user1);
                //accounts.getLoggedInInfo();
                //gui.showLoggedInInfo();
                //Logged in info showed in center jpanel.
            }else{
                JOptionPane.showMessageDialog(this,"Username or password wrong", "Error", JOptionPane.INFORMATION_MESSAGE);
            }
        }

    }else if(e.getSource() == loggaUt){
        loggaIn.setEnabled(true);
        loggaUt.setEnabled(false);
        //accounts.gotOutUser();
        //text1.setEditable(true);  
        //text2.setEditable(true);
    }else if(e.getSource() == exit){
        this.dispose();
    }else if(e.getSource() == aboutBank){
        String mssg = "Ali Bank.\nSince 2014.\nIf you have questions,\nthere is no customer service.\nSorry we cant help you.";
        JOptionPane.showMessageDialog(this, mssg, "Bank Information",JOptionPane.INFORMATION_MESSAGE);
    }else{}

}

public void showLogInInfo(String user){
    String messs = ("User " + user + " is now logged in.");
    JOptionPane.showMessageDialog(this, messs, "Log In Info", JOptionPane.INFORMATION_MESSAGE);
}

}

抱歉,有些評論可能是瑞典語。 通常,我認為它們無關緊要,但是如果您有任何疑問,請詢問。

您在彈出框中交換了用戶名和密碼字段:

Object[] message = {"Enter your username:", pass, "Enter your password:", user};

應該是:

Object[] message = {"Enter your username:", user, "Enter your password:", pass};

解決此問題后,我就能夠成功登錄。

您傳遞反向密碼和用戶名。 看到變化

 Boolean valid = validate.validateUser(user1,pass1);

 Boolean valid = validate.validateUser(pass1,user1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM