【问题标题】:How to add numbers to integer?如何将数字添加到整数?
【发布时间】:2015-10-30 17:56:23
【问题描述】:

按钮必须将 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");

        }
    });

【问题讨论】:

  • @sam 那,他是如何从ActionListener 内部访问money 而不是final
  • @TrippKinetics 是的。我什至没有看那部分。我看到的只是数字:)

标签: java string swing numbers actionlistener


【解决方案1】:

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");

        }
    });

【讨论】:

    猜你喜欢
    • 2015-11-21
    • 2020-09-13
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    相关资源
    最近更新 更多