【问题标题】:Action Listener within Action Listener动作监听器中的动作监听器
【发布时间】:2021-07-02 13:27:20
【问题描述】:

我正在为学校项目创建自动售货机,当用户单击 A1、A2、B1、B2 等时,我必须更新数字。小数点后的所有内容都会更改,但之前的所有内容都不会更改。所以如果我点击设置为 4 美元 50 美分的 A1,然后我选择了 1 美元 5 美分的 D4,我的JTextField 显示为 4 美元 5 美分。

这是 GUI 上的按钮:

this is for buttons A and B

for buttons C and D

public void cost() {
    
     C_button.addActionListener (new ActionListener () {
         public void actionPerformed (ActionEvent e) {
          button_1.addActionListener (new ActionListener () {
             public void actionPerformed (ActionEvent e) {
                total_order = 2 + 0.5;
                cost_total.setText(String.valueOf(total_order));

             }
            });
          button_2.addActionListener (new ActionListener () {
             public void actionPerformed (ActionEvent e) {
                total_order = 2 + 0.25;
                cost_total.setText(String.valueOf(total_order));

             }
            });
          button_3.addActionListener (new ActionListener () {
             public void actionPerformed (ActionEvent e) {
                total_order = 2 + 0.10;
                cost_total.setText(String.valueOf(total_order));

             }
            });
          button_4.addActionListener (new ActionListener () {
             public void actionPerformed (ActionEvent e) {
                total_order = 2 + 0.05;
                cost_total.setText(String.valueOf(total_order));

             }
            });
         } 
        });

       D_button.addActionListener (new ActionListener () {
             public void actionPerformed (ActionEvent e) {
              button_1.addActionListener (new ActionListener () {
                 public void actionPerformed (ActionEvent e) {
                    total_order = 1 + 0.5;
                    cost_total.setText(String.valueOf(total_order));

                 }
                });
              button_2.addActionListener (new ActionListener () {
             public void actionPerformed (ActionEvent e) {
                total_order = 1 + 0.25;
                cost_total.setText(String.valueOf(total_order));

             }
            });
              button_3.addActionListener (new ActionListener () {
             public void actionPerformed (ActionEvent e) {
                total_order = 1 + 0.10;
                cost_total.setText(String.valueOf(total_order));

             }
            });
              button_4.addActionListener (new ActionListener () {
             public void actionPerformed (ActionEvent e) {
                total_order = 1 + 0.05;
                cost_total.setText(String.valueOf(total_order));

             }
            });
             }
            });

【问题讨论】:

  • 代码应该以文本的形式发布在论坛中,而不是图像。发布适当的minimal reproducible example 来演示问题。我猜你需要保留一个“总”变量。因此,当您单击 A1 时,您会将金额添加到总数中。当您单击 D4 时,您将金额添加到总数中。然后在这两种情况下,您都使用“total”变量的新值更新标签。

标签: java swing user-interface actionlistener jtextfield


【解决方案1】:

你让这种方式变得比应该的更困难。首先,我会创建一个包含所有价值组合及其成本的地图,例如:

Map<String, Double> costMap = new HashMap<>();
costMap.put("A1", 4.5);
costMap.put("A2", 4.25);

然后我会在某处创建一个字符串来跟踪用户输入:

String register = "";

然后创建一个动作来处理大部分按键按下的基本按键:

public class VendingAction extends AbstractAction {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        register += getValue(Action.NAME);
        if (costMap.containsKey(register)) {
            costLabel.setText(costMap.get(register).toString());
            register = "";
        } else if (register.length() == 2) {
            //handle bad choice
            register = "";
        }
    }
}

然后当你创建你的按钮时,它会是这样的:

JButton buttonA = new JButton(new VendingAction("A"));
JButton buttonB = new JButton(new VendingAction("B"));
//so on and so forth.

【讨论】:

  • 使用Map.of("A1", 450, "A2", 425,..); 可能更容易,只是为了减少打字。该值可以除以100.0。获取实际金额。
  • 呃,我不知道它存在。早在所有函数式编程的东西出现并且最近做了很多 Go 之前,我就是一个老派的 Java 开发人员。 Java 的一些新增功能非常棒。
  • 它从 Java 9 开始就存在。它创建了一个不可变映射,这不是问题,因为如果需要更改它可以传递给实现构造函数。不利的一面是它仅限于大约 10 个入场对,因此进一步考虑可能不合适。我很懒,我可能会创建一串键值对并以这种方式填充地图。无论如何,我同意你的回答。
猜你喜欢
  • 2013-12-24
  • 2011-09-26
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
相关资源
最近更新 更多