无法将我的JtextField保存在arraylist中

时间:2013-11-26 14:47:11

标签: java swing arraylist

我尝试在Java中实现注册表单。我的主框架包含3个按钮:登录,注册,下订单,我有setEnabled(false)。如果用户想要登录(意味着他已经拥有一个帐户),并且他提供了正确的id和psw,我希望隐藏登录框架,并且我的主框架显示按钮这次启用了Place order。这是我的第一个问题。我的第二个问题是注册时我无法将用户的输入保存在ArrayList<User>

MainFrame.java:

public class MainFrame extends JFrame {

private JButton loginButton;
private JButton signUpButton;
 JButton placeOrderButton;

private boolean q;

private UserManager userManager;

public MainFrame(){
    super("Main Panel");
    userManager = new UserManager();
    loginButton = new JButton("Login");
    loginButton.addActionListener(new LoginButtonListener());
    placeOrderButton = new JButton("Place Order");
    placeOrderButton.addActionListener(new PlaceOrderButtonListener());
    placeOrderButton.setEnabled(false);



    signUpButton = new JButton("Sign Up");
    signUpButton.addActionListener(new SignUpButtonListener());



    this.getContentPane().add(loginButton, BorderLayout.LINE_START);
    this.getContentPane().add(signUpButton, BorderLayout.CENTER);
    this.getContentPane().add(placeOrderButton, BorderLayout.LINE_END);
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);






}

class LoginButtonListener implements ActionListener{

    @Override

    public void actionPerformed(ActionEvent arg0) {
         new LoginFrame(userManager);

    }
}

class SignUpButtonListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent arg0) {
        new SignUpFrame(userManager);

    }
}


class PlaceOrderButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent argO){



    }


}

User.java:

