2017-04-23 33 views
-1

我希望用戶從組合框中選擇選項,並根據他們選擇的輸出字段計算其中的價格。 例如,如果他們選擇英特爾酷睿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); 



}            

這是代碼。

+0

這是你的封裝數據到一個對象會有所幫助,您可以在描述和價格成一個單一的對象合併,然後簡單地從'JComboBox'採取選定的項目,並獲得來自對象的「價格」 – MadProgrammer

+0

在我們能夠幫助您之前,我們需要更多的上下文。考慮提供一個[可運行的示例](https://stackoverflow.com/help/mcve),它可以證明你的問題。這不是代碼轉儲,而是您正在做的事情的一個例子,它突出了您遇到的問題。這將導致更少的困惑和更好的迴應 – MadProgrammer

回答

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

謝謝你的所有幫助,但我不是100%確定你在說什麼,但是這個。這是否替換我的代碼 – Lizzie

+0

也有comboboxUI錯誤。找不到標誌 – Lizzie

相關問題