您可以使用對您的選擇作出反應的監聽器。
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