有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用数组为密码和ID创建GUI java

应用程序将询问用户ID和密码,以及他们是现有用户还是新用户。如果您是新用户,系统将提示您重新输入详细信息以确认id和密码,然后这些信息将存储在阵列中。 一旦用户单击登录按钮,应用程序将通过ID和密码数组进行搜索以进行验证。(将您的数组设置为最多包含10个用户–假设它是一个小数组,则可以使用线性搜索) 然后,应用程序将显示相应的消息

到目前为止我的代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Log extends JFrame
{
    public static void main(String[] args)
    {
        Log frameTabel = new Log();
    }


        JLabel title = new JLabel("Please type your ID and Password");
        JLabel id = new JLabel("ID:");
        JLabel psword = new JLabel("Password:");
        JButton blogin = new JButton("Login");
        JPanel panel = new JPanel();
        JTextField txuser = new JTextField(15);
        JPasswordField pass = new JPasswordField(15);

        JRadioButton radNew = new JRadioButton("New", true);
        JRadioButton radExisting = new JRadioButton("Existing", false);

        ButtonGroup radioGroup1 = new ButtonGroup();

        Log()
        {
            super("Week 9 Question 3");
            setSize(300,250);
            setLocation(600,250);
            panel.setLayout (null);

            title.setBounds(40,5,300,20);
            id.setBounds(52,40,150,20);
            txuser.setBounds(70,40,150,20);
            psword.setBounds(7,80,150,20);
            pass.setBounds(70,80,150,20);
            blogin.setBounds(180,130,80,20);

            radNew.setBounds(30, 130, 50, 20);
            radExisting.setBounds(90, 130, 90, 20);

            panel.add(title);
            panel.add(id);
            panel.add(psword);
            panel.add(blogin);
            panel.add(txuser);
            panel.add(pass);
            add(radNew);
            add(radExisting);
            radioGroup1.add(radNew);
            radioGroup1.add(radExisting);

            map.put("test", "12345");


            getContentPane().add(panel);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            actionlogin();
        }

        public void actionlogin() {
            blogin.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    String puname = txuser.getText();
                    String ppaswd = new String(pass.getPassword());

                        JOptionPane.showMessageDialog(null, "Correct Information", "Correct",
                                JOptionPane.INFORMATION_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "No Password/ID Found on System",
                                "Incorrect", JOptionPane.INFORMATION_MESSAGE);
                        txuser.setText("");
                        pass.setText("");
                        txuser.requestFocus();

                        if (radNew.isSelected()) {

                            }

                            JOptionPane.showMessageDialog(null,
                                    "Re-enter your password and ID to confirm", "Information Saved",
                                    JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
}

            });
        }
}

共 (1) 个答案

  1. # 1 楼答案

    使用

     char[] ppaswd = pass.getPassword();
    

    而不是

    String ppaswd = pass.getText();
    

    因为

    The method getText() from the type JPasswordField is deprecated.

    正如@MadProgrammer所说:

    The password is stored in a char array and you never convert it back to a String.

    Strings live in the JVM for the life of JVM, making easier for nasty people examine the String pool and find the passwords.


    使用Map存储用户名/密码

     Map<String, char[]> map = new HashMap<String, char[]>();
    

    输入一些初始值

        map.put("test", "12345".toCharArray());
        map.put("admin", "admin".toCharArray());
    

    这是修改后的代码

    public void actionlogin() {
        blogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                String puname = txuser.getText();
                char[] ppaswd = pass.getPassword();
                if (map.keySet().contains(puname) && Arrays.equals(map.get(puname), ppaswd)) {
                    JOptionPane.showMessageDialog(null, "Correct Information", "Correct",
                            JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "No Password/ID Found on System",
                            "Incorrect", JOptionPane.INFORMATION_MESSAGE);
                    txuser.setText("");
                    pass.setText("");
                    txuser.requestFocus();
    
                    if (radNew.isSelected()) {
                        if (!puname.equals("") && ppaswd.length > 0) {
                            map.put(puname, ppaswd);
                        }
    
                        JOptionPane.showMessageDialog(null,
                                "Re-enter your password and ID to confirm", "Information Saved",
                                JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }
        });
    }