gpt4 book ai didi

Java GUI - 华氏度到摄氏度

转载 作者:行者123 更新时间:2023-11-30 08:59:30 25 4
gpt4 key购买 nike

现在,在您将其标记为重复之前 - 请听我说完。我已经看过其他 2 个类似的问题。

由于这是一个 GUI Java 程序,它因用户而异,在我的具体情况下,我只是尝试将华氏度转换为摄氏度,绝对没有其他功能。

这是我的代码:

package my.tempconvert;

public class TempConvertUI extends javax.swing.JFrame
{
public TempConvertUI()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
Farenheit = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
Celsius = new javax.swing.JTextField();
CalcButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Farenheit");

Farenheit.setText("Insert Farenheit Value");
Farenheit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FarenheitActionPerformed(evt);
}
});

jLabel2.setText("Celsius");

Celsius.setText("Celsius Will Appear Here");

CalcButton.setText("Calculate");
CalcButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CalcButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(Farenheit)
.addComponent(Celsius, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
.addGap(0, 183, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(CalcButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(Farenheit, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Celsius, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(CalcButton)
.addGap(0, 205, Short.MAX_VALUE))
);

pack();
}// </editor-fold>
private void FarenheitActionPerformed(java.awt.event.ActionEvent evt) {

}

private void CalcButtonActionPerformed(java.awt.event.ActionEvent evt) {
float num1, result;

num1 = Float.parseFloat(Farenheit.getText());

result = (((float) 5/9) * (num1 - 32));

Celsius.setText(String.valueOf(result));
}

public static void main(String args[])
{
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TempConvertUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TempConvertUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TempConvertUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TempConvertUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TempConvertUI().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton CalcButton;
private javax.swing.JTextField Celsius;
private javax.swing.JTextField Farenheit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}

没有语法错误——只有一个输出错误:

:摄氏度始终输出为 0.0 或 -0.0- 已解决 -

显然我的代码有问题,我需要帮助来弄清楚那是什么。

如果有人认为我的问题不清楚或者我没有遵循发布问题的指南,我很乐意考虑这些批评,我们都可以继续前进。

最佳答案

因为 (5/9) 的结果被评估为整数,所以它的值为零,因为它只取点左侧的值(例如 0.0557 -> 0 ),你应该像这样将它转换为 float :

num1 = Float.parseFloat(Farenheit.getText());

result = (((float) 5/9) * (num1 - 32));

Celsius.setText(String.valueOf(result));

关于Java GUI - 华氏度到摄氏度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27220089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com