需要根据从 JList 中选择的项目设置双精度值

发布于 2024-09-25 04:46:37 字数 2546 浏览 2 评论 0原文

到目前为止我的代码:

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TestFile2 {

    public static void main(String args[]) {
        String size[] = {"Small", "Medium", "Large", "Extra Large"};
        String toppings[] = {"Cheese", "Pepperoni", "Sausage", "Spinach", "Pepperoncini"};
        JFrame f = new JFrame("Pizza");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JList list1 = new JList(size);
        JList list2 = new JList(toppings);
        Container c = f.getContentPane();
        JScrollPane sp1 = new JScrollPane(list1);
        sp1.setColumnHeaderView(new JLabel("Select Size"));
        JScrollPane sp2 = new JScrollPane(list2);
        sp2.setColumnHeaderView(new JLabel("Select Toppings. Hold Ctrl to select multiple toppings"));
        Box box = Box.createHorizontalBox();
        box.add(sp1);
        box.add(sp2);
        list1.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                JList jListSource = (JList) e.getSource();
                Object[] selection = jListSource.getSelectedValues();
                if (!e.getValueIsAdjusting()) {
                    System.out.println("----");
                    for (int i = 0; i < selection.length; i++) {
                        double costSize;
                        if (selection[i].equals("Small")) {
                            costSize = 7.00;
                        } else if (selection[i].equals("Medium")) {
                            costSize = 9.00;
                        } else if (selection[i].equals("Large")) {
                            costSize = 11.00;
                        } else {
                            costSize = 14.00;
                        }
                        System.out.println("selection = " + selection[i]);
                        System.out.println("selection = " + costSize);
                    }
                }
            }
        });
        c.add(box, BorderLayout.CENTER);
        f.setSize(
                300, 200);
        f.setVisible(
                true);
    }
}

我需要做一些类似于我对 list1 所做的事情。

我需要让用户从 JList 中选择披萨的大小和配料。最后我需要能够计算总成本。每个配料售价 1 美元。某个方向的任何一点都将受到极大的赞赏,因为在过去的几个小时里我一直在努力尝试不同的方法来解决这个问题。

提前致谢。

My code so far:

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TestFile2 {

    public static void main(String args[]) {
        String size[] = {"Small", "Medium", "Large", "Extra Large"};
        String toppings[] = {"Cheese", "Pepperoni", "Sausage", "Spinach", "Pepperoncini"};
        JFrame f = new JFrame("Pizza");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JList list1 = new JList(size);
        JList list2 = new JList(toppings);
        Container c = f.getContentPane();
        JScrollPane sp1 = new JScrollPane(list1);
        sp1.setColumnHeaderView(new JLabel("Select Size"));
        JScrollPane sp2 = new JScrollPane(list2);
        sp2.setColumnHeaderView(new JLabel("Select Toppings. Hold Ctrl to select multiple toppings"));
        Box box = Box.createHorizontalBox();
        box.add(sp1);
        box.add(sp2);
        list1.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                JList jListSource = (JList) e.getSource();
                Object[] selection = jListSource.getSelectedValues();
                if (!e.getValueIsAdjusting()) {
                    System.out.println("----");
                    for (int i = 0; i < selection.length; i++) {
                        double costSize;
                        if (selection[i].equals("Small")) {
                            costSize = 7.00;
                        } else if (selection[i].equals("Medium")) {
                            costSize = 9.00;
                        } else if (selection[i].equals("Large")) {
                            costSize = 11.00;
                        } else {
                            costSize = 14.00;
                        }
                        System.out.println("selection = " + selection[i]);
                        System.out.println("selection = " + costSize);
                    }
                }
            }
        });
        c.add(box, BorderLayout.CENTER);
        f.setSize(
                300, 200);
        f.setVisible(
                true);
    }
}

I need to do something similar to what I did with list1.

I need to have the user select from a JList both the size of pizza and the toppings. At the end I need to be able to calculate the total cost. Each topping is $1. Any point in a certain direction would be greatly appreciated as I have been pulling my hair out for the past few hours trying different methods for this problem.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

五里雾 2024-10-02 04:46:37

