2015-10-30 91 views
-3

按钮必须添加50到整数,但不是。我不太了解Jframe,所以帮助我们。如何将数字添加到整数?

int money = 0; 
    ... 
    JButton verlan = new JButton("50 kr\u015F"); 
    verlan.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
     int moremoney = money + 50; 
     String x=Integer.toString(moremoney); 
     textArea.setText(x + " cent"); 

     } 
    }); 
+2

@sam那么,他如何从他的'ActionListener'内部访问'money'而不是'final'? –

+0

@TrippKinetics是的。我甚至没有看那部分。所有我看到的是数字:) – sam

回答

3

在你ActionListener定义上,您加起来的money和50的值的新变量,但你永远不更新的money的初始值。相反,您可以更新资金,但是您必须确保该变量在ActionListener的范围内可用,例如,通过声明它为一个成员变量。

private int money = 0; 
    ... 
    JButton verlan = new JButton("50 kr\u015F"); 
    verlan.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
     money += 50; 
     String x=Integer.toString(money); 
     textArea.setText(x + " cent"); 

     } 
    }); 
+0

是的谢谢你的帮助 –