如何使 JButton 每次单击时都添加 Doubles

How to make JButton add Doubles each time its clicked

我正在尝试使用一个按钮来每次添加双打并且现在已经使用了这个按钮。

btnAnswer3 = new JButton("C");

btnAnswer3.setBackground(Color.YELLOW);
btnAnswer3.setHorizontalAlignment(SwingConstants.LEFT);
btnAnswer3.addActionListener(new ActionListener() {double scoreAdder, currentScore, ans;
        scoreAdder = 30000.00;
        currentScore = 0.0;

        ans=scoreAdder+currentScore;        

        currentScoretxt.setText(Double.toString(ans)); //This is textfield in which I wish to display the doubles.


        }
    });

它已经显示了一个数字,但是一旦我希望它在每次单击 Jbutton 时都保持相加。请让我知道如何使用我的代码执行此操作。问候。

您需要在 ActionListener

之外声明此变量
double scoreAdder, currentScore, ans;

如果不是,您只是覆盖数学运算而不是累加值...

移动这些声明并将其作为 class 变量放置...