2016-11-29 10 views
1

Java의 이벤트 구동 형 프로그램에서 어떤 객체가 이벤트를 일으켰는지 알아낼 수 있습니다 (예 : JRadioButton이 선택되었으므로 특정 동작이 발생 함). 제 질문은, 당신이 buttongroup에 2 개의 JRadioButtons을 가지고 있다면, 그들에게 액션 리스너가 추가되어 있고, 하나에서 다른 것을 계속 선택한다면, JRadioButton이 이전에 선택되었던 것을 발견 할 수 있습니까? 즉, 다른 JRadioButton을 선택한 경우 현재 JRadioButton을 선택하기 전에 어느 것이 이전에 JRadioButton을 선택했는지 결정하는 코드를 작성할 수 있습니까?Java가 어떤 JRadioButton이 선택되었는지 판단하는 Java

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, 산림 : $ 2.10 등). 이 음료수는 JRadioButtons입니다. 고객이이 버튼 중 하나를 클릭하여 "음료 주문"을하면 "총"변수에 $ 2.10이 추가됩니다. 그러나 다른 JRadioButton을 클릭하면 $ 2.10이 "total"변수에 계속 추가되므로 음료수를 변경하려고 할 때 문제가 발생합니다. 매주 주문에 $ 2.10을 추가하지 않고도 음료수를 바꿀 수 있도록하고 싶습니다.

답변

0

아마도이 질문에 대한 오해가 있습니다.하지만 공용 변수를 만드는 방법을 생각한 다음 방금 선택한 라디오 단추로 변수를 업데이트하는 동작 수신기 내부에서 생각하고 있습니다. 선택한 이벤트가 발생하면 변수를보고 마지막으로 선택한 라디오 버튼을보고 원하는 내용을 수행 한 다음 새 라디오 버튼으로 업데이트 할 수 있습니다.

+0

더 구체적으로 말씀 드리겠습니다. 나는 "상상의"레스토랑 프로그램을 만들고 있습니다. 여기에는 같은 가격의 여러 음료가 나와 있습니다 (예 : 펩시 : $ 2.10, 산림 : $ 2.10 등). 이 음료수는 JRadioButtons입니다. 고객이이 버튼 중 하나를 클릭하여 "음료 주문"을하면 "총"변수에 $ 2.10이 추가됩니다. 그러나 다른 JRadioButton을 클릭하면 $ 2.10이 "total"변수에 계속 추가되므로 음료수를 변경하려고 할 때 문제가 발생합니다. 매주 주문에 $ 2.10을 추가하지 않고도 음료수를 바꿀 수 있도록하고 싶습니다. – MoneyC

+0

이제 알겠습니다. 기존 코드를 보지 않고도 사용자가해야 할 일을 알려주는 것은 쉽지 않습니다. 당신은 "전체"변수를 언급했다. 사용자가 마음을 바꾸고 선택한 음료를 주문하지 않기로 결정할 수있는 옵션이 있습니까? – MattCash

+0

죄송합니다. 아직 응답을 보내지 않으 셨습니다. 금액을 저장하는 음료를 나타내는 라디오 버튼을 선택하면 "drinkTotals"변수를 저장하는 것이 좋습니다. 그들은 변수에서 해당 금액을 변경 한 음료를 변경하고 음료수를 선택하지 않으면 0을 저장합니다. 그런 다음 (및 다른 변수)를 사용하여 "전체"변수를 합산하십시오. 기존 코드가 있습니까? – MattCash

1

나는 문제점이 줄 생각 :

MenuFrame.totalPrice += drinksPrice; 

때문에 "+ ="다른 값 대신에 단순히 두 값을 가산함으로써 값을 증가시킨다.

그래서 때마다 사용자가 당신이 당신이라면 반면 그것은 지속적으로, 총 값에 2.10를 추가합니다, 라디오 버튼 중 하나를 클릭은 말 :

MenuFrame.totalPrice = MenuFrame.totalPrice + drinksPrice; 

그것은 동일한 총 가격을 설정합니다 버튼을 누를 때마다 합계 값에 2.10을 더하는 대신 현재 총 가격에 음료 가격을 더한 값입니다.