通过 JList 调整价格

Adjusting a price through a JList

因此,我将对象数组(来自枚举)添加到 JList。说 Jlist 有一个 ListSelectionListener。 Jlist 是默认的,所以你可以 select 多个项目(虽然只有 2 个)我目前这样设置(有括号,我只是删除它们):

public void valueChanged(ListSelectionEvent e) {
double price = 0;
if (selectedObject == Enum.object1) 
  price += Enum.object1.getPrice();
else if (selectedObject == Enum.object2) 
  price += Enum.object2.getPrice();
else if (selectedObject == Enum.object1 && selectedObject == Enum.object2) 
  price += Enum.object1.getPrice();
  price += Enum.object2.getPrice();
else 
 price -= Enum.object1.getPrice();
 price -= Enum.object2.getPrice();

没有准确去除价格。它只会不断添加,然后偶尔删除。

主要问题是,ListSelectionListener 不会向您提供有关某个项目是被选中还是被取消选中的信息。这意味着您被迫从 0 的值开始并循环遍历当前选定的项目并从头开始计算新的计数,例如...

import java.awt.BorderLayout;
import java.io.IOException;
import java.text.NumberFormat;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Test {

    enum Item {
        PEARS(100.0),
        APPLES(25.0),
        ICECREAME(225.30);

        private double price;

        private Item(double price) {        
            this.price = price;
        }

        public double getPrice() {
            return price;
        }

        @Override
        public String toString() {
            return name();
        }

    }

    public static void main(String[] args) throws IOException {
        new Test();
    }

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JLabel tally = new JLabel("0");
                DefaultListModel<Item> model = new DefaultListModel<>();
                model.addElement(Item.PEARS);
                model.addElement(Item.APPLES);
                model.addElement(Item.ICECREAME);
                JList<Item> listOfItems = new JList<>(model);
                listOfItems.addListSelectionListener(new ListSelectionListener() {
                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        if (e.getValueIsAdjusting()) {
                            return;
                        }
                        double price = 0;
                        for (Item item : listOfItems.getSelectedValuesList()) {
                            price += item.getPrice();
                        }
                        tally.setText(NumberFormat.getCurrencyInstance().format(price));
                    }
                });

                JFrame frame = new JFrame("Test");
                frame.add(new JScrollPane(listOfItems));
                frame.add(tally, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true); 
            }
        });
    }

}

saw something about mouse events, would that be the correct solution?

没有,为什么?因为可以通过键盘或编程方式更改选择