需要帮助通过此程序捕获异常

时间:2010-10-09 19:31:19

标签: java exception

一直试图改变我的代码来捕捉异常,但无法弄清楚如何使用这个。我不时会遇到JOptionPane的双重实例化,这取决于我把它放在哪里。

import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class JCalculateWeight extends JFrame {

    Font font = new Font("Arial", Font.BOLD, 14);
    JFrame frame;
    String name = JOptionPane.showInputDialog(frame, "What's your name?");
    String weight = JOptionPane.showInputDialog(frame, "What's your weight (lbs)");

    @Override
    public void paint(Graphics brush) {
        super.paint(brush);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        double lbs = Double.parseDouble(weight);
        double oz = lbs * 16;
        double kilo = lbs / 2.204623;
        double metricton = lbs / 2204.623;
        brush.setFont(font);
        brush.drawString(name, 55, 50);
        brush.drawString(String.valueOf(lbs), 55, 150);
        brush.drawString(String.valueOf(oz), 55, 170);
        brush.drawString(String.valueOf(kilo), 55, 190);
        brush.drawString(String.valueOf(metricton), 55, 210);
    }

    public static void main(String[] args) {
        JCalculateWeight frame = new JCalculateWeight();
        frame.setSize(500, 450);
        frame.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:0)

当最终用户输入无效NumberFormatException时,您正在谈论weight,不是吗?并且您希望在用户输入无效时重新显示对话框?

其中一种方法是在循环中执行此操作,只要权重为零即可。在main()创建后,在frame方法内执行此操作。以下是更改。

// ...
String name;
double weight;

@Override
public void paint(Graphics brush) {
    // ...
    double lbs = weight; // "weight" can also just be renamed to "lbs", then you can remove this line.
    // ...
}

public static void main(String[] args) {
    JCalculateWeight frame = new JCalculateWeight();
    frame.name = JOptionPane.showInputDialog(frame, "What's your name?");
    while (frame.weight == 0) {
        try {
            frame.weight = Double.parseDouble(JOptionPane.showInputDialog(frame, "What's your weight (lbs)"));
        } catch (NumberFormatException e) {
            // Maybe log it? Maybe change the question message?
        }
    }
    // ...
}

答案 1 :(得分:0)

我认为你是一个很新的人。

我不知道你在说什么异常,但我应该这样做:

public WeightCalculatorFrame extends JComponent
{
    private String name;
    private double lbs;

    public WeightCalculatorFrame()
    {
        name = JOptionPane.showInputDialog("What is your name?");
        try
        {
            lbs = Double.parseDouble(JOptionPane.showInputDialog("What is your weight in lbs"));
        } catch (NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Invalid decimal format!\nWill now exit");
            System.exit(-1);

        JFrame frame = new JFrame("Weight Calculator");
        frame.setPreferredSize(new Dimension(300, 400));
        frame.add(this);
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        g.setFont(yourFont);
        g.drawString("Your Name: " + name, x, y);
        double kilo = lbs / ....;
        double ... = lbs / ....;
        // Here all your calculations...
        g.drawString("LBS: " + lbs, x, y);
        g.drawString("Kilo: " + kilo, x, y);
    }
}

用它初始化它:

new WeightCalculatorFrame();