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);
}
}
Moutardをクリックすると、TotalArgentの値を2ずつ増やす必要がありますか?そうですか? – pahan