Java无法获取修改的Int的正确值

Java无法获取修改的Int的正确值,java,swing,methods,Java,Swing,Methods,所以我有三个类,一个是主GUI,一个是方法类,一个是登录窗口类 在我的方法课上我有 public int IsLoggedOn = 0; public int returnLoggedinValue (){ return this.IsLoggedOn ; } public int setLoggedOn(){ System.out.println("logged on "); return 1; } 在我的登录窗口中,我的按钮的操作侦听器 methodWin me

所以我有三个类,一个是主GUI,一个是方法类,一个是登录窗口类

在我的方法课上我有

public int IsLoggedOn = 0;

public int returnLoggedinValue (){
    return  this.IsLoggedOn ;
}
public int setLoggedOn(){
    System.out.println("logged on ");
    return 1;
}
在我的登录窗口中,我的按钮的操作侦听器

methodWin meth = new methodWin ();
methodWin.IsLoggedOn = methodWin.setLoggedOn();
System.out.println("logged in value "+methodWin.IsLoggedOn);
然后返回主GUI,查看启动登录窗口的操作侦听器的结尾

methodWin meth = new methodWin ();
int ar = meth.IsLoggedOn;
System.out.println("ar is "+ ar);
if (ar==1){
    System.out.println("user is logged in");
    jTextField1.setEnabled(true);
    jButton1.setEnabled(true);
}
我遇到的问题是,如果我从“登录”窗口打印出IsLoggedOn的值,它已正确地将其更改为“1”,但当我检查IsLoggedOn的值时,返回到主GUI,我会得到“0”


很抱歉,这对整个Java来说都很陌生,不太清楚为什么没有看到更改

在每次初始化methodWin的新实例时,只有在“登录”窗口中,才会将初始值从0更改为1,但这并没有完成。在主GUI中,未触及初始值,因此它保持为0

我假设您只想初始化methodWin的一个实例,并让两组代码引用它

  methodWin.IsLoggedOn = methodWin.setLoggedOn();
在日志中,您将IsLoggedOn的值设置为1; 但在主GUI中,您并没有这样做

将IsLoggedOn声明为静态

 public static int IsLoggedOn = 0;
我希望这会有所帮助

您可以尝试以下方法:

 public class MethodWin{
     private int isLoggedOn=0;
     private static MethodWin objectMethodWin = new MethodWin();
     private MethodWin(){}
     public static MethodWin getInstance(){
         return objectMethodWin;
     }
     public void setIsLoggedOn(int value){
         this.isLoggedOn=value;
     }
     public int getIsLoggedOn(){
         return this.isLoggedOn;
     }
 }
在登录窗口操作中:

 MethodWin meth = MethodWin.getInstance();
 meth.setIsLoggedOn(1);
 System.out.println("logged in value "+ meth.getIsLoggedOn);
在主GUI中:

 MethodWin meth = MethodWin.getInstance();
 int ar = meth.getIsLoggedOn();
 System.out.println("ar is "+ ar);
 if (ar==1){
     System.out.println("user is logged in");
     jTextField1.setEnabled(true);
     jButton1.setEnabled(true);
 }

首先“坚持”。其次,您正在创建methodWin类的新实例,以获取不起作用的值。@HarryJoy您是对的。这里的主要问题是两个不同的对象。将int声明为静态有效