ログインバリデーターに取り組んでおり、ユーザー名とパスワードの有効性をチェックするクラスがあります。チェック後、ブール変数 (isValidLoginCredentials) が LoginProxy クラスで更新されます。これは、get メソッドによってフェッチされ、別の目的に使用できます。ただし、get メソッドによって返される値は常に、クラスの作成時に isValidLoginCredentials に割り当てたデフォルト値です。問題は、isValidLoginCredentials を更新する前に main() で getter メソッドを呼び出していることだと思いますが、これを停止するためにどのような変更を加える必要があるかわかりません。クラスとメインプログラムの関連部分を次に示します。
public class LoginProxy implements ActionListener
{
private JLabel usernameLabel;
private JTextField usernameText;
private JLabel passwordLabel;
private JPasswordField passwordText;
private JButton loginButton;
private boolean isValidLoginCredentials = false;
public void createLogin()
{
/*Here was code irrelevant to the problem I removed*/
loginButton.addActionListener(new LoginProxy());
loginButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String user = usernameText.getText();//get the username
String pass = passwordText.getText();//get the password
String credentials = user +":"+pass;//creates the string I compare to other valid
//credentials
ConcreteLoginValidator validator = new ConcreteLoginValidator(credentials);
try
{
isValidLoginCredentials = validator.checkLogin();
System.out.println("The credentials are "+isValidLoginCredentials);
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
}
});
}
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
public boolean getValidity()
{
return isValidLoginCredentials;
}
そして、これが主な方法です
public static void main(String[] args)
{
boolean isValidLogin = false;
LoginProxy proxy = new LoginProxy();
proxy.createLogin();
isValidLogin = proxy.getValidity();
if(isValidLogin == true)
{
JFrame frame = MainUI.getInstance();
frame.setSize(900, 600);
frame.pack();
frame.setVisible(true);
}
}
isValidLogin=proxy.getValidity(); になるように何を追加すればよいですか? すでに入力してログイン資格情報が正しいかどうかを確認した後にのみ値を返しますか?