如何在文本框中显示计算的浮点数?
我正在尝试显示“剩下的白天”和“剩下的夜晚” 进行小计算后,
我希望能够从组合框中选择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));
}
}
I'm trying to display the 'Day time left' and 'Night time left'
after doing a small calculation
I want to be able to select any of the 3 packages from the combo box
then when I click calculate it should do the calculation and display the result in the text boxes.
I tried converting a float value to string and then display in the text box, It worked !!
so that means this calculation doesen't work.
Somehow the calculation doesn't work so it always displays the value as 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));
}
I always get 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));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如安德鲁(Andrew)所写的那样,如果没有工作程序,很难找到错误。我会仔细检查
float.toString
方法,如。As Andrew wrote, it's difficult to find the bug without a working program. I would double-check the
Float.toString
method as seen in Float to String format specifier.