Java 如何根据用户在组合框中选择的内容计算总数?

Java 如何根据用户在组合框中选择的内容计算总数?,java,swing,jcombobox,Java,Swing,Jcombobox,我希望用户从组合框中选择选项,并根据他们选择的内容,输出字段将计算其中的价格。 例如,如果他们选择Intel Core i5,计算价格将增加50美元 我的错误是,我的输出框只将最高/最后的值相加 我的三个组合框是名称处理器、内存和磁盘 我想要计算价格的框名为Output 我主要需要帮助,了解如何在if语句的组合框中选择选项 private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {

我希望用户从组合框中选择选项,并根据他们选择的内容,输出字段将计算其中的价格。 例如,如果他们选择Intel Core i5,计算价格将增加50美元

我的错误是,我的输出框只将最高/最后的值相加

我的三个组合框是名称处理器、内存和磁盘

我想要计算价格的框名为Output

我主要需要帮助,了解如何在if语句的组合框中选择选项

 private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                

   double price; //initilize variable
   double windowsUpgrade; //Initilize variable
   double processorPrice;
   double memoryPrice;
   double diskPrice;

   if(Processor.getSelectedItem().equals("Intel Core i3"));
   {
       processorPrice = 0;
   }

   if(Processor.getSelectedItem().equals("Intel Core i5"));
   {
       processorPrice = 50;
   }

   if(Processor.getSelectedItem().equals("Intel Core i7"));
   {
       processorPrice = 150;
   }

   if(Memory.getSelectedItem().equals("4GB"));
   {
       memoryPrice = 0;
   }

   if(Memory.getSelectedItem().equals("8GB"));
   {
       memoryPrice = 50;
   }

   if(Memory.getSelectedItem().equals("16GB"));
   {
       memoryPrice = 100;
   }

   if(Memory.getSelectedItem().equals("32GB"));
   {
       memoryPrice = 150;
   }

   if(Disk.getSelectedItem().equals("1TB"));
   {
       diskPrice = 0;
   }


   if(Disk.getSelectedItem().equals("2TB"));
   {
       diskPrice = 50;
   }

   if(Disk.getSelectedItem().equals("512GB SSD"));
   {
       diskPrice = 150;
   }


   double calculation;

   calculation = processorPrice + memoryPrice + diskPrice;

  NumberFormat currency = NumberFormat.getCurrencyInstance();
String message =
    currency.format(calculation);

    Output.setText(message);



}                                               

下面是代码。

您可以使用listener,它会根据您的选择做出反应

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

class ComboBoxTest
{
    //A reference to the UI class where you initialize the buttons,
    //combo box etc
    private ComboBoxUI _ui;

    private double _processorPrice;
    private double _memoryPrice;
    private double _diskPrice;

    public ComboBoxTest()
    {
        _processorPrice = 0;
        _memoryPrice = 0;
        _diskPrice = 0;
        _ui = new ComboBoxUI();
        registerListener();
    }

    //Register three different comboBoxes and a button for 
    //the calculation to listener
    private void registerListener()
    {
        _ui.getProcessorBox().addItemListener(new ItemListener()
        {
            @Override
            public void itemStateChanged(ItemEvent e)
            {
                if (e.getItem().equals("Intel Core i3"))
                {
                    _processorPrice = 0;
                }
                else if (e.getItem().equals("Intel Core i5"))
                {
                    _processorPrice = 50;
                }
                else if (e.getItem().equals("Intel Core i7"))
                {
                    _processorPrice = 150;
                }
            }
        });

        _ui.getMemoryBox().addItemListener(new ItemListener()
        {
            @Override
            public void itemStateChanged(ItemEvent e)
            {
                if (e.getItem().equals("4GB"))
                {
                    _memoryPrice = 0;
                }
                else if (e.getItem().equals("8GB"))
                {
                    _memoryPrice = 50;
                }
                else if (e.getItem().equals("16GB"))
                {
                    _memoryPrice = 100;
                }
                else if (e.getItem().equals("32GB"))
                {
                    _memoryPrice = 150;
                }
            }
        });

        _ui.getDiscBox().addItemListener(new ItemListener()
        {
            @Override
            public void itemStateChanged(ItemEvent e)
            {
                if (e.getItem().equals("1TB"))
                {
                    _diskPrice = 0;
                }
                else if (e.getItem().equals("2TB"))
                {
                    _diskPrice = 50;
                }
                else if (e.getItem().equals("512GB SSD"))
                {
                    _diskPrice = 150;
                }
            }
        });

        //getCalcButton() is a new Button that calcualtes the price
        _ui.getCalcButton().addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println(_processorPrice + _memoryPrice + _diskPrice);
            }
        });
    }
}
在我的示例中,您需要组件的UI类。类似这样的东西:)


这就是将数据封装到一个对象中会有所帮助的地方,您可以将描述和价格组合到一个对象中,然后只需从
JComboBox
中选择项目,并从对象中获取
price
,在我们能够帮助您之前,我们需要更多的上下文。考虑提供一个说明你的问题的方法。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱和更好的响应谢谢你的帮助,但我不是100%确定你在说什么,但这个。这会取代我的代码吗?comboboxUI也有一个错误。找不到符号
import java.awt.BorderLayout;

import javax.swing.*;

public class ComboBoxUI
{
    JFrame _frame;
    JPanel _componentPanel;

    JButton _calcButton;

    JComboBox _procBox;
    JComboBox _memBox;
    JComboBox _discBox;

    ComboBoxUI()
    {
        initFrame();
        initComponenentPanel();
        showGui();
    }

    //initialize a new JFrame with BorderLayout in this example
    private void initFrame()
    {
        _frame = new JFrame("Title");
        _frame.setLayout(new BorderLayout());
    }

    //initialize a new JPanel with your components like comboBox, buttons...
    private void initComponenentPanel()
    {
        _componentPanel = new JPanel();

        String[] proc = { "Intel Core i3", "Intel Core i5", "Intel Core i7"};
        _procBox = new JComboBox(proc);

        String[] memory = { "4GB", "8GB", "16GB", "32GB"};
        _memBox = new JComboBox(memory);

        String[] disc = { "1TB", "2TB", "512GB SSD"};
        _discBox = new JComboBox(disc);

        _calcButton = new JButton("Calculate");

        _componentPanel.add(_procBox);
        _componentPanel.add(_memBox);
        _componentPanel.add(_discBox);
        _componentPanel.add(_calcButton);

        _frame.add(_componentPanel, BorderLayout.NORTH);
    }

    //Set the size of your JFrame and make it visible
    private void showGui()
    {
        _frame.setSize(500, 200);
        _frame.setVisible(true);
        _frame.setDefaultCloseOperation(_frame.EXIT_ON_CLOSE);
    }


    public JComboBox getProcessorBox()
    {
        return _procBox;
    }

    public JComboBox getMemoryBox()
    {
        return _memBox;
    }

    public JComboBox getDiscBox()
    {
        return _discBox;
    }

    public JButton getCalcButton()
    {
        return _calcButton;
    }
}