希望得到你的指导:)。我有两个组合框,其中包含要转换的距离单位列表。现在,当我想选择前。来自组合框From (distance)...
的“厘米”,我真的不需要在组合框To (distance)...
中使用“厘米”,因为从厘米转换为厘米没有意义。因此,当From (distance)...
中选择“厘米”时,我希望在To (distance)...
组合框中删除它。但是,当我更改我的选择(说“Meter”)时,我希望“Centimeter”返回并且“Meter”消失等。我设法删除To (distance)...
框中的所选项目,但不知道如何当我改变我的选择时返回它。此外,当我更改选择时,下面的代码只删除To (distance)...
组合框中的相应项。请引导我找到正确的解决方案。这是相应的代码。如果需要,我可以给你整个代码。谢谢!
private String[] convertFromDistance = {"From (distance)...", "Centimeter", "Inch", "Kilometer", "Knot", "Meter", "Mile", "Millimeter", "Yard"};
private String[] convertToDistance = {"To (distance)...", "Centimeter", "Inch", "Kilometer","Knot", "Meter", "Mile", "Millimeter", "Yard"};
private JComboBox fromListDistance, toListDistance;
fromListDistance.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String list = (String)fromListDistance.getSelectedItem();
for(int i=0; i<convertToDistance.length; i++) {
if(convertToDistance[i].equals(list)) {
toListDistance.removeItem(convertToDistance[i]);
//here should go the code for adding back the item if selection is changed
}
}
}});
toListDistance = new JComboBox<String>(convertToDistance);