我正在申请学校应该接受来自两个选择框的位置,并将这些位置之间的英里距离设置为文本字段。我的问题是textFields没有得到我想要的数字。在与我的老师核实时,我们发现我的itemStateChange方法没有读取itemListeners的来源。任何人都可以告诉我究竟是什么问题,因为我似乎无法自学这个特定的概念。我对此问题的相关代码如下:
//Connect ActionListeners and ItemListeners
submit.addActionListener(this);
reset.addActionListener(this);
fromChoice.addItemListener(this);
toChoice.addItemListener(this);
vehicleType.addItemListener(this);
gasType.addItemListener(this);
submit.setActionCommand("Submit");
reset.setActionCommand("Reset");
...
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (arg == "Submit")
{
String val1 = miles.getText();
String val2 = gasPrice.getText();
double double1 = Double.parseDouble(val1);
double double2 = Double.parseDouble(val2);
total = double1 * double2;
totalPriceResult.setText(total.toString());
}
else if (arg == "Reset")
{
gasPrice.setText("");
miles.setText("");
}
}
public void itemStateChanged(ItemEvent c)
{
Object source = c.getItemSelectable();
if (source == gasType)
{
if (c.getStateChange() == ItemEvent.SELECTED)
{
int gasChoice = gasType.getSelectedIndex();
if (gasChoice == 0)
{
gasPrice.setText ("3.00");
}
else if (gasChoice == 1)
{
gasPrice.setText("2.90");
}
else if (gasChoice == 2)
{
gasPrice.setText("2.50");
}
else if (gasChoice == 3)
{
gasPrice.setText("4.00");
}
}
}
else if (source == fromChoice)
{
getMiles();
}
else if(source == toChoice)
{
getMiles();
}
}