2016-05-25 7 views
1

ビーフボタン、ビーガンボタンなどはすべて、それらの横に価格が表示されます。下部に他の人(マスタードケチャップと他の調味料も数字を持っている)私はすべての3つのチェックボックスのうち、TotalArgentをintに番号を追加する方法を知っておく必要があると3点のJRadioButtonJCheckBoxまたはJButtonがチェックされるたびに番号を追加する手助けが必要

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Shopping 
{ 
    public static void main(String[] args) { 
    JFrame frame = new JFrame("Lister v1.0"); 

    int TotalArgent = 0; 
    int BeefArgent = 15; 
    int PouletArgent = 20; 
    int VeganArgent = 70; 
    int KetchupArgent = 1; 
    int MoutardArgent = 2; 
    int CorcnichonsArgent = 17; 
    JPanel entreePanel = new JPanel(); 
    final ButtonGroup entreeGroup = new ButtonGroup(); 
    JRadioButton radioButtonBeef; 
    JRadioButton radioButtonVegan; 
    JRadioButton radioButtonPoulet; 
    entreePanel.add(radioButtonBeef = new JRadioButton("Boeuf 15$")); 
    radioButtonBeef.setActionCommand("Boeuf"); 
    entreeGroup.add(radioButtonBeef); 
    entreePanel.add(radioButtonPoulet = new JRadioButton("Poulet 20$")); 
    radioButtonPoulet.setActionCommand("Poulet"); 
    entreeGroup.add(radioButtonPoulet); 
    entreePanel.add(radioButtonVegan = new JRadioButton("Végétarien 70$", true)); 
    radioButtonVegan.setActionCommand("Végétarien"); 
    entreeGroup.add(radioButtonVegan); 

    final JPanel condimentsPanel = new JPanel(); 
    condimentsPanel.add(new JCheckBox("Ketchup (1$)")); 
    condimentsPanel.add(new JCheckBox("Moutard (2$)")); 
    condimentsPanel.add(new JCheckBox("Cornichons (17$)")); 

    JPanel orderPanel = new JPanel(); 
    JButton orderButton = new JButton("Place la commande, recoivre votre totale"); 
    orderPanel.add(orderButton); 

    Container content = frame.getContentPane(); // unnecessary in 5.0+ 
    content.setLayout(new GridLayout(3, 1)); 
    content.add(entreePanel); 
    content.add(condimentsPanel); 
    content.add(orderPanel); 

    orderButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent ae) { 
     String entree = 
      entreeGroup.getSelection().getActionCommand(); 
     System.out.println("total: " + TotalArgent + "$\n" + entree + " sandwich"); 
     Component[] components = condimentsPanel.getComponents(); 
     for (Component c : components) { 
      JCheckBox cb = (JCheckBox)c; 
      if (cb.isSelected()) 
      System.out.println("With " + cb.getText()); 
     } 
     } 
    }); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(400, 200); 
    frame.setVisible(true); 
    } 
} 
+0

Moutardをクリックすると、TotalArgentの値を2ずつ増やす必要がありますか?そうですか? – pahan

答えて

0

ここで行うための一つの方法です

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Shopping 
{ 
    // Have all these static so you don't have to create a Shopping instance 
    static int TotalArgent = 0; 
    static int BeefArgent = 15; 
    static int PouletArgent = 20; 
    static int VeganArgent = 70; 
    static int KetchupArgent = 1; 
    static int MoutardArgent = 2; 
    static int CorcnichonsArgent = 17; 

    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("Lister v1.0"); 
     JPanel entreePanel = new JPanel(); 
     final ButtonGroup entreeGroup = new ButtonGroup(); 
     JRadioButton radioButtonBeef; 
     JRadioButton radioButtonVegan; 
     JRadioButton radioButtonPoulet; 
     entreePanel.add(radioButtonBeef = new JRadioButton("Boeuf 15$")); 
     radioButtonBeef.setActionCommand("Boeuf"); 
     entreeGroup.add(radioButtonBeef); 
     entreePanel.add(radioButtonPoulet = new JRadioButton("Poulet 20$")); 
     radioButtonPoulet.setActionCommand("Poulet"); 
     entreeGroup.add(radioButtonPoulet); 
     entreePanel.add(radioButtonVegan = new JRadioButton("Végétarien 70$", true)); 
     radioButtonVegan.setActionCommand("Végétarien"); 
     entreeGroup.add(radioButtonVegan); 

     final JPanel condimentsPanel = new JPanel(); 
     condimentsPanel.add(new JCheckBox("Ketchup (1$)")); 
     condimentsPanel.add(new JCheckBox("Moutard (2$)")); 
     condimentsPanel.add(new JCheckBox("Cornichons (17$)")); 

     JPanel orderPanel = new JPanel(); 
     JButton orderButton = new JButton("Place la commande, recoivre votre totale"); 
     orderPanel.add(orderButton); 

     Container content = frame.getContentPane(); // unnecessary in 5.0+ 
     content.setLayout(new GridLayout(3, 1)); 
     content.add(entreePanel); 
     content.add(condimentsPanel); 
     content.add(orderPanel); 

     orderButton.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent ae) 
      { 
       // reset the total back to zero 
       TotalArgent = 0; 
       String entree = entreeGroup.getSelection().getActionCommand(); 

       // determine which sandwich you have and set total accordingly 
       if (entree.equals("Boeuf")) 
       { 
        TotalArgent += BeefArgent; 
       } 
       else if (entree.equals("Poulet")) 
       { 
        TotalArgent += PouletArgent; 
       } 
       else if (entree.equals("Végétarien")) 
       { 
        TotalArgent += VeganArgent; 
       } 

       // Print the sandwich and total 
       System.out.println(entree + " sandwich (" + TotalArgent + "$)"); 
       Component[] components = condimentsPanel.getComponents(); 
       for (Component c : components) 
       { 
        JCheckBox cb = (JCheckBox) c; 
        if (cb.isSelected()) 
        { 
         String txt = cb.getText(); 
         System.out.println(" - With " + txt); 

         // add to the total what condiments they checked 
         if (txt.equals("Ketchup (1$)")) 
         { 
          TotalArgent += 1; 
         } 
         if (txt.equals("Moutard (2$)")) 
         { 
          TotalArgent += 2; 
         } 
         if (txt.equals("Cornichons (17$)")) 
         { 
          TotalArgent += 17; 
         } 
        } 
       } 

       // Print out the total 
       System.out.println("total: " + TotalArgent + "$"); 
      } 
     }); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(400, 200); 
     frame.setVisible(true); 
    } 
} 
関連する問題