Java 如何将JComboBox项与其他项交换?
我正在尝试制作一个货币转换器程序,我希望JComboBox项目从另一个JComboBox中被替换 我知道错误在于尝试将字符串转换为整数,但我看不到其他设置JComboBox内容的方法 以下是jButton的代码:Java 如何将JComboBox项与其他项交换?,java,swing,user-interface,jbutton,jcombobox,Java,Swing,User Interface,Jbutton,Jcombobox,我正在尝试制作一个货币转换器程序,我希望JComboBox项目从另一个JComboBox中被替换 我知道错误在于尝试将字符串转换为整数,但我看不到其他设置JComboBox内容的方法 以下是jButton的代码: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handl
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");
}
}
同时,当我删除:
System.out.println(“conv后的jcombox2”+currency1.getItemAt(Integer.parseInt(box2))
时,它会按预期打印两个jcombox的所有项目。非常感谢,我真不敢相信这会这么容易!(尴尬地笑)没问题,是这样的:)
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String txt1 = (String) JComboBox1.getSelectedItem();
String txt2 = (String) JComboBox2.getSelectedItem();
jComboBox1.setSelectedItem(txt2);
jComboBox2.setSelectedItem(txt1);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String txt1 = (String) JComboBox1.getSelectedItem();
String txt2 = (String) JComboBox2.getSelectedItem();
jComboBox1.setSelectedItem(txt2);
jComboBox2.setSelectedItem(txt1);
}