如何与另一个交换JComboBox项目?

问题描述 投票:0回答:1

我正在尝试制作货币转换器程序,并且希望从其他JComboBox when I hit the convert button.中替换JComboBox项目>

我知道错误是试图将String转换为整数,但是我看不到其他方法来设置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的所有项目。

我正在尝试制作货币转换器程序,并且当我按下转换按钮时,希望从其他JComboBox中替换JComboBox项目。我知道错误是尝试将String转换为...

java swing user-interface jbutton jcombobox
1个回答
0
投票
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String txt1 = (String) JComboBox1.getSelectedItem();
    String txt2 = (String) JComboBox2.getSelectedItem();
    jComboBox1.setSelectedItem(txt2);
    jComboBox2.setSelectedItem(txt1);
}
© www.soinside.com 2019 - 2024. All rights reserved.