简体   繁体   English

JFrame中的长度转换无法正常工作

[英]Length conversion in a JFrame not working properly

I am trying to have a length conversion for the following measurements: feet, inch, yard, millimeter, and meter. 我正在尝试进行以下尺寸的长度转换:英尺,英寸,码,毫米和米。 But for some reason I can only convert if I have something in my foot JTextField . 但是由于某种原因,我只能在脚下有JTextField If I try to do another I get an error: 如果我尝试执行其他操作,则会收到错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at SpecsProgram$LengthBttnHandler.actionPerformed(SpecsProgram.java:711)
        ...

I'm guessing my logic is wrong for the whole thing, I have tried a few things but none of them worked. 我猜我的逻辑对于整个事情都是错误的,我尝试了一些事情,但没有一个起作用。 So, what do I need to do to fix this? 那么,我该怎么做才能解决此问题?

    class LengthBttnHandler implements ActionListener {
       public void actionPerformed(ActionEvent e) {
    String ft, in, yd, mm, mt;
           double foot, inch, yard, mill, met;
           ft = footEText.getText();
           if (ft!=null)
           {
               foot = Double.parseDouble(ft);
               yard = foot/3;
               yd = yard+"";
               inch = foot*12;
               in = inch+"";
               met = foot*.3048;
               mt = met+"";
               mill = (foot*12)*25.4;
               mm = mill+"";

               yardEText.setText(yd);
               inchEText.setText(in);
               metEText.setText(mt);
               millEText.setText(mm);
           }

           in = inchEText.getText();
           if (in!=null)
           {
               //Look up how to truncate
               inch = Double.parseDouble(in);
               foot = inch/12;
               ft = foot+"";
               yard = inch/36;
               yd = yard+"";
               met = inch*0.0254;
               mt = met+"";
               mill = inch*25.4;
               mm = mill+"";

               footEText.setText(ft);
               yardEText.setText(yd);
               metEText.setText(mt);
               millEText.setText(mm);
           }

           yd = yardEText.getText();
           if (yd!=null)
           {
               yard = Double.parseDouble(yd);
               foot = yard*3;
               ft = foot+"";
               inch = yard*36;
               in = inch+"";
               met = yard*.9144;
               mt = met+"";
               mill = yard*(36*25.4);
               mm = mill+"";

               footEText.setText(ft);
               inchEText.setText(in);
               metEText.setText(mt);
               millEText.setText(mm);
           }

           mm = millEText.getText();
           if (mm!=null)
           {
               mill = Double.parseDouble(mm);
               foot = mill*.00328084;
               ft = foot+"";
               inch = mill*.0393701;
               in = inch+"";
               yard = mill*.00109361;
               yd = yard+"";
               met = mill*.001;
               mt = met+"";

               footEText.setText(ft);
               inchEText.setText(in);
               metEText.setText(mt);
               yardEText.setText(yd);
           }

           mt = metEText.getText();
           if (mt!=null)
           {
               met = Double.parseDouble(mt);
               foot = met*3.28084;
               ft = foot+"";
               yard = met*1.09361;
               yd = yard+"";
               inch = met*39.3701;
               in = inch+"";
               mill = met*1000;
               mm = mill+"";

               footEText.setText(ft);
               yardEText.setText(yd);
               inchEText.setText(in);
               millEText.setText(mm);
           }
       }
   }

Double.parseDouble(ft)调用FloatingDecimal.readJavaFormatString(ft).doubleValue() ,其中显然不适用于空字符串,只需编写ft.isEmpty()的检查并提供敏感的默认输出即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM