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

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

问题描述

我的程序包含一个带有选择咖啡标题的标签和四个复选框 - Americano,Espresso,Double Espresso和Latte。注意:这里建议使用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)

我需要添加事件处理代码以允许用户购买一个或多个项目。在用户进行选择后,账单金额显示在标签中。
价格为Americano€3.75,Espresso€4.00,Double Espresso€4.50和Latte€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.

这是我到目前为止的代码:

This is my code so far:

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
};

然后分配建议使用数组,因为你可以创建另一个并行的价格数组(你做了) )。但由于您需要并行使用这些价格,因此无法为每个循环使用a。你需要索引。然后,您需要在每次选择或取消选择任何内容时重新计算全部费用。

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天全站免登陆