Java 如何从按下按钮时调用的方法中获取返回值?

Java 如何从按下按钮时调用的方法中获取返回值?,java,swing,actionlistener,Java,Swing,Actionlistener,因此,我有一个按钮,按下该按钮时,通过使用actionListener调用以下方法: public Die[] roll(){ Die[] playerRoll = new Die[5]; //code goes in here... return playerRoll; } 我想在按下按钮后访问返回的playerRoll阵列,但我不确定如何操作。这就是我所尝试的: Die[] returnedArray = throwButton.addActionListe

因此,我有一个按钮,按下该按钮时,通过使用actionListener调用以下方法:

public Die[] roll(){

    Die[] playerRoll = new Die[5];

    //code goes in here...

    return playerRoll;
}
我想在按下按钮后访问返回的playerRoll阵列,但我不确定如何操作。这就是我所尝试的:

Die[] returnedArray  = throwButton.addActionListener(new MyActionListener());
但是它给了我一个错误,说
类型不匹配:无法从void转换为Die[]

我的整个代码包括actionListener中的代码,我只想访问返回的变量。如果你需要额外的代码,我会编辑这篇文章

编辑。我尽量少粘贴代码,因为有很多代码,所以我尝试粘贴相关内容:

 public class Dice {
 static JButton throwButton = new JButton("Throw!");
static JButton scoreTitle = new JButton("Score!");
 static JLabel playerRoll1 = new JLabel();
static JLabel playerRoll2 = new JLabel();
static JLabel playerRoll3 = new JLabel();

 Die[] playerCurrentRoll;
 boolean playerRolled = false;
 static JButton throwButton = new JButton("Throw!");
 frame.getContentPane().add(scoreTitle);
 frame.getContentPane().add(playerRoll1);
    frame.getContentPane().add(playerRoll2);
    frame.getContentPane().add(playerRoll3);
 public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel jp = new JPanel();
    frame.setContentPane(jp);

    frame.getContentPane().add(throwButton);
 throwButton.addActionListener(new MyActionListener());
 frame.setVisible(true);
    frame.setSize(400, 400);
}

public Die[] roll() {
Die[] playerRoll = new Die[5];
//A lot of code in here but it basically populates the playRoll array. 
//This part of the code does work.
return playerRoll

}

public void scoreMethod() {
for (int i =0; i < playerCurrentRoll.length; i++) {
        humanTotal += playerCurrentRoll[i].getValue();
        humanScore.setText(Integer.toString(humanTotal));
    }
}
}

可能我还没有粘贴代码。有些零件可能缺失,但我可以向您保证一切正常。我知道这一点,因为当我按下抛出按钮时,它会给我数组的输出(如system.out.println中的myactionlistener类所示)。给我数组的内存地址。当我按下“分数”按钮,它将我指向包含错误的行时,它就不起作用了,错误是:

for (int i =0; i < playerCurrentRoll.length; i++) {
for(int i=0;i
位于scoreMethod()中;在Dice类中。

试试这个

Die[] returnedArray; // member variable

// Inside the container's constructor
throwButton.addActionListener(new MyActionListener());

public Die[] roll(){
    Die[] playerRoll = new Die[5];

    //code goes in here...
    return playerRoll;
}

// Your custom ActionListener
class MyActionListener implements ActionListener {
    @Override
    public void onActionPerformed(ActionEvent e) {
         returnedArray = roll();
    }
}
试试这个

Die[] returnedArray; // member variable

// Inside the container's constructor
throwButton.addActionListener(new MyActionListener());

public Die[] roll(){
    Die[] playerRoll = new Die[5];

    //code goes in here...
    return playerRoll;
}

// Your custom ActionListener
class MyActionListener implements ActionListener {
    @Override
    public void onActionPerformed(ActionEvent e) {
         returnedArray = roll();
    }
}
你只要做:

returnedArray = roll();
在action listener内部,将Die[]returnedArray设置为全局变量。

您只需执行以下操作:

returnedArray = roll();

在action listener的内部,将Die[]returnedArray设置为全局变量。

您基本上需要将action listener添加到按钮中并
操作:

 button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){

        }
 });      
每次单击按钮时都会调用
actionPerformed
方法

因此,在它的体内,你只需调用:

Die[] returnedArray = roll();
按下按钮后,
returnedArray
将等于
roll()
方法返回的数组


希望这足够清楚。

您基本上需要在按钮中添加和
ActionListener

 button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){

        }
 });      
每次单击按钮时都会调用
actionPerformed
方法

因此,在它的体内,你只需调用:

Die[] returnedArray = roll();
按下按钮后,
returnedArray
将等于
roll()
方法返回的数组


希望这足够清楚。

你真的需要
类MyActionListener
吗?匿名类有什么问题吗?OP使用了一个自定义的
ActionListener
类。我想保持这种方式。@Sparta这是一个很好的方式。但我现在得到了一个nullpointerException。我想这与我的assig有关ning..@TarikhChouhan如果你给我看更多的代码,我们可以解决这个问题。@Sparta哦,天哪,我不敢相信我犯了那个错误!!我现在已经纠正了错误。发生的事情是在scoreMethod中,我重新初始化了playerRoll数组。谢谢你的帮助。非常感谢:)你真的需要
类MyActionListener
?什么是匿名类有问题吗?OP使用了一个自定义的
ActionListener
类。我想保持这种方式。@Sparta这是一个很好的方式。但是我现在得到了一个nullpointerException。我想这与我的赋值有关。@TarikhChouhan如果你给我更多的代码,我们可以解决这个问题。@Sparta哦,我的天啊,我不能我相信我犯了那个错误!!我现在修复了错误。发生的事情是在scoreMethod中,我重新初始化了playerRoll数组。谢谢你的帮助。非常感谢:)