gpt4 book ai didi

java - JComboBox ,如何知道一次选择了哪个特定选项

转载 作者:行者123 更新时间:2023-11-29 05:31:21 25 4
gpt4 key购买 nike

您好,我目前正在尝试为我的大学作业设计一个购物车,我目前正在尝试做的是有一个组合框,当用户单击组合框中的一个选项时,会显示有关用户选择的选项的信息。

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

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

组合框

golfBagOptions = new JComboBox();

      golfBagOptions.addItem("TaylorMade Juggernaut Cart Bag");
golfBagOptions.addItem("Mizuno Golf Rider Cart Bag");
golfBagOptions.addItem("Titleist RC13 Reverse Cart Bag");
golfBagOptions.addItem("Callaway Golf Euro Chev Org Cart Bag");

Action 监听器

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 (((String)golfBagOptions.getSelectedItem()).equals("TaylorMade Juggernaut Cart Bag"){

调整所有 if 条件的变化。

1 ) Remove all the semicolon from the if statements

2 ) To compare String objects in java use .equals() method instead of "=="operator

关于java - JComboBox ,如何知道一次选择了哪个特定选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20995402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com