将静态变量的值从类保存到另一个java

时间:2017-03-15 11:35:09

标签: java class jframe

我遇到了问题。当我尝试存储我试图从另一个类访问的全局公共静态值时。

在课堂上移动我有按钮事件的功能。

import static monopoly.Interface.zar1;
import static monopoly.Interface.zar2;


public class Move {

public int rolezar1;
public int rolezar2;
public int location=0;
public int overstart=0;  //after start
public int untilstart=0;  //until start
public int locbefore=0; //save location before passing over start

public void End(java.awt.event.ActionEvent evt) {                                    

}                                   

public void RollAction(java.awt.event.ActionEvent evt) {                                     

    Dice dice = new Dice();   

    rolezar1 = dice.RollDice();
    rolezar2 = dice.RollDice();
    zar1.setText(Integer.toString(rolezar1));
    zar2.setText(Integer.toString(rolezar2));

    Integer pfinal = rolezar2 + rolezar1;

    int[] posx = new int[]{95, 145, 195, 245, 295, 345, 395, 445, 495, 495, 495, 495, 495, 495, 495, 495, 495, 495, 445, 395, 345, 295, 245, 195, 145,  95,  45,  45,  45,  45,  45,  45,  45,  45, 45, 45};
    int[] posy = new int[]{60,  60,  60,  60,  60,  60,  60,  60,  60, 110, 160, 210, 260, 310, 360, 410, 460, 510, 510, 510, 510, 510, 510, 510, 510, 510, 510, 460, 410, 360,310, 260, 210, 160, 110, 60};

    int i = 0;

    int loc = rolezar1 + rolezar2;
    while(i <loc){
        int j=0;
        if (i+location==posx.length){
            untilstart=posx.length-location;
            overstart=loc - untilstart;
            locbefore=location;
            location=0;
        }
        else{
            overstart=0;
            untilstart=0;
        }
        while(j<untilstart){
            Interface.Player1.setBounds(posx[locbefore+j], posy[locbefore+j], 20, 20);
            j++;
            i++;
        }
        j=0;
        while(j<overstart){
            Interface.Player1.setBounds(posx[location+j], posy[location+j], 20, 20);
            j++;
            i++;

        }
        if(overstart==0&&untilstart==0){
            Interface.Player1.setBounds(posx[location+i], posy[location+i], 20, 20);
            i++;
        }

    }
    location+=overstart;

}                                    


public void move(){

}

}

当我在Main类中调用该函数时,该变量不会保留它的最新值

   private void RollActionPerformed(java.awt.event.ActionEvent evt) {                                     
    Move apel = new Move();

    apel.RollAction(evt);
   }

现在的问题是:如何正确使用类Main中的变量?当我把这段代码放在Main类中时,它就可以了。

1 个答案:

答案 0 :(得分:0)

我在Move类中找不到任何静态成员。只存在类属性变量。让您的程序按预期运行

将骰子设为移动成员,而非方法本地i,e

剪切Dice dice = new Dice();

并将其与其他类成员一起过去并尝试

public int rolezar1;
public int rolezar2;
public int location=0;
public int overstart=0;  //after start
public int untilstart=0;  //until start
public int locbefore=0
Dice dice = new Dice()  // Now this may work