gpt4 book ai didi

java - 选择 JCheckBox 时添加到变量

转载 作者:行者123 更新时间:2023-11-30 11:04:17 24 4
gpt4 key购买 nike

我需要编写一个允许用户在 JCheckBoxes 中选择保险选项的应用程序。选择 JCheckBox 后,我想将该选项的值添加到“TotalPrice”。我将 totalPrice 初始化为 0,但是当我尝试在“if”语句中添加它时,没有任何反应。

package p3s4;

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

public class JInsurance extends JFrame implements ItemListener {
//initialize variables.
FlowLayout flow = new FlowLayout();
int hmoPrice = 200;
int ppoPrice = 600;
int dentalPrice = 75;
int visionPrice = 20;
String decimal = ".00";
DecimalFormat moneyFormat = new DecimalFormat(decimal);
int totalPrice = 0;
//initialize TextFields and CheckBoxes.
JLabel heading = new JLabel("Choose insurance options: ");
JCheckBox hmo = new JCheckBox("HMO");
JCheckBox ppo = new JCheckBox("PPO");
ButtonGroup providersGroup = new ButtonGroup();
JCheckBox dental = new JCheckBox("Dental");
JCheckBox vision = new JCheckBox("Vision");
JTextField hmoSelection = new JTextField(hmo.getText() + " $" + moneyFormat.format(hmoPrice));
JTextField ppoSelection = new JTextField(ppo.getText() + " $" + moneyFormat.format(ppoPrice));
JTextField dentalSelection = new JTextField(dental.getText() + " $" + moneyFormat.format(dentalPrice));
JTextField visionSelection = new JTextField(vision.getText() + " $" + moneyFormat.format(visionPrice));
JTextField totalSelection = new JTextField("Total: $" + totalPrice);

public JInsurance() {
super("Insurance Options");

setLayout(flow);
add(heading);
providersGroup.add(hmo);
providersGroup.add(ppo);
add(hmo);
add(ppo);
add(dental);
add(vision);
add(totalSelection);
add(hmoSelection);
add(ppoSelection);
add(dentalSelection);
add(visionSelection);
//Set visibility.
hmoSelection.setVisible(false);
ppoSelection.setVisible(false);
dentalSelection.setVisible(false);
visionSelection.setVisible(false);
totalSelection.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add listeners.
hmo.addItemListener(this);
ppo.addItemListener(this);
dental.addItemListener(this);
vision.addItemListener(this);
}

public void itemStateChanged(ItemEvent event) {
Object source = event.getSource();
//Check if boxes are selected. If they are, add price of insurance
//option to "totalPrice" TextField.
if (source == hmo) {
if (event.getStateChange() == ItemEvent.SELECTED) {
totalPrice += 200;
hmoSelection.setEditable(false);
hmoSelection.setVisible(true);
hmoSelection.getParent().revalidate();

} else {
totalPrice -= 200;
hmoSelection.setVisible(false);
}
} else if (source == ppo) {
if (event.getStateChange() == ItemEvent.SELECTED) {
totalPrice += 600;
ppoSelection.setEditable(false);
ppoSelection.setVisible(true);
ppoSelection.getParent().revalidate();
} else {
totalPrice -= 600;
ppoSelection.setVisible(false);
}
} else if (source == dental) {
if (event.getStateChange() == ItemEvent.SELECTED) {
totalPrice += 75;
dentalSelection.setEditable(false);
dentalSelection.setVisible(true);
dentalSelection.getParent().revalidate();
} else {
totalPrice -= 75;
dentalSelection.setVisible(false);
}
} else {
if (event.getStateChange() == ItemEvent.SELECTED) {
totalPrice += 20;
visionSelection.setEditable(false);
visionSelection.setVisible(true);
visionSelection.getParent().revalidate();
} else {
totalPrice -= 20;
visionSelection.setVisible(false);
}
}
}

public static void main(String[] args) {
JInsurance first = new JInsurance();
final int WIDTH = 500;
final int HEIGHT = 300;
first.setSize(WIDTH, HEIGHT);
first.setVisible(true);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
first.setLocation(dim.width / 2 - first.getSize().width / 2,
dim.height / 2 - first.getSize().height / 2);
}
}

最佳答案

您需要自己更新totalSelection 的文本。这是因为当您初始化它时,您提供了一个 String 对象,该对象是当时从 totalPrice 的值生成的。

因此要修复它,请在 itemStateChanged 的末尾重置 totalSelection 的文本

关于java - 选择 JCheckBox 时添加到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30088804/

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