如何防止我的ArrayList在Java GUI中重置?

时间:2013-03-22 05:38:13

标签: java swing login arraylist store

我有一个具有用户登录功能的程序,该功能会提示创建用户名和密码,该用户名和密码稍后存储在两个不同的ArrayLists中。我在帐户创建后添加了一个直接登录的按钮,登录工作正常。但是,如果我关闭GUI并尝试使用以前的登录信息直接登录,则无法识别用户名/密码。我的ArrayLists是否重置,或者我的代码中是否有错误?

以下是我的登录类的缩短版本:

public class OptionGUI extends JPanel  implements ActionListener
{
   ArrayList<String>passworddatabase= new ArrayList();
   ArrayList<String> usernamedatabase= new ArrayList();
   JButton newaccount = new JButton("Create a new account");
   JButton login= new JButton("Login");
   public int response;
  public OptionGUI()
  {

    newaccount.setVerticalTextPosition (AbstractButton.CENTER);
    newaccount.setHorizontalTextPosition(AbstractButton.LEADING);
    newaccount.setActionCommand("create");

    login.setVerticalTextPosition(AbstractButton.CENTER);
    login.setHorizontalTextPosition(AbstractButton.TRAILING);
    login.setActionCommand("login");
    login.setEnabled(true);

    newaccount.addActionListener(this);
    login.addActionListener(this);

    add(newaccount);
    add(login);
   }
  public void actionPerformed(ActionEvent e)
  {
   {
       if ("create".equals(e.getActionCommand()))
      {

        newaccount.setEnabled(true);
        login.setEnabled(false);

        JPanel logininfo= new JPanel();
        JLabel usernameLbl= new JLabel("Choose a username:");
        JLabel passwordLbl= new JLabel("Choose a password:");
        JTextField username= new JTextField();
        JPasswordField password= new JPasswordField();

        logininfo.add(usernameLbl);
        logininfo.add(username);
        logininfo.add(passwordLbl);
        logininfo.add(password);


        logininfo.setVisible(true);

        int input2 = JOptionPane.showConfirmDialog(frame,logininfo, "Create your account:"
                            ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

        char[] enteredPassword= password.getPassword();
        char[] enteredPassword2= passwordconfirm.getPassword();
        String newusername= username.getText();
        String newpassword= String.valueOf(enteredPassword);
        String newpassword2= String.valueOf(enteredPassword2);

        String[] options = { "Login", "Cancel" };
        response = JOptionPane.showOptionDialog(null, "Your account has been successfully created! Please login to continue.", "Confirmation",JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options,options[0]);                    

            }
        passworddatabase.add(newpassword);
        usernamedatabase.add(newusername);

        }
       if ("loginbutton".equals(e.getActionCommand())|| response==0)
       {  
        login.setEnabled(true);
        newaccount.setEnabled(false);
        JPanel panel= new JPanel();
        panel.setLayout(new GridLayout(2,2));
        JLabel usernameLbl = new JLabel("Username:");
        JLabel passwordLbl = new JLabel("Password:");
        JTextField username = new JTextField();
        JPasswordField passwordFld = new JPasswordField();

        panel.add(usernameLbl);
        panel.add(username);
        panel.add(passwordLbl);
        panel.add(passwordFld);

        panel.setVisible(true);

        int input = JOptionPane.showConfirmDialog(frame, panel, "Enter your password:"
                            ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

        String inputPassword= String.valueOf(passwordFld);
        String inputUsername= username.getText();

        for(int i=0; i < passworddatabase.size(); i++) 
        {
         if (passworddatabase.get(i).contains(inputPassword))
        {
           JOptionPane.showMessageDialog(null, "Welcome to AIR Ticket Reservation!");
         }

        else
         {   JOptionPane.showMessageDialog(null, "The username or password you entered does not exist.", "Error",
                                    JOptionPane.ERROR_MESSAGE);
                }
            }                
        }
    }
}
    }

这是我的司机:

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JDialog;
import java.awt.GridLayout;
import java.util.Arrays;
import java.awt.EventQueue;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.AbstractButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class Driver 
{
   public static void main(String[] args)
    { 

        JFrame option= new JFrame ("Ticket Reservation");
        option.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        option.setSize(500,300);
        option.setLocationRelativeTo(null);
        OptionGUI newContentPane= new OptionGUI();
        option.setContentPane(newContentPane);

        option.setVisible(true);
    }             
}

谢谢!

1 个答案:

答案 0 :(得分:1)

OptionGUI newContentPane= new OptionGUI();创建一个新的OptionGUI实例。

在你的OptionGUI课程中,您提供了一小段代码: -

ArrayList<String> passworddatabase= new ArrayList();
ArrayList<String> usernamedatabase= new ArrayList();

因此,每次关闭并再次打开它时,它都会创建一个新实例OptionGUI,它将ArrayList初始化为new ArrayList()(空数组列表)。< / p>