gpt4 book ai didi

java - 单击 JCheckBox 时添加值

转载 作者:行者123 更新时间:2023-11-29 07:50:10 25 4
gpt4 key购买 nike

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

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

我无法弄清楚如何在选中复选框时添加成本,以及在使用数组取消选中复选框时删除成本。任何帮助表示赞赏。

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

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 数组仅包含空元素:

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

因此您需要在实例化实际复选框后创建数组。

...

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 循环。你需要索引。然后,每次选择或取消选择任何内容时,您都需要重新计算全部成本。

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

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

  • 您应该始终为这种关联创建一个类。
  • 您永远不应该使用double 来赚钱。使用 BigDecimal 或类似的东西。

使用类可以使逻辑更简单,不使用 double 可以使这个十进制加法的计算不会出错。

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

关于java - 单击 JCheckBox 时添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21790021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com