如何计算一个总数取决于用户在组合框中的seletcs?
问题描述:
我希望用户从组合框中选择选项,并根据他们选择的输出字段计算其中的价格。 例如,如果他们选择英特尔酷睿i5,它将增加50美元的计算价格如何计算一个总数取决于用户在组合框中的seletcs?
他们的错误是我的输出框只添加最高/最后的值在一起。
我的三个组合框的名字处理器,内存,磁盘
我要计算在价格被命名为输出
我主要就需要如何在我的组合框中选择该选项可以帮助盒子在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);
}
这是代码。
答
您可以使用对您的选择作出反应的监听器。
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;
}
}
这是你的封装数据到一个对象会有所帮助,您可以在描述和价格成一个单一的对象合并,然后简单地从'JComboBox'采取选定的项目,并获得来自对象的“价格” – MadProgrammer
在我们能够帮助您之前,我们需要更多的上下文。考虑提供一个[可运行的示例](https://stackoverflow.com/help/mcve),它可以证明你的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的困惑和更好的回应 – MadProgrammer