我正在尝试制作一个货币转换器程序,并且我希望 JComboBox 项目从其他 JComboBox when I hit the convert button. 中替换
我知道错误是尝试将字符串转换为整数,但我看不到其他方法来设置 JComboBox 的内容。
A screenshot from NetBeans with my results in case the code is hard to understand.
下面是jButton的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
for(int a = 0; a < 4; a++){
String box2 = (currency2.getItemAt(a));
System.out.println("List items b4 conversion, JComboBox1: "+ currency1.getItemAt(a));
System.out.println("List items b4 conversion, JComboBox2: "+ box2+"\n");
System.out.println("--End of first 2 items--");
System.out.println("JComboBox2 after conv" +currency1.getItemAt(Integer.parseInt(box2)));
}
}catch (NumberFormatException e){
System.out.println("error");
}
}
打印的内容如下:
List items b4 conversion, JComboBox1: EUR
List items b4 conversion, JComboBox2: ALL
--End of first 2 items--
error
同时,当我删除: System.out.println("JComboBox2 after conv"+currency1.getItemAt(Integer.parseInt(box2)))
时,它会按预期打印 2 个 JComboBox 的所有项目.
最佳答案
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String txt1 = (String) JComboBox1.getSelectedItem();
String txt2 = (String) JComboBox2.getSelectedItem();
jComboBox1.setSelectedItem(txt2);
jComboBox2.setSelectedItem(txt1);
}
关于java - 如何与另一件 JComboBox 元素交换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60128572/