【问题标题】:How to exchange JComboBox items with another one?如何与另一个交换 JComboBox 项目?
【发布时间】:2020-05-24 11:35:25
【问题描述】:

我正在尝试制作一个货币转换器程序,我希望将 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 的所有项目。

【问题讨论】:

    标签: java swing user-interface jbutton jcombobox


    【解决方案1】:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        String txt1 = (String) JComboBox1.getSelectedItem();
        String txt2 = (String) JComboBox2.getSelectedItem();
        jComboBox1.setSelectedItem(txt2);
        jComboBox2.setSelectedItem(txt1);
    }
    

    【讨论】:

    • 非常感谢你,我不敢相信它这么容易! (尴尬地笑了
    • 没问题,它发生了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多