首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Action Listener中的Action Listener

Action Listener中的Action Listener
EN

Stack Overflow用户
提问于 2021-04-07 02:06:47
回答 1查看 41关注 0票数 0

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

以下是GUI上的按钮:

this is for buttons A and B

for buttons C and D

代码语言:javascript
复制
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));

             }
            });
             }
            });
EN

回答 1

Stack Overflow用户

发布于 2021-04-07 02:26:25

你让这件事变得比原来更难了。首先,我将创建所有价值组合及其成本的映射,如下所示:

代码语言:javascript
复制
Map<String, Double> costMap = new HashMap<>();
costMap.put("A1", 4.5);
costMap.put("A2", 4.25);

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

代码语言:javascript
复制
String register = "";

然后创建一个Action来处理为您的大部分键按下的基本键:

代码语言:javascript
复制
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 = "";
        }
    }
}

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

代码语言:javascript
复制
JButton buttonA = new JButton(new VendingAction("A"));
JButton buttonB = new JButton(new VendingAction("B"));
//so on and so forth.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66974124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档