当前位置: 首页 > 知识库问答 >
问题:

Java确定是否选择了哪个JRadioButton

温峻熙
2023-03-14

我知道,在Java中的事件驱动程序中,可以找出是什么对象导致了事件(例如,选择了JRadioButton,因此会发生特定的操作)。我的问题是,如果在一个按钮组中有两个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.1美元,山地到期:2.1美元等)。这些列出的饮料是JRadioButton。一旦客户单击其中一个按钮“订购饮料”,2.1美元将被添加到一个“总计”变量中。但是,当用户想要更改那里的饮料时会出现问题,因为如果他们单击不同的JRadioButton,2.1美元仍然会被添加到“总计”变量中。我想使其可以更改那里的饮料,而无需每次都在订单中添加2.1美元。

共有2个答案

唐珂
2023-03-14

也许我误解了这个问题,但我的思路是创建一个公共变量,然后在action listeners中使用刚刚选择的单选按钮更新变量。当所选事件激发时,您可以查看变量以查看上次选择的单选按钮,对其执行所需操作,然后使用新的单选按钮更新它。

巫马磊
2023-03-14

我认为问题出在这一行:

MenuFrame.totalPrice += drinksPrice;

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

因此,每当用户点击其中一个单选按钮时,它将持续为你的总价值增加2.10,而如果你是,你只需说:

MenuFrame.totalPrice = MenuFrame.totalPrice + drinksPrice;

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

 类似资料:
  • 问题内容: 调用获取数据库后,有什么方法可以稍后输出当前所选数据库的名称?这似乎很基础,但是我在php.net或stackoverflow上找不到任何东西(所有结果都是针对“未选择数据库”)。 问题答案: 只需使用mysql_query(或更好的mysqli_query,或最好使用PDO): 附录: 关于是否应包括在其中,有很多讨论。从技术上讲,它是Oracle的保留,可以安全地删除。如果您愿意,

  • 问题内容: 我有一个表,希望从每个ID的3(只有3)列中找到第一个非空值,从Col1开始,然后到Col2,再到Col3 注意:Col3为NEVER NULL 为了获得每个值的正确列,我使用以下SQL Select 这将返回以下内容并且可以正常工作 我想要的是返回的第三列,指示合并成功的那列。以下是我希望产生的结果集: 问题答案: 也许这行得通吗?

  • 问题内容: 假设已编写以下代码: 我试图确定已选择哪个单选按钮,然后提取用户选择的那个单选按钮的值。我该怎么做呢? 问题答案: 关键是要确保两个单选按钮共享相同的变量。然后,要知道选择了哪个变量,您只需要获取变量的值即可。 这是一个例子:

  • 问题内容: 我有三个类(,和),它们扩展了另一个类()。如何判断对象属于哪个子类?到目前为止,我有一个具有类名称的属性,但是我认为可以使用类似于javascript的typeof的运算符。(类似:) 问题答案: 您可以使用关键字。 但是请注意,需要使用它通常是不良设计的标志。通常,您应该在每个派生类中编写方法覆盖,以便您无需显式地检查是哪个类。

  • 问题内容: 我正在尝试确定给定类型()是否为可选类型,我正在使用此测试 但它始终返回false。 那么有什么办法可以做到这一点? 问题答案: 假设您要执行的操作是这样的: 可悲的是,swift当前(从Swift 2开始)不支持协方差和协变,并且不能直接针对类型进行类型检查: 一种替代方法是使特定协议扩展,并检查该类型:

  • 我正在寻找一种方法来找出用jQuery选择了哪个字段。我构建了一个函数,但它不起作用。 JS函数 在jsfiddle开放链接中测试