带有按钮的Java GUI Fahrenheit / Celsius转换器

时间:2014-02-04 07:48:26

标签: java user-interface temperature

我是Java GUI的初学者,对于课程,我需要制作一个将华氏温度转换为摄氏温度的程序,反之亦然。我想制作一个使用按钮的GUI程序,但无法在网上找到任何可以帮助我的帖子。我在按钮实际正常工作时遇到问题。它从华氏温度转换为摄氏温度很好,但不会将摄氏温度转换为华氏温度。我知道逻辑上有一个明显的错误,但我不能为我的生活弄明白。任何帮助将不胜感激。

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;

    @SuppressWarnings("serial")
    public class Temperature_Converter extends JFrame{
    private static final double CelsiusTOFarenheit = 9.0 / 5.0;
    private static final double FahrenheitTOCelsius = 5.0 / 9.0;
    private static final int offset = 32;
    private JLabel LFahrenheit, LCelsius; //Labels
    private JTextField TFFahrenheit, TFCelsius; //Text Fields
    private JButton BConvert; //Convert Button
    private ConvertButtonHandler ConvButtonHandler;

    public Temperature_Converter() {
    setTitle("Temperature Converter");
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(1,5));

    LFahrenheit = new JLabel("Farenheit:", JLabel.CENTER);
        pane.add(LFahrenheit);
    TFFahrenheit = new JTextField();
        pane.add(TFFahrenheit);
    BConvert = new JButton("Convert");
        pane.add(BConvert);
            ConvButtonHandler = new ConvertButtonHandler();
            BConvert.addActionListener(ConvButtonHandler);
    LCelsius = new JLabel("Celsius:", JLabel.CENTER);
        pane.add(LCelsius);
    TFCelsius = new JTextField();
        pane.add(TFCelsius);

    setSize(600, 85);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}
 private class ConvertButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            double celsius = 0, fahrenheit = 0;

            DecimalFormat twoDigits = new DecimalFormat("0.00");     
                    celsius = Double.parseDouble(TFCelsius.getText());
                    fahrenheit = Double.parseDouble(TFFahrenheit.getText());
                        celsius = (fahrenheit - offset) * FahrenheitTOCelsius;
                        fahrenheit = celsius * CelsiusTOFarenheit + offset;
                            TFCelsius.setText(" "+ twoDigits.format(celsius));
                            TFFahrenheit.setText(" "+ twoDigits.format(fahrenheit));       
    }
 }

public static void main(String[] args) {
    new Temperature_Converter();
}

}

3 个答案:

答案 0 :(得分:2)

以下是可能对您有所帮助:将要填充的文本字段留空(如果要将摄氏温度转换为华氏温度,请留空华氏度)。

 @Override
public void actionPerformed(final ActionEvent e) {
    double celsius = 0, fahrenheit = 0;
    DecimalFormat twoDigits = new DecimalFormat("0.00");
    if (Temperature_Converter.this.TFCelsius.getText() == null || "".equals(Temperature_Converter.this.TFCelsius.getText().trim())) {
        // convert from fahrenheit to celsius
        fahrenheit = Double.parseDouble(Temperature_Converter.this.TFFahrenheit.getText());
        celsius = (fahrenheit - Temperature_Converter.offset) *   Temperature_Converter.FahrenheitTOCelsius;
        Temperature_Converter.this.TFCelsius.setText(" " + twoDigits.format(celsius));
    } else if (Temperature_Converter.this.TFFahrenheit.getText() == null || "".equals(Temperature_Converter.this.TFFahrenheit.getText().trim())) {
           // convert from celsius to fahrenheit
           celsius = Double.parseDouble(Temperature_Converter.this.TFCelsius.getText());
           fahrenheit = celsius * Temperature_Converter.CelsiusTOFarenheit + Temperature_Converter.offset;
           Temperature_Converter.this.TFFahrenheit.setText(" " + twoDigits.format(fahrenheit));
    }
}

编辑:您可以验证用户输入文本并提醒用户是否有其他内容,然后将数字作为温度引入。

try {
    fahrenheit = Double.parseDouble(Temperature_Converter.this.TFFahrenheit.getText());
} catch (NumberFormatException e1) {
     //alert the user       
    JOptionPane.showMessageDialog(null, "You are allowed to introduce numbers only for temperature");
    return;
}

答案 1 :(得分:1)

假设我必须在华氏度字段中放置一个值而在摄氏度字段中放置一个值并且它们应该同时计算,则必须将计算值“缓存”在除了您获得的变量之外的其他变量中来自textfields。

执行此操作时:

celsius = Double.parseDouble(TFCelsius.getText());
fahrenheit = Double.parseDouble(TFFahrenheit.getText());
celsius = (fahrenheit - offset) * FahrenheitTOCelsius;
fahrenheit = celsius * CelsiusTOFarenheit + offset;

第一次计算的结果(新的Celsius值)将在第二次计算中使用,导致与华氏温度字段中最初的值相同的结果(因为它只是将值转换为摄氏度而不是返回再次)

您的问题的解决方案是在新变量中保护您的结果,例如:

celsius = Double.parseDouble(TFCelsius.getText());
fahrenheit = Double.parseDouble(TFFahrenheit.getText());
double celsiusNew = (fahrenheit - offset) * FahrenheitTOCelsius;
double fahrenheitNew = celsius * CelsiusTOFarenheit + offset;
TFCelsius.setText(" "+ twoDigits.format(celsiusNew));
TFFahrenheit.setText(" "+ twoDigits.format(fahrenheitNew));

这样就可以了。

编辑:阅读其他两个答案,以使您的程序更加用户友好和防弹。

答案 2 :(得分:0)

我认为你的文字字段问题,如果你没有在两个文本字段中输入数字,你将得到“空字符串”问题,它无法将其转换为Double,所以你需要检查你的字符串不相等要清空

所以,你的问题在于这两行:

 celsius = Double.parseDouble(TFCelsius.getText());
 fahrenheit = Double.parseDouble(TFFahrenheit.getText());

要解决它,你可以这样做,例如:

if (!TFCelsius.getText().equals("")) {
     celsius = Double.parseDouble(TFCelsius.getText());
 } else {
     ////i don't know what the value but make it equal 0
 }
相关问题