Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionpackage com.creperie.test; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Carte extends Frame implements ActionListener { Frame f = new Frame("creperie"); Label entree = new Label("entree"); Checkbox salade = new Checkbox("salade"); Checkbox melon = new Checkbox("melon"); Label cs = new Label("crepe salee"); Checkbox jf = new Checkbox("jambon fromage"); Checkbox complete = new Checkbox("complete"); Label csu = new Label("crepe sucree"); Checkbox su = new Checkbox("crepe au sucre"); Checkbox choc = new Checkbox("crepe au chocolat"); Label boi = new Label("boisson"); Checkbox coca = new Checkbox("coca cola"); Checkbox cidre = new Checkbox("cidre"); Button bout = new Button("ticket"); Button quitter = new Button("quitter l'application"); Hashtable associationProduitsPrix = new HashMap(); //10 euros la salade associationProduitsPrix.put(salade, new BigInteger(10)); ==> 1 associationProduitsPrix.put(melon, new BigInteger(10)); ==> 2 public Carte() { //salade.addItemListener();//il faut utiliser la méthode addItemListener() avec un Checkbox quitter.addActionListener(new ListenerQuitter()); bout.addActionListener(this); add(entree); add(salade); add(melon); add(cs); add(jf); add(complete); add(csu); add(su); add(choc); add(boi); add(coca); add(cidre); add(bout); add(quitter); setSize(250, 250); setLayout(new FlowLayout()); //sinon n'affiche qu'un seul element ds la fenetre setVisible(true); setBackground(Color.yellow); setForeground(Color.black); } bout.addActionListener(new ActionListener() { ==> 3 public void actionPerformed(ActionEvent e) { Interger total = new Integer(0); Enumeration enu = associationProduitsPrix.keys(); while (enu.hasMoreElement()) { Checkbox check = (Checkbox) enu.nextElement(); if (check.getState()) { BigInteger prix = (Integer) associationProduitsPrix.get( check); total = total.add(prix); } } } ==> 4 class ListenerQuitter implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String[] args) { Frame f = new Carte(); } }
if (check.getState()) { BigInteger prix = (BigInteger)associationProduitsPrix.get(check); total = total.add(prix); JOptionPane.showMessageDialog (null, "Total : " + total + " \u20ac");
if (check.getState()) { BigInteger prix = (BigInteger)associationProduitsPrix.get(check); total = total.add(prix); } } JOptionPane.showMessageDialog(null, "Total : " + total+ " \u20ac"); } }); }