您好我正在尝试为我的大学工作设计一个购物车,我目前正在尝试做的是有一个comboBox,当用户点击comboBox中的选项信息时,会显示用户选择的该选项。
我花了一些时间试图查找如何执行此操作,并给出的建议是在comboBox上使用actionListener。我已经添加了一个actionListerner,但是我对下一部分感到困惑。目前,当用户第一次点击选项时,它将正确显示文本,但是如果他选择了另一个选项,则文本将保持不变。我需要找到一种方法来继续运行我的IF语句,以便继续检查已选择的内容。我尝试在我的ActionListener中添加一个while循环,但这似乎没有做到这一点。
以下是Listener中的代码以及我如何创建comboBox及其选项。
组合框
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");
的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." );
}
}
});
如果有人能指出我正确的方向,我非常感激。
干杯
答案 0 :(得分:2)
更改
if (golfBagOptions.getSelectedItem().toString()=="TaylorMade Juggernaut Cart Bag");{
到
if (((String)golfBagOptions.getSelectedItem()).equals("TaylorMade Juggernaut Cart Bag"){
调整所有if条件的更改。
1)从if语句中删除所有分号
2)比较java中的String对象使用.equals()方法而不是“==” 操作
答案 1 :(得分:1)
if (golfBagOptions.getSelectedItem().toString()=="TaylorMade Juggernaut Cart Bag");
首先,if语句之后不应该有; semicolon
。
也代替==
使用equals
方法。
您可以使用enums
以更好的方式制作它。
答案 2 :(得分:0)
尝试与动作命令进行比较,它可以帮助您
e.getActionCommand();
答案 3 :(得分:0)
您不能只存储golfBagOptions.getSelectedItem();
的值,然后在if语句中使用它。
这将减少运行时负载。