无法将给定对象格式化为数字组合框

时间:2015-08-24 04:22:29

标签: java combobox

如何将整数设置为double?假设我有一个名为day的comboBox,它有1,2,3个元素。我想将它设置为两个小数点。如果用户选择1,它将变为1.00。我怎么能这样做?

public void actionPerformed(ActionEvent e){
                        String a=(String)comboBox.getSelectedItem();
                        //Integer b=(comboBox_1.getSelectedIndex()+1);
                        int day=(Integer)comboBox_2.getSelectedItem();
                        double bo;
                        DecimalFormat df = new DecimalFormat("#.##");      
                        bo= Double.valueOf(df.format(day));

我得错误

java.lang.IllegalArgumentException: Cannot format given Object as a Number
    at java.text.DecimalFormat.format(DecimalFormat.java:507)
    at java.text.Format.format(Format.java:157)
    at gui.User.<init>(User.java:105)
    at gui.User$1.run(User.java:49)

1 个答案:

答案 0 :(得分:1)

检查

public void actionPerformed(ActionEvent e){
                    String a=comboBox.getSelectedItem().toString();
                    //Integer b=(comboBox_1.getSelectedIndex()+1);
                    int day=Integer.ParseInt(comboBox_2.getSelectedItem().toString());
                    double bo;
                    DecimalFormat df = new DecimalFormat("#.##");      
                    bo=  Double.parseDouble(df.format(day));