如何计算总数取决于用户在组合框中的seletcs?

时间:2017-04-23 20:42:46

标签: java swing jcombobox

我希望用户从组合框中选择选项,并根据他们选择的内容输出字段来计算其中的价格。 例如,如果他们选择英特尔酷睿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);



}                                               

这是代码。

1 个答案:

答案 0 :(得分:0)

您可以使用对您的选择做出反应的听众。

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类。像这样:)

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;
    }
}