Java从按钮按下获取变量的结果

Java从按钮按下获取变量的结果,java,random,methods,Java,Random,Methods,我有一个游戏,玩家相对于骰子上掷骰子的数量移动。掷5,玩家向右移动5格。我的代码有一个按钮,当按下时,就会滚动模具。我希望玩家得到这个结果,并将其移动到相应的位置。然而,我的玩家从掷骰子的方法中得到答案,而不是按钮的结果。这意味着当玩家移动他们的人时,它会再次掷骰子。这很难解释,但希望我的代码可以帮助解释: 骰子法 public int diceRoll() { Random rand = new Random(); int n = rand.nextI

我有一个游戏,玩家相对于骰子上掷骰子的数量移动。掷5,玩家向右移动5格。我的代码有一个按钮,当按下时,就会滚动模具。我希望玩家得到这个结果,并将其移动到相应的位置。然而,我的玩家从掷骰子的方法中得到答案,而不是按钮的结果。这意味着当玩家移动他们的人时,它会再次掷骰子。这很难解释,但希望我的代码可以帮助解释:

骰子法

public int diceRoll()
    {
        Random rand = new Random();
        int  n = rand.nextInt(4) + 1;
        System.out.print(n);
        return n;   
    }
骰子按钮

diceRollBut.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                diceRoll();
            }
        });
运动

moveC1But.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if(playerTurn == true)
                {

                    counterY1 -= 51 * diceRoll();     //As you can see, it's grabbing the method here, I want it to grab the result of the button push
                    firstPanel.repaint();


                }
            }
       });

听起来你已经明白问题所在了。用相应的按钮值替换呼叫?是的,我已确定问题。我只是不知道如何把握结果。我如何调用按钮值?谢谢将其存储在变量中或作为参数发送,但不确定侦听器是否可以这样做。这就是我的意思。我真的被这样一个小问题困住了我该如何将它存储为变量?int diceRollInt=diceRullBut?
   static int rollValue;
...

 diceRollBut.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    rollValue = diceRoll();
                }
            });