public class User {

private String fullName;
private String email;
private String userID;
private String password;

public User(String fullName, String email, String userID, String password) {
    this.fullName = fullName;
    this.email = email;
    this.userID = userID;
    this.password = password;
}

public String getFullName() {
    return fullName;
}
public void setFullName(String fullName) {
    this.fullName = fullName;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getUserID() {
    return userID;
}
public void setUserID(String userID) {
    this.userID = userID;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

LoginFrame.java:

public class LoginFrame extends JFrame{

    private JButton loginButton;
    private JTextField usernameField;
    private JPasswordField password;

    private UserManager userManager;

    public LoginFrame(UserManager userManager){
        super("Enter your credentials to login");
        this.userManager = userManager;
        loginButton = new JButton("Login");
        loginButton.addActionListener(new LoginButtonListener());

        usernameField = new JTextField(15);
        password = new JPasswordField(15);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(3,2));

        mainPanel.add(new JLabel("Username:"));
        mainPanel.add(usernameField);
        mainPanel.add(new JLabel("Password:"));
        mainPanel.add(password);
        mainPanel.add(loginButton);

        this.setContentPane(mainPanel);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }           
        class LoginButtonListener implements ActionListener{


public void actionPerformed(ActionEvent arg0) {
    String givenID = usernameField.getText();



    if(userManager.isUserInList(givenID)){
        String givenPass = password.getText();
        User theUser = userManager.getUserWithID(givenID);
        if(givenPass.equals(theUser.getPassword())){
            JOptionPane.showMessageDialog(null, "Access granted!");

        dispose();

        new MainFrame();
        }


        else{
            JOptionPane.showMessageDialog(null, 
                            "Wrong Password", 
                            "Error!",
                            JOptionPane.ERROR_MESSAGE);
        }
    }
    else{
        JOptionPane.showMessageDialog(null, 
                "Wrong username", 
                "Error!",
                JOptionPane.ERROR_MESSAGE);
    }
}

}

SignUpFrame.java:

public class SignUpFrame extends JFrame{

private JButton signupButton;
private JTextField name;
private JTextField email;
private JTextField username;
private JPasswordField pass;

private UserManager userManager;

public SignUpFrame (UserManager userManager){
super("Please fill in your Data");

userManager = new UserManager();

signupButton = new JButton("Sign Up!");
signupButton.addActionListener(new signupButtonListener());

JTextField name = new JTextField(15);

JTextField email = new JTextField(15);

JTextField username = new JTextField(15);

JTextField pass = new JTextField(15);



JPanel mainPanel = new JPanel();

mainPanel.setLayout(new GridLayout(5,2));

mainPanel.add(new JLabel("Name:"));
mainPanel.add(name);
mainPanel.add(new JLabel("Email:"));
mainPanel.add(email);
mainPanel.add(new JLabel("Username"));
mainPanel.add(username);
mainPanel.add(new JLabel("Password:"));
mainPanel.add(pass);
mainPanel.add(signupButton);

this.setContentPane(mainPanel);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);


class signupButtonListener implements ActionListener{

public void actionPerformed(ActionEvent arg0) {



    User u2 = new User(name.getText(),email.getText(),username.getText(),pass.getText());



    add(u2);

}
}

import java.util.ArrayList;

公共类UserManager {

private ArrayList<User> userList;

public UserManager(){
    userList = new ArrayList<User>();
    User u1 = new User("Teo Haik", "teo@uom.gr","teohaik","teopass");
    userList.add(u1);
}

public boolean isUserInList(String ID){
    for(User u: userList){
        if(u.getUserID().equals(ID)){
            return true;
        }
    }
    return false;
}

public User getUserWithID(String ID){
    for(User u: userList){
        if(u.getUserID().equals(ID)){
            return u;
        }
    }
    return null;
}

2 个答案:

答案 0 :(得分:0)

从上面发布的代码中,您不是要将新用户添加到UserManager。 signupButtonListener中的行add(u2);尝试将用户添加到框架,而不是UserManager。实际上,将User添加到ArrayList的唯一代码是UserManager构造函数中的硬编码。

您需要向UserManager添加一个方法,该方法将User作为参数,并将该User添加到ArrayList(如果它们尚未存在)(您可能还需要一个删除给定User的方法,如果它们已经在ArrayList中也是如此)。然后,在用户注册时,调用该方法以添加用户。

答案 1 :(得分:0)


1)对于placeOrder按钮:


A)将其更改为静态;从loginFrame类访问它。

      static JButton placeOrderButton;

B)修改loginButtonListener类中的actionPerformed方法,如下所示:

      public void actionPerformed(ActionEvent arg0) {

        String givenID = usernameField.getText();
        if(userManager.isUserInList(givenID)){
            String givenPass = password.getText();
            User theUser = userManager.getUserWithID(givenID);
            if(givenPass.equals(theUser.getPassword())){
                JOptionPane.showMessageDialog(null, "Access granted!");
                dispose();
                MainFrame.placeOrderButton.setEnabled(true); //her we enable editing
                //  new MainFrame(); no need for that 
            }else{
            JOptionPane.showMessageDialog(null,  "Wrong Password",  "Error!", JOptionPane.ERROR_MESSAGE);
            }
        }else{
        JOptionPane.showMessageDialog(null, "Wrong username","Error!",JOptionPane.ERROR_MESSAGE);
        }
    }

2)将用户添加到userList(数组列表):


A)将其更改为静态;从SignUpFrame类访问它。

      static ArrayList<User> userList;

B)按如下方式修改SignUpFrame类:

      public class SignUpFrame extends JFrame{


           private JButton signupButton;
           private JTextField name;
           private JTextField email;
           private JTextField username;
           private JPasswordField pass;

           //private UserManager userManager; no need for user manager 

           public SignUpFrame (){
                super("Please fill in your Data");

                //userManager = new UserManager(); no need for user manager 

                signupButton = new JButton("Sign Up!");
                signupButton.addActionListener(new signupButtonListener());


                // You were declaring them again !
                 name = new JTextField(15);

                 email = new JTextField(15);

                 username = new JTextField(15);

                 pass = new JPasswordField(15);



                JPanel mainPanel = new JPanel();

                mainPanel.setLayout(new GridLayout(5,2));

                mainPanel.add(new JLabel("Name:"));
                mainPanel.add(name);
                mainPanel.add(new JLabel("Email:"));
                mainPanel.add(email);
                mainPanel.add(new JLabel("Username"));
                mainPanel.add(username);
                mainPanel.add(new JLabel("Password:"));
                mainPanel.add(pass);
                mainPanel.add(signupButton);

                this.setContentPane(mainPanel);
                this.pack();
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setVisible(true);

         }
     class signupButtonListener implements ActionListener{
         public void actionPerformed(ActionEvent arg0) {
           try{ 
              // Organizing for better readability 
              String newName = name.getText();
              String newEmail = email.getText();
              String newUserName = username.getText();
              String newPass = pass.getText();

              User u2 = new User(newName,newEmail,newUserName,newPass);

              UserManager.userList.add(u2);


          }catch(NullPointerException e){
          System.out.println("fill all fields");
           }
               dispose(); //to close SignUpFrame
   }
}