如何与另一个交换 JComboBox 项目?
How to exchange JComboBox items with another one?
我正在尝试制作一个货币转换器程序,并且我希望 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 个 JComboBoxes 的所有项目。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String txt1 = (String) JComboBox1.getSelectedItem();
String txt2 = (String) JComboBox2.getSelectedItem();
jComboBox1.setSelectedItem(txt2);
jComboBox2.setSelectedItem(txt1);
}
我正在尝试制作一个货币转换器程序,并且我希望 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 个 JComboBoxes 的所有项目。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String txt1 = (String) JComboBox1.getSelectedItem();
String txt2 = (String) JComboBox2.getSelectedItem();
jComboBox1.setSelectedItem(txt2);
jComboBox2.setSelectedItem(txt1);
}