我知道在Java中的事件驱动程序中有可能找出导致事件的对象(例如,选择了JRadioButton,因此将执行某些操作)。我的问题是,如果一个按钮组中有2个JRadioButtons,并且都添加了动作侦听器,并且您一直在选择它们,那么是否有可能找出之前选择的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,而如果您是,则只说:

MenuFrame.totalPrice = MenuFrame.totalPrice + drinksPrice;


它将使您的总价格等于当前的总价格加上饮料的价格,而不是每次按下按钮都会在总价值上加上2.10。

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

10-12 16:35