【问题标题】:Java determining if which JRadioButton was selectedJava 确定是否选择了哪个 JRadioButton
【发布时间】:2017-04-12 20:47:44
【问题描述】:

我知道在 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 美元。

【问题讨论】:

    标签: java swing jradiobutton


    【解决方案1】:

    我认为问题出在这一行:

    MenuFrame.totalPrice += drinksPrice;
    

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

    因此,每次用户单击其中一个单选按钮时,它都会不断为您的总价值增加 2.10,而如果您只是说:

    MenuFrame.totalPrice = MenuFrame.totalPrice + drinksPrice;
    

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

    【讨论】:

      【解决方案2】:

      也许我误解了这个问题,但我正在考虑创建一个公共变量,然后在动作侦听器内部使用刚刚选择的单选按钮更新变量。当所选事件触发时,可以查看变量以查看已选择哪个单选按钮,执行您想要的信息,然后使用新的单选按钮更新它。

      【讨论】:

      • 我会更具体。我正在创建一个“虚构的”餐厅计划。在其中,我列出了几种价格相同的饮料(例如百事可乐:2.10 美元,Mountain Due:2.10 美元等)。这些列出的饮料是 JRadioButtons。一旦客户单击其中一个按钮“点一杯饮料”,2.10 美元将被添加到“总”变量中。但是,当用户想要更换饮料时会出现问题,因为如果他们单击不同的 JRadioButton,$2.10 仍将添加到“total”变量中。我想这样做,这样他们就可以在那里更换饮料,而无需每次在订单中增加 2.10 美元。
      • 我现在明白了。如果没有看到现有代码,很难告诉您应该做什么。你确实提到了一个“总”变量。用户是否可以在选择饮品后改变主意并决定不点一杯饮品?
      • 对不起,我还没有打算发送上面的回复...我建议可能存储一个“drinkTotals”变量,如果您选择一个代表饮料的单选按钮您将金额存储到,如果他们更改饮料,则您在变量中更改该金额,如果选择不喝酒,则将 0 存储到其中。然后使用它(和其他变量)对“总”变量求和。你有现有的代码吗?
      • 是的。还有另一个 JRadioButton 只是说“无”。它只会将 0 添加到“总”变量中。
      • 我添加了代码。希望你能理解。 MenuFrame、TaxAndGratuityFrame 和 Receipt 是其他类
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      相关资源
      最近更新 更多