Add values when JCheckBox clicked

后端 未结 3 1774
无人及你
无人及你 2020-12-22 02:04

My program contains a label with the caption “Choose a coffee” and four check boxes – Americano, Espresso, Double Espresso and Latte. Note: A JCheckBox array is recommended

3条回答
  •  情深已故
    2020-12-22 02:33

    Although both answers posted here are very helpful I'm adding this one because IMHO arrays are the less Object Oriented thing ever. You can achieve a more robust and OO solution following this hints:

    • Use putClientProperty() method inherited from JComponent to hold the prices in the check boxes.
    • Implement an ActionListener to add/substract the check box price to the total depending on the check box state. Use getClientProperty() method to retrieve the value stored in the check box.

    See the example below:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.math.BigDecimal;
    import java.math.BigInteger;
    import javax.swing.BoxLayout;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Demo {
    
        BigDecimal total = new BigDecimal(BigInteger.ZERO);
    
        private void createAndShowGUI() {        
    
            final JLabel totalLabel = new JLabel("Total: ");
    
            ActionListener actionListener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JCheckBox checkBox = (JCheckBox)e.getSource();
                    BigDecimal value = (BigDecimal)checkBox.getClientProperty("price");
                    total = checkBox.isSelected() ? total.add(value) : total.subtract(value);
                    StringBuilder sb = new StringBuilder("Total: ").append(total);
                    totalLabel.setText(sb.toString());
                }
            };
    
            JCheckBox americano = new JCheckBox("Americano");
            americano.addActionListener(actionListener);
            americano.putClientProperty("price", new BigDecimal("3.75"));
    
            JCheckBox espresso = new JCheckBox("Espresso");
            espresso.addActionListener(actionListener);
            espresso.putClientProperty("price", new BigDecimal("4.00"));
    
            JCheckBox doubleEspresso = new JCheckBox("Double Espresso");
            doubleEspresso.addActionListener(actionListener);
            doubleEspresso.putClientProperty("price", new BigDecimal("4.50"));
    
            JCheckBox latte = new JCheckBox("Latte");
            latte.addActionListener(actionListener);
            latte.putClientProperty("price", new BigDecimal("3.50"));
    
            JPanel content = new JPanel();
            content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
            content.add(americano);
            content.add(espresso);
            content.add(doubleEspresso);
            content.add(latte);
            content.add(totalLabel);
    
            JFrame frame = new JFrame("Demo");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(content);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {                
                    new Demo().createAndShowGUI();
                }
            });
        }    
    }
    

    This way you can forget about mapping every check box with a value using arrays or maps. If you need to add a new sort of coffee you should simply add 4 lines like this:

    JCheckBox newCoffee = new JCheckBox("New Coffee");
    newCoffee.addActionListener(actionListener);
    newCoffee.putClientProperty("price", new BigDecimal("4.00"));
    
    content.add(newCoffee);
    

提交回复
热议问题