我知道在Java中的事件驱动程序中可以找出导致事件的对象(例如JRadioButton
被选中),因此将发生某个动作。我的问题是,如果你在一个buttongroup中有两个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美元。
答案 0 :(得分:1)
我认为问题出在这一行:
MenuFrame.totalPrice += drinksPrice;
因为“+ =”将值增加另一个值而不是简单地添加两个值。
因此,每当用户点击其中一个单选按钮时,它将不断向您的总价值添加2.10,而如果您只是说:
MenuFrame.totalPrice = MenuFrame.totalPrice + drinksPrice;
它会将您的总价格设置为当前总价格加上饮料价格,而不是每次按下按钮时将总价格加上2.10。
答案 1 :(得分:0)
也许我误解了这个问题,但我正在考虑创建一个公共变量,然后在动作监听器中使用刚刚选中的单选按钮更新变量。当所选事件触发时,您可以查看变量以查看上次选择了哪个单选按钮,执行您想要的操作,然后使用新的单选按钮进行更新。