作成したユーザークラスを使用してログインシステムを作成し、複数のユーザーをArrayListに格納しようとしています。次に、ユーザーがさまざまなフィールドに入力する内容が特定の「ユーザー」と同じであることを確認するために、2つを比較するループが下部にあります。下部のメソッドはtrueまたはfalseを返します。私がいる場所はかなり遅いので、可能な限り明確な方法で説明していない場合は、お詫び申し上げます。
私の問題は、loginAuthorization()メソッドがtrueを返すのは、ArrayListの最初の要素が入力され、正しく入力された場合のみです。ArrayListに追加された他のすべてのユーザーを無視します。この質問には明らかに「まあまあ」の答えがあるので、私は推測しますが、なぜこれが起こっているのかわかりません。誰かが私が間違っていることを私に見せてくれるなら、それは大いにありがたいです。:)
私のloginWindowクラス:
/**
* @author OperatorX© 2013
*/
package dossierIB;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
public class LoginWindow extends JFrame
{
private JPanel contentPane;
private JFrame errorFrame;
private JTextField usernameField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
LoginWindow frame = new LoginWindow();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginWindow() {
setResizable(false);
setTitle("Employee Authorization");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[][][][grow][]", "[][][][][][][]"));
JPanel bgImagePanel = new JPanel();
ImagePanel backgroundPanel = new ImagePanel(new ImageIcon("C:/Users/OpX/Documents/workspace/Dossier (WBP)/images/testtoptrans.png").getImage());
bgImagePanel.add(backgroundPanel);
contentPane.add(bgImagePanel, "cell 0 0 5 1");
Component rigidArea_4 = Box.createRigidArea(new Dimension(20, 20));
contentPane.add(rigidArea_4, "cell 4 1 1 6");
JLabel lblUsername = new JLabel("Username");
contentPane.add(lblUsername, "cell 1 2");
Component rigidArea_1 = Box.createRigidArea(new Dimension(20, 20));
contentPane.add(rigidArea_1, "cell 0 1 1 6");
Component rigidArea_3 = Box.createRigidArea(new Dimension(20, 20));
contentPane.add(rigidArea_3, "cell 2 2 1 3");
usernameField = new JTextField();
contentPane.add(usernameField, "cell 3 2,growx");
usernameField.setColumns(10);
Component rigidArea_2 = Box.createRigidArea(new Dimension(20, 20));
contentPane.add(rigidArea_2, "cell 1 3");
JLabel lblPassword = new JLabel("Password");
contentPane.add(lblPassword, "cell 1 4");
passwordField = new JPasswordField();
contentPane.add(passwordField, "cell 3 4,growx");
Component rigidArea_5 = Box.createRigidArea(new Dimension(20, 20));
contentPane.add(rigidArea_5, "cell 1 5 3 1");
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(loginAuthorization() == true)
{
ManageWindow manifest = new ManageWindow();
manifest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
manifest.setSize(697,500);
manifest.setLocationRelativeTo(null);
manifest.setVisible(true);
setVisible(false);
dispose();
}
else
return;
}
});
contentPane.add(btnLogin, "flowx,cell 3 6,alignx right");
JButton btnCreateAccount = new JButton("Create Account");
btnCreateAccount.setEnabled(false);
btnCreateAccount.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
contentPane.add(btnCreateAccount, "cell 3 6");
}
public Boolean loginAuthorization()
{
errorFrame = new JFrame();
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
ArrayList<User> users = new ArrayList<User>();
User manager = new User("manager", "gocompany");
User shiftManager = new User("shiftmanager", "goshifts");
User stockBoy = new User("stockboy", "istockshelves");
User cashier = new User("cashier", "trustedwithmoney");
users.add(manager);
users.add(shiftManager);
users.add(stockBoy);
users.add(cashier);
for(int i = 0; i < users.size(); i++)
{
if(users.get(i).getUsername().compareTo(username) == 0 && users.get(i).getPassword().compareTo(password) == 0)
{
return true;
}
else
{
passwordField.setText("");
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(errorFrame,
"Could not authorize employee with given username and password.\n\n" +
"Please reenter or contact your system administrator.\n\n",
"Employee Authorization Failed",
JOptionPane.ERROR_MESSAGE);
return false;
}
}
return false;
}
}
私のユーザークラス:
/**
* @author OperatorX© 2013
*/
package dossierIB;
public class User
{
private String user;
private String pass;
public User(String u, String p)
{
user = u;
pass = p;
}
public String getUsername()
{
return user;
}
public String getPassword()
{
return pass;
}
public void setUsername(String username)
{
this.user = username;
}
public void setPassword(String password)
{
this.pass = password;
}
}