gpt4 book ai didi

Java 确定是否选择了哪个 JRadioButton

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

我知道在Java中的事件驱动程序中可以找出哪个对象引起了事件(例如选择了JRadioButton,因此将发生特定的操作)。我的问题是,如果按钮组中有 2 个 JRadioButton,并且都添加了操作监听器,并且您不断从一个到另一个进行选择,是否可以找出 JRadioButton 之前被选择过吗?换句话说,如果我选择了另一个 JRadioButton,是否可以编写代码来确定在选择当前 JRadioButton 之前先前选择了哪个 JRadioButton

public class Drinks extends JPanel implements ActionListener{
double drinksPrice = 2.10;
double noDrinks = 0;
static String selectedDrink;
JRadioButton btnPepsi = new JRadioButton("Pepsi"); //add a button to choose different drinks

JRadioButton btnMtDew = new JRadioButton("Mt Dew");

JRadioButton btnDietPepsi= new JRadioButton("Diet Pepsi");

JRadioButton btnCoffee = new JRadioButton("Coffee");

JRadioButton btnTea = new JRadioButton("Tea");

JRadioButton btnNone = new JRadioButton("None");
JLabel lblDrinksHeading = new JLabel("Choose a drink (each drink is $2.10):");
ButtonGroup drinksButtonGroup = new ButtonGroup();



private static final long serialVersionUID = 1L;

public Drinks(){

setLayout(new FlowLayout()); //Using GridLayout

btnPepsi.setActionCommand(btnPepsi.getText()); //set the ActionCommand to getText so I can retrieve the name for the receipt

btnMtDew.setActionCommand(btnMtDew.getText());

btnDietPepsi.setActionCommand(btnDietPepsi.getText());

btnCoffee.setActionCommand(btnCoffee.getText());

btnTea.setActionCommand(btnTea.getText());

btnNone.setActionCommand(btnNone.getText());

drinksButtonGroup.add(btnPepsi);
drinksButtonGroup.add(btnMtDew);
drinksButtonGroup.add(btnDietPepsi);
drinksButtonGroup.add(btnCoffee);
drinksButtonGroup.add(btnTea);
drinksButtonGroup.add(btnNone);
btnNone.setSelected(true); //set default to "none"

btnPepsi.addActionListener(this);
btnMtDew.addActionListener(this);
btnDietPepsi.addActionListener(this);
btnCoffee.addActionListener(this);
btnTea.addActionListener(this);
btnNone.addActionListener(this);

add(lblDrinksHeading);

add(btnPepsi);

add(btnDietPepsi);

add(btnMtDew);

add(btnCoffee);

add(btnTea);

add(btnNone);


repaint();
revalidate();

selectedDrink = drinksButtonGroup.getSelection().getActionCommand();
//add the drink price to totalPrice, it is adding it every time though, even if its none
/*if(drinksButtonGroup.getSelection() == btnNone){
MenuFrame.totalPrice += 0;
}
else{
MenuFrame.totalPrice += drinksPrice;
}
*/


// buttonGroup1.getSelection().getActionCommand()

//String selectedDrink = drinksButtonGroup.getSelection().toString();
//class

}

@Override
public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if(source == btnNone) {

MenuFrame.totalPrice += 0;
TaxAndGratuityFrame.subtotalTextField.setText("$" + MenuFrame.totalPrice);
TaxAndGratuityFrame.subtotalVariable = MenuFrame.totalPrice;
TaxAndGratuityFrame.taxVariable = TaxAndGratuityFrame.subtotalVariable * TaxAndGratuityFrame.TAX_RATE;
TaxAndGratuityFrame.taxTextField.setText("$" + TaxAndGratuityFrame.taxVariable);
Receipt.receiptTotal.setText("Total: $" + (MenuFrame.totalPrice));
Receipt.receiptsubtotal.setText("Subtotal: " + (TaxAndGratuityFrame.subtotalVariable));

}

else {

MenuFrame.totalPrice += drinksPrice;
TaxAndGratuityFrame.subtotalTextField.setText("$" + MenuFrame.totalPrice);
TaxAndGratuityFrame.subtotalVariable = MenuFrame.totalPrice;
TaxAndGratuityFrame.taxVariable = TaxAndGratuityFrame.subtotalVariable * TaxAndGratuityFrame.TAX_RATE;
TaxAndGratuityFrame.taxTextField.setText("$" + TaxAndGratuityFrame.taxVariable);
Receipt.receiptTotal.setText("Total: $" + (MenuFrame.totalPrice));
Receipt.receiptsubtotal.setText("Subtotal: " + (TaxAndGratuityFrame.subtotalVariable));

}

}

编辑:我会更具体。我正在创建一个“想象的”餐厅程序。在其中,我列出了几种价格相同的饮料(例如百事可乐:2.10 美元,Mountain Due:2.10 美元等)。这些列出的饮料是 JRadioButtons。一旦客户点击其中一个按钮“点一杯饮料”,2.10 美元将被添加到“总计”变量中。然而,当用户想要更换那里的饮料时,就会出现问题,因为如果他们单击不同的 JRadioButton,$2.10 仍将添加到“total”变量中。我想让他们可以更换那里的饮料,而无需每次在订单中添加 2.10 美元。

最佳答案

我认为问题出在这一行:

MenuFrame.totalPrice += drinksPrice;

因为“+=”将一个值增加另一个值,而不是简单地将两个值相加。

因此,每次用户单击其中一个单选按钮时,它都会不断地将 2.10 添加到您的总值(value)中,而如果您只是说:

MenuFrame.totalPrice = MenuFrame.totalPrice + drinksPrice;

它会将您的总价设置为当前总价加上饮料价格,而不是每次按下按钮时为总价加上 2.10。

关于Java 确定是否选择了哪个 JRadioButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40856011/

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