单击 JCheckBox 时添加值 [英] Add values when JCheckBox clicked

查看:15
本文介绍了单击 JCheckBox 时添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序包含一个标题为选择咖啡"的标签和四个复选框——美式咖啡、浓缩咖啡、双份浓缩咖啡和拿铁咖啡.注意:这里推荐使用 JCheckBox 数组)

My program contains a label with the caption "Choose a coffee" and four check boxes – Americano, Espresso, Double Espresso and Latte. Note: A JCheckBox array is recommended here)

我需要添加事件处理代码以允许用户购买一件或多件商品.用户做出选择后,账单金额会显示在标签中.价格为美式咖啡 3.75 欧元、浓缩咖啡 4.00 欧元、双份浓缩咖啡 4.50 欧元和拿铁 3.50 欧元.数组也适用于此.当用户做出选择时,会显示一个标签来显示账单.

I need to add event handling code to allow the user to purchase one or more items. The bill amount is displayed in a label after the user has made their selections. The prices are Americano €3.75, Espresso €4.00, Double Espresso €4.50 and Latte €3.50. An array is suitable here also. As the user makes a choice a label is displayed showing the bill.

我不知道如何在选中复选框时添加成本,并在使用数组取消选中复选框时删除成本.任何帮助表示赞赏.

I cant figure out how to add the cost when the check box is selected and remove the cost when it is de selected using the arrays. Any help appreciated.

这是我目前的代码:

package Lab4EventHandling;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Frame3 extends JFrame implements ActionListener {

    private Container cPane;
    private JLabel tMsg, bMsg;

    private JCheckBox americano, espresso, doubleEspresso, latte;
    JCheckBox[] boxes = new JCheckBox[]{americano, espresso, doubleEspresso, latte};

    private JPanel checkPanel = new JPanel(new GridLayout(0,1));
    private Color cl;

    private double cost = 0;

    private final int WINDOW_WIDTH = 200;
    private final int WINDOW_HEIGHT = 200;
    private final int x = 550;
    private final int y = 400;


    public Frame3()
    {
        cPane = getContentPane();

        cl = new Color(150, 150, 250);
        cPane.setBackground(cl);
        this.setLayout(new BorderLayout(0,1));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(x,y);

        this.add(checkPanel, BorderLayout.CENTER);

        tMsg = new JLabel("Choose a coffee:" ,SwingConstants.CENTER);
        tMsg.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));

        this.add(tMsg, BorderLayout.PAGE_START);

        americano = new JCheckBox("Americano", false);
        checkPanel.add(americano);
        americano.addActionListener(this);

        espresso = new JCheckBox("Espresso", false);
        checkPanel.add(espresso);
        espresso.addActionListener(this);

        doubleEspresso = new JCheckBox("Double Espresso", false);
        checkPanel.add(doubleEspresso);
        doubleEspresso.addActionListener(this);

        latte = new JCheckBox("Latte", false);
        checkPanel.add(latte);
        latte.addActionListener(this);

        bMsg = new JLabel("Bill is ");
        bMsg.setHorizontalAlignment(SwingConstants.CENTER);
        bMsg.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
        this.add(bMsg, BorderLayout.SOUTH);


        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        Double[] array = {3.75, 4.00, 4.50, 3.50}; 

        for (JCheckBox box : boxes) {

        if(americano.isSelected())
        {
            cost += 3.75;
            String r = String.valueOf(cost);
            bMsg.setText(r);
        }

        if(espresso.isSelected())
        {
            cost += 4.00;
            String r = String.valueOf(cost);
            bMsg.setText(r);
        }

        else if(doubleEspresso.isSelected())
        {
            cost = 4.50;
            String r = String.valueOf(cost);
            bMsg.setText(r);
        }

        else  if(latte.isSelected())
        {
            cost = 3.50;
            String r = String.valueOf(cost);
            bMsg.setText(r);
        }
    }

}

public class Frame3Test{

    public static void main(String [] args)
    {
        Frame3 f = new Frame3();
        f.setVisible(true);

    }
}

推荐答案

您的第一个问题(您可能注意到也可能没有注意到)是您的 JCheckBoxes 数组仅包含空元素:

Your first problem (which you may or may not have noticed) is your array of JCheckBoxes only contains null elements:

// check boxes are null here
private JCheckBox americano, espresso, doubleEspresso, latte;

// array created with only null elements
JCheckBox[] boxes = new JCheckBox[]{americano, espresso, doubleEspresso, latte};

所以你需要在实例化实际复选框之后创建数组.

So you need to create the array after instantiating the actual check boxes.

...

americano = new JCheckBox("Americano", false);
checkPanel.add(americano);
americano.addActionListener(this);

espresso = new JCheckBox("Espresso", false);
checkPanel.add(espresso);
espresso.addActionListener(this);

doubleEspresso = new JCheckBox("Double Espresso", false);
checkPanel.add(doubleEspresso);
doubleEspresso.addActionListener(this);

latte = new JCheckBox("Latte", false);
checkPanel.add(latte);

boxes = new JCheckBox[] {
    americano, espresso, doubleEspresso, latte
};

然后作业建议使用数组,因为您可以创建另一个平行的价格数组(您已经这样做了).但是由于您需要并行使用这些价格,因此您不能使用 for each 循环.你需要索引.然后,每次选择或取消选择任何内容时,您都需要重新计算整个成本.

Then the assignment is suggesting to use arrays because you can create another parallel array of prices (which you did). But since you need these prices in parallel you cannot use a for each loop. You need the index. Then you need to recalculate the entire cost every time anything is selected or deselected.

final double[] prices = {
    3.75, 4.00, 4.50, 3.50
};

...

double total = 0.0;

for(int i = 0; i < boxes.length; i++) {
    if(boxes[i].isSelected()) {
        total += prices[i];
    }
}

还有另外两个注释似乎超出了作业的范围:

There's two other notes that seem to be outside the scope of the assignment:

  • 您应该始终为这种关联创建一个类.
  • 你不应该使用 double 来赚钱.使用 BigDecimal 或类似的东西.
  • You should always make a class for this kind of association.
  • You should never use double for money. Use BigDecimal or something like it.

使用类使逻辑更简单,不使用 double 使得该小数加法的计算不会产生错误.

Using a class makes logic simpler and not using double makes the calculation not incur error for this decimal addition.

class PricePair {
    JCheckBox jCheckBox;
    BigDecimal price;
}

BigDecimal total = new BigDecimal("0.00");

for(PricePair option : options) {
    if(option.jCheckBox.isSelected()) {
        total = total.add(option.price);
    }
}

这篇关于单击 JCheckBox 时添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