I have looked at a couple of examples that would return values, but I don't fully understand how to make them work in my own code. I figured I should post it here and see if I can get some good pointers. I need to do a check to see if the user has logged in, then if they have logged in open the window to allow them to create the event(I can make the logic work with the if, then, and else statements). The part I need help with is passing the variables from the login class to the CreateEventActionPerformed . The following is from my mainFrame Jframe class:
我已经看了几个会返回值的示例,但我并不完全理解如何使它们在我自己的代码中工作。我想我应该在这里发布,看看我能不能得到一些好的指示。我需要检查用户是否已登录,然后如果他们已登录打开窗口以允许他们创建事件(我可以使用if,then和else语句使逻辑工作)。我需要帮助的部分是将变量从登录类传递给CreateEventActionPerformed。以下是我的mainFrame Jframe类:
private void CreateEventActionPerformed(java.awt.event.ActionEvent evt) {
//This is where I want to check for the login variable to allow or deny the user access
//if the have logged in then new CreateEvent().setVisible(true);
//if the user has not yet logged in, then open the the login window.
// note the only piece that I need help with is getting the varible from the CdEventPlannerLogin() jframe.
// the varrible will be from the submit button also put as SubmitActionPerformed(java.awt.event.ActionEvent evt) I think
new CdEventPlannerLogin().setVisible(true);
// Once the user has logged in then new CreateEvent().setVisible(true);
//I also need to maintain the username (variable is un) from the CdEventPlannerLogin()
//I need it for the next page.
//new CreateEvent().setVisible(true);
// TODO add your handling code here:
}
The Following is from The login jframe class:
以下是来自登录jframe类:
private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {
String un = UserName.getText().trim();
String pw = Password.getText().trim();
HashSet hs= new HashSet();
HashSet users = new HashSet();
boolean goahead=true;
try {
Scanner Scan = new Scanner(new File("Login.txt"));
while (Scan.hasNextLine())
{
String authenticator = Scan.nextLine().trim();
String[] autparts=authenticator.split(" ");
String user = autparts[0];
if (goahead)
{
if (users.contains(user))
{
if (user.equals(un))
//this checks for a duplicate user when loging in, and denies access if a duplicate is found
{
JOptionPane.showMessageDialog(null, "Duplicate user found. Access Denied");
goahead=false;
dispose();
}
} else {
hs.add(authenticator);
users.add(user);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (goahead) {
//if the user has checked the box it will prevent the user from creating an account if one already exsist
if (createAccount.isSelected() & (hs.contains(new String(un+" "+pw))))
{
JOptionPane.showMessageDialog(null,"Account Already Exsist! No Need to create a new account. Please try again.");
dispose();
} else { if (hs.contains(new String(un+" "+pw)))
{
JOptionPane.showMessageDialog(null,"User, Found Access Granted!");
//this is where I need a varriable or boolean of some kind granting access to other portion of the program.
//I need this value to be accessed by other classes to grant that access.
dispose();
} else {
if (createAccount.isSelected())
//if the user has selected the create account box
//it will allow him to make one base on the values entered
{
try {
PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("Login.txt", true)));
output.close();
} catch (IOException ex) {
System.out.printf("error %s/n", ex );
}
//below sends a message to the user that their account has been created.
JOptionPane.showMessageDialog(null,"Welcome!"+" " + un+" "+"Your account is now created. You may now login");
dispose();
}else {
JOptionPane.showMessageDialog(null, "user doesn't exist or password incorrect. ");
dispose();
}
}
}
}
}
I haven't posted all of my code I hope this is enough to help me figure this out. I have thought about getter and or setter methods, but I am not sure how to make that work either.
我没有发布我的所有代码,我希望这足以帮助我解决这个问题。我已经考虑过getter和/或setter方法,但我也不确定如何做到这一点。
1 个解决方案
#1
1
You are correct in the assumption that you have to use getters/setters. So, first off add a getter method and a boolean at the top of your login JFrame class.
假设您必须使用getter / setter,这是正确的。因此,首先在登录JFrame类的顶部添加一个getter方法和一个boolean。
public class mainFrame {
....
boolean isLoggedIn = false; // By default their not logged in
// Getter method to get value of the boolean
public boolean getIsLoggedIn() {
return isLoggedIn;
}
... Rest of code not shown
}
Then simply set the isLoggedIn
variable to true
where you want it to be:
然后只需将isLoggedIn变量设置为true即可:
private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {
... Other code not shown
//this is where I need a varriable or boolean of some kind granting access
// to other portion of program
// I need this value to be accessed by other classes to grant that access.
isLoggedIn = true;
dispose();
....
}
Finally, in the CreateEventActionPerformed
method you need to create or reuse the current instance of the JFrame login class and use that to invoke the getIsLoggedIn()
method.
最后,在CreateEventActionPerformed方法中,您需要创建或重用JFrame登录类的当前实例,并使用它来调用getIsLoggedIn()方法。
For example, if you wanted to create a new instance of the login class simply add this bit to your CreateEventActionPerformed
method, where LoginClass
is the name of your login class.
例如,如果要创建登录类的新实例,只需将此位添加到CreateEventActionPerformed方法,其中LoginClass是登录类的名称。
LoginClass login = new LoginClass();
boolean isLoggedIn = login.getIsLoggedIn();
You can then use the boolean to perform whatever checks or activities you like. This same process (of creating a getter method) can also be used to pass the un
variable across classes.
然后,您可以使用布尔值执行您喜欢的任何检查或活动。同样的过程(创建getter方法)也可用于跨类传递un变量。
Please note that if you want to save the variables into memory (so they can be preserved across application shutdowns) you will have to look into storing persistent data.
请注意,如果要将变量保存到内存中(因此可以跨应用程序关闭保留它们),则必须考虑存储持久数据。
Best of luck, if you have any questions please let me know!
祝您好运,如果您有任何疑问,请告诉我们!
#1
1
You are correct in the assumption that you have to use getters/setters. So, first off add a getter method and a boolean at the top of your login JFrame class.
假设您必须使用getter / setter,这是正确的。因此,首先在登录JFrame类的顶部添加一个getter方法和一个boolean。
public class mainFrame {
....
boolean isLoggedIn = false; // By default their not logged in
// Getter method to get value of the boolean
public boolean getIsLoggedIn() {
return isLoggedIn;
}
... Rest of code not shown
}
Then simply set the isLoggedIn
variable to true
where you want it to be:
然后只需将isLoggedIn变量设置为true即可:
private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {
... Other code not shown
//this is where I need a varriable or boolean of some kind granting access
// to other portion of program
// I need this value to be accessed by other classes to grant that access.
isLoggedIn = true;
dispose();
....
}
Finally, in the CreateEventActionPerformed
method you need to create or reuse the current instance of the JFrame login class and use that to invoke the getIsLoggedIn()
method.
最后,在CreateEventActionPerformed方法中,您需要创建或重用JFrame登录类的当前实例,并使用它来调用getIsLoggedIn()方法。
For example, if you wanted to create a new instance of the login class simply add this bit to your CreateEventActionPerformed
method, where LoginClass
is the name of your login class.
例如,如果要创建登录类的新实例,只需将此位添加到CreateEventActionPerformed方法,其中LoginClass是登录类的名称。
LoginClass login = new LoginClass();
boolean isLoggedIn = login.getIsLoggedIn();
You can then use the boolean to perform whatever checks or activities you like. This same process (of creating a getter method) can also be used to pass the un
variable across classes.
然后,您可以使用布尔值执行您喜欢的任何检查或活动。同样的过程(创建getter方法)也可用于跨类传递un变量。
Please note that if you want to save the variables into memory (so they can be preserved across application shutdowns) you will have to look into storing persistent data.
请注意,如果要将变量保存到内存中(因此可以跨应用程序关闭保留它们),则必须考虑存储持久数据。
Best of luck, if you have any questions please let me know!
祝您好运,如果您有任何疑问,请告诉我们!