我无法让我的try catch在java中工作

时间:2016-01-27 19:35:58

标签: java swing

所以这是我的GUI,你可以看到。

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Frame1 {

    private JFrame frame;
    private JTextField textFieldnum1;
    private JTextField textFieldnum2;
    private JTextField textFieldAns;
    private Termostat thermo;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Frame1 window = new Frame1();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Frame1() {
        initialize();
    }

    private void initialize() {
        thermo = new Termostat();

        frame = new JFrame();
        frame.setBounds(100, 100, 696, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        textFieldnum1 = new JTextField();
        textFieldnum1.setBounds(176, 11, 147, 46);
        frame.getContentPane().add(textFieldnum1);
        textFieldnum1.setColumns(10);

        textFieldnum2 = new JTextField();
        textFieldnum2.setBounds(176, 154, 147, 46);
        frame.getContentPane().add(textFieldnum2);
        textFieldnum2.setColumns(10);

        JButton btnNewButton = new JButton("Convert to celcius");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                double myf = thermo.convertToCelcius(Double.parseDouble(textFieldnum1.getText()));
                textFieldAns.setText(String.valueOf(myf));
            }
        });
        btnNewButton.setBounds(0, 0, 171, 68);
        frame.getContentPane().add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Convert to fahrenheit");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                double myf = thermo.convertToFahrenheit(Double.parseDouble(textFieldnum2.getText()));
                textFieldAns.setText(String.valueOf(myf));
            }
        });
        btnNewButton_1.setBounds(0, 143, 171, 68);
        frame.getContentPane().add(btnNewButton_1);

        textFieldAns = new JTextField();
        textFieldAns.setBounds(354, 90, 147, 46);
        frame.getContentPane().add(textFieldAns);
        textFieldAns.setColumns(10);

        JLabel lblNewLabel = new JLabel("Converted");
        lblNewLabel.setBounds(285, 94, 112, 38);
        frame.getContentPane().add(lblNewLabel);
    }
}

这就是我的问题。当我上课的时候,我想把输入输入计算celcius到华氏度,当我输入除数字以外的任何东西时,我不希望它崩溃。但我不能让它工作,所以我需要你们帮助让尝试捕获工作。 我非常感谢所有的帮助。

import javax.swing.JOptionPane;

public class Termostat {
    public double convertToCelcius(double input) {
        double far = 0;
        try {
            far = (input - 32) * 5 / 9;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Wrong input");
            return far;
        }
        return far;
    }

    public double convertToFahrenheit(double input) {
        double cel = 1;
        try {
            cel = (input * 9 / 5) + 32;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Wrong input");
            return cel;
        }
        return cel;

    }
}

3 个答案:

答案 0 :(得分:5)

这可能是您获得例外情况的行:

     double myf = thermo.convertToFahrenheit(Double.parseDouble(textFieldnum2.getText()));

所以用NumberFormatException(parseDouble)保护它。

答案 1 :(得分:1)

您可以使用NumberUtil.isNumber(str)检查输入是否为数字。 Here is more information

答案 2 :(得分:0)

如果要使用此方法将文本字段文本解析为Double,则必须使用try catch Double.parseDouble(text)

将它们写为这样,并删除convertToFahrenheitconvertToCelcius方法中的尝试捕获

    JButton btnNewButton_1 = new JButton("Convert to fahrenheit");
    btnNewButton_1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            try { // start trying to parse input string  to double
                double myf = thermo.convertToFahrenheit(Double.parseDouble(textFieldnum2.getText()));
                textFieldAns.setText(String.valueOf(myf));
            } catch (NumberFormatException ex) { // catch the exception that you mentioned
                JOptionPane.showMessageDialog(null, "Wrong input");
                textFieldAns.setText("");
            }
        }
    });