如何在文本框中显示计算的浮点值?

How to display the float value of a calculation in a text box?

我正在尝试显示 'Day time left' 和 'Night time left' 稍作计算后

我希望能够 select 组合框中的 3 个包中的任何一个 然后当我点击计算时它应该进行计算并在文本框中显示结果。

我尝试将浮点值转换为字符串,然后显示在文本框中,成功了!! 所以这意味着这个计算不起作用。

不知何故计算不起作用,所以它总是显示值 0.0

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        day_left = tot_day_time - day_used;
        night_data = tot_all_time - tot_day_time;
        night_left = night_data - night_used;
        jTextPane3.setText(Float.toString(day_left));jTextPane2.setText(Float.toString(night_left));
    } 

我总是得到 0.0

package slt_package;

public class slt_jframe extends javax.swing.JFrame {
    float tot_day_time;
    float tot_all_time;
    float night_used;
    float day_used;
    float day_left;
    float night_left;
    float night_data;
    
   
    private void package_comboboxActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        int selectedPackage = package_combobox.getSelectedIndex();
        
        switch (selectedPackage) {
            case 0:
                tot_day_time = 85;tot_all_time = 205;
                break;
            case 1:
                tot_day_time = 105;tot_all_time = 260;
                break;
            case 2:
                tot_day_time = 190;tot_all_time = 280;
                break;
            default:
                break;
        }
    }                                                

    private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        day_used=Float.parseFloat(textField1.getSelectedText());   // TODO add your handling code here:
    }                                          

    private void textField2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        night_used=Float.parseFloat(textField2.getSelectedText());
            // TODO add your handling code here:
    }                                          

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        day_left = tot_day_time - day_used;
        night_data = tot_all_time - tot_day_time;
        night_left = night_data - night_used;
        
        jTextPane3.setText(Float.toString(day_left));
        jTextPane2.setText(Float.toString(night_left));
        
    }                                     

}

正如 Andrew 所写,如果没有可用的程序,很难找到错误。我会 double-check Float.toString 方法,如 Float to String format specifier.

中所示