每次检查JCheckBox或JButton时都需要帮助添加数字

时间:2016-05-25 18:43:07

标签: java numbers jcheckbox jradiobutton

牛肉按钮,素食按钮和其他所有人在打开时都有价格。底部的其他人(Mustard Ketchup和其他调味品也有数字)我只需要知道如何在所有3个复选框和3个JRadioButtons中添加数字到TotalArgent中

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);
  }
}

1 个答案:

答案 0 :(得分: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);
    }
}