Java JComboBox,如何知道一次选择了什么特定选项

Java JComboBox,如何知道一次选择了什么特定选项,java,swing,combobox,jframe,Java,Swing,Combobox,Jframe,嗨,我现在正试图为我的大学工作设计一个购物车,我现在正试图做的是拥有一个组合框,当用户点击组合框中的一个选项时,会显示关于用户选择的选项的信息 我花了一段时间试图查找如何做到这一点,给出的建议是在组合框上使用actionListener。我已经添加了一个actionListerner,但是我对下一部分有点困惑。当前,当用户第一次单击某个选项时,它将正确显示文本。但是,如果用户选择另一个选项后,文本将保持不变。我需要找到一种方法来继续运行IF语句,以不断检查所选内容。我尝试在ActionListe

嗨,我现在正试图为我的大学工作设计一个购物车,我现在正试图做的是拥有一个组合框,当用户点击组合框中的一个选项时,会显示关于用户选择的选项的信息

我花了一段时间试图查找如何做到这一点,给出的建议是在组合框上使用actionListener。我已经添加了一个actionListerner,但是我对下一部分有点困惑。当前,当用户第一次单击某个选项时,它将正确显示文本。但是,如果用户选择另一个选项后,文本将保持不变。我需要找到一种方法来继续运行IF语句,以不断检查所选内容。我尝试在ActionListener中添加一个while循环,但这似乎不起作用

下面是来自侦听器的代码,以及我如何创建组合框及其选项

组合框

golfBagOptions=newjcombobox()

ActionListener

golfBagOptions.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){

            if (golfBagOptions.getSelectedItem().toString()=="TaylorMade Juggernaut Cart Bag");{
                String startNewLine = System.getProperty("line.separator");
                golfBagInformation.setText("Name : TaylorMade Juggernaut Cart Bag" + startNewLine + "Price: £129" + startNewLine + "Colour: Blue" + startNewLine + 
                        "Description: With powerful looks and crush-resistant construction, the TaylorMade Juggernaut Cart Bag is an intimidating and widely popular golf bag." );
            }
            if (golfBagOptions.getSelectedItem().toString()=="Mizuno Golf Rider Cart Bag");{
                String startNewLine = System.getProperty("line.separator");
                golfBagInformation.setText("Name : Mizuno Golf Rider Cart Bag" + startNewLine + "Price: £99.99" + startNewLine + "Colour: Black" + startNewLine + 
                        "Description: Something which is often difficult to achieve on a busy golf course is that sense of everything in its place. The Mizuno Aerolite 5 Golf Stand Bag has been created with an abundance of storage options, " +
                        "excellent golf club placement and " +
                        "reduces strain on the back when carried." );
            }
            if (golfBagOptions.getSelectedItem().toString()=="Titleist Lightweight SE Stand Bag");{
                String startNewLine = System.getProperty("line.separator");
                golfBagInformation.setText("Name : TaylorMade Juggernaut Cart Bag" + startNewLine + "Price: £99.99" + startNewLine + "Colour: Silver" + startNewLine + 
                        "Description: Carry your golf gear around the golf course effortlessly with the stylish Titleist Lightweight SE Stand Bag. Slip on this comfortable bag and you will be amazed by the " +
                        "lightweight comfort and convenience of a fast stand mechanism with broad foot and tripod rubber feet for ultimate stability." +
                        " The Titleist Lightweight SE Stand Bag is perfect for walking around the course in style." );
            }
            if (golfBagOptions.getSelectedItem().toString()=="Cleveland Golf Lightweight Stand Bag");{
                String startNewLine = System.getProperty("line.separator");
                golfBagInformation.setText("Name : TaylorMade Juggernaut Cart Bag" + startNewLine + "Price: £79.99" + startNewLine + "Colour: Blue" + startNewLine + 
                        "Description: To keep your golf clubs protected both on and off the golf course, the Cleveland Lightweight Stand Bag will assure total comfort, safety and smart construction." );
            }

        }
    });
如果有人能给我指出正确的方向,我将非常感激

干杯

改变

  if (golfBagOptions.getSelectedItem().toString()=="TaylorMade Juggernaut Cart Bag");{

调整对所有if条件的更改

1)从if语句中删除所有分号

2)要在java中比较字符串对象,请使用.equals()方法,而不是“==” 接线员

首先,不应该有一个
;if语句后面的分号

也可以使用
equals
方法代替
==


您可以通过使用
enums

以更好的方式创建它。尝试与操作命令进行比较,它可能会对您有所帮助

e.getActionCommand();

您不能只存储
golfBagOptions.getSelectedItem()的值吗
然后在if语句中使用它


这将减少运行时负载

谢谢,真不敢相信这是一个如此容易解决的问题
 if (((String)golfBagOptions.getSelectedItem()).equals("TaylorMade Juggernaut Cart Bag"){
if (golfBagOptions.getSelectedItem().toString()=="TaylorMade Juggernaut Cart Bag");
e.getActionCommand();