我希望这会有所帮助:) ...还有其他方法可以做到这一点...这只是其中之一...

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TestFile2 {

    private static JList list1;
    private static JList list2;

    public static void main(String args[]) {
        String size[] = {"Small", "Medium", "Large", "Extra Large"};
        String toppings[] = {"Cheese", "Pepperoni", "Sausage", "Spinach", "Pepperoncini"};
        JFrame f = new JFrame("Pizza");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        list1 = new JList(size);
        list2 = new JList(toppings);
        Container c = f.getContentPane();
        JScrollPane sp1 = new JScrollPane(list1);
        sp1.setColumnHeaderView(new JLabel("Select Size"));
        JScrollPane sp2 = new JScrollPane(list2);
        sp2.setColumnHeaderView(new JLabel("Select Toppings. Hold Ctrl to select multiple toppings"));
        Box box = Box.createHorizontalBox();
        box.add(sp1);
        box.add(sp2);
        list1.addListSelectionListener(new ListSelectionListener() 
        {

            public void valueChanged(ListSelectionEvent e)
            {
                handleEvent(e);
            }
        });
        list2.addListSelectionListener(new ListSelectionListener() 
        {

            public void valueChanged(ListSelectionEvent e)
            {
                handleEvent(e);
            }
        });
        c.add(box, BorderLayout.CENTER);
        f.setSize(
                300, 200);
        f.setVisible(
                true);
    }

    protected static void handleEvent(ListSelectionEvent e) 
    {

        double cost = 0.0;
         Object[] selection = list1.getSelectedValues();
         Object[] toppings = list2.getSelectedValues();

         if(toppings.length == 0)
             System.out.println("Please select a topping");

         if(selection.length == 0)
             System.out.println("Please select a size");

         if (!e.getValueIsAdjusting()) 
         {
             System.out.println("----");
             for (int i = 0; i < selection.length; i++) 
             {
                 double costSize;
                 if (selection[i].equals("Small")) {
                    cost = 7.00;
                 } else if (selection[i].equals("Medium")) {
                    cost = 9.00;
                 } else if (selection[i].equals("Large")) {
                    cost = 11.00;
                 } else {
                    cost = 14.00;
                 }

             }


             for (int i = 0; i < toppings.length; i++) 
             {
                cost++;
             }


             System.out.println("Total = " + cost);
         }

    }
}

I hope this will help :) ... there are other ways to do it.... this just one of them ...

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TestFile2 {

    private static JList list1;
    private static JList list2;

    public static void main(String args[]) {
        String size[] = {"Small", "Medium", "Large", "Extra Large"};
        String toppings[] = {"Cheese", "Pepperoni", "Sausage", "Spinach", "Pepperoncini"};
        JFrame f = new JFrame("Pizza");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        list1 = new JList(size);
        list2 = new JList(toppings);
        Container c = f.getContentPane();
        JScrollPane sp1 = new JScrollPane(list1);
        sp1.setColumnHeaderView(new JLabel("Select Size"));
        JScrollPane sp2 = new JScrollPane(list2);
        sp2.setColumnHeaderView(new JLabel("Select Toppings. Hold Ctrl to select multiple toppings"));
        Box box = Box.createHorizontalBox();
        box.add(sp1);
        box.add(sp2);
        list1.addListSelectionListener(new ListSelectionListener() 
        {

            public void valueChanged(ListSelectionEvent e)
            {
                handleEvent(e);
            }
        });
        list2.addListSelectionListener(new ListSelectionListener() 
        {

            public void valueChanged(ListSelectionEvent e)
            {
                handleEvent(e);
            }
        });
        c.add(box, BorderLayout.CENTER);
        f.setSize(
                300, 200);
        f.setVisible(
                true);
    }

    protected static void handleEvent(ListSelectionEvent e) 
    {

        double cost = 0.0;
         Object[] selection = list1.getSelectedValues();
         Object[] toppings = list2.getSelectedValues();

         if(toppings.length == 0)
             System.out.println("Please select a topping");

         if(selection.length == 0)
             System.out.println("Please select a size");

         if (!e.getValueIsAdjusting()) 
         {
             System.out.println("----");
             for (int i = 0; i < selection.length; i++) 
             {
                 double costSize;
                 if (selection[i].equals("Small")) {
                    cost = 7.00;
                 } else if (selection[i].equals("Medium")) {
                    cost = 9.00;
                 } else if (selection[i].equals("Large")) {
                    cost = 11.00;
                 } else {
                    cost = 14.00;
                 }

             }


             for (int i = 0; i < toppings.length; i++) 
             {
                cost++;
             }


             System.out.println("Total = " + cost);
         }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文