如何将变量从一个 Jframe(或 class)传递到 java 中的另一个?
How do I pass variables from one Jframe (or class) to another in java?
我看过几个 return 值的示例,但我不完全理解如何让它们在我自己的代码中工作。我想我应该 post 在这里,看看我是否能得到一些好的建议。我需要检查用户是否已登录,如果他们已登录,则打开 window 以允许他们创建事件(我可以使逻辑与 if、then 和 else 一起工作陈述)。我需要帮助的部分是将变量从登录 class 传递到 CreateEventActionPerformed 。以下是我的 mainFrame Jframe class:
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:
}
以下来自登录jframe class:
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();
}
}
}
}
}
我还没有 post 我的所有代码我希望这足以帮助我解决这个问题。我考虑过 getter 和/或 setter 方法,但我也不确定如何使它起作用。
您必须使用 getters/setters 的假设是正确的。因此,首先在登录 JFrame class.
的顶部添加一个 getter 方法和一个布尔值
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
}
然后只需将 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();
....
}
最后,在 CreateEventActionPerformed
方法中,您需要创建或重用 JFrame 登录的当前实例 class 并使用它来调用 getIsLoggedIn()
方法。
例如,如果您想创建登录名 class 的新实例,只需将此位添加到您的 CreateEventActionPerformed
方法,其中 LoginClass
是您的登录名 class.
LoginClass login = new LoginClass();
boolean isLoggedIn = login.getIsLoggedIn();
然后您可以使用布尔值执行您喜欢的任何检查或活动。同样的过程(创建 getter 方法)也可用于在 class 中传递 un
变量。
请注意,如果您想将变量保存到内存中(以便它们可以在应用程序关闭时保留),您将不得不查看 storing persistent data.
祝你好运,如果您有任何问题,请告诉我!
我看过几个 return 值的示例,但我不完全理解如何让它们在我自己的代码中工作。我想我应该 post 在这里,看看我是否能得到一些好的建议。我需要检查用户是否已登录,如果他们已登录,则打开 window 以允许他们创建事件(我可以使逻辑与 if、then 和 else 一起工作陈述)。我需要帮助的部分是将变量从登录 class 传递到 CreateEventActionPerformed 。以下是我的 mainFrame Jframe class:
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:
}
以下来自登录jframe class:
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();
}
}
}
}
}
我还没有 post 我的所有代码我希望这足以帮助我解决这个问题。我考虑过 getter 和/或 setter 方法,但我也不确定如何使它起作用。
您必须使用 getters/setters 的假设是正确的。因此,首先在登录 JFrame class.
的顶部添加一个 getter 方法和一个布尔值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
}
然后只需将 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();
....
}
最后,在 CreateEventActionPerformed
方法中,您需要创建或重用 JFrame 登录的当前实例 class 并使用它来调用 getIsLoggedIn()
方法。
例如,如果您想创建登录名 class 的新实例,只需将此位添加到您的 CreateEventActionPerformed
方法,其中 LoginClass
是您的登录名 class.
LoginClass login = new LoginClass();
boolean isLoggedIn = login.getIsLoggedIn();
然后您可以使用布尔值执行您喜欢的任何检查或活动。同样的过程(创建 getter 方法)也可用于在 class 中传递 un
变量。
请注意,如果您想将变量保存到内存中(以便它们可以在应用程序关闭时保留),您将不得不查看 storing persistent data.
祝你好运,如果您有任何问题,请告诉我!