我正在尝试在 JFrame 中运行简单的温度转换。然而,无论华氏温度如何,华氏温度到 celuis(整数)的温度转换返回为零。代码如下,后面是主类:
/*
* ch 11 GUI programming questions
* Q 11.12
*/
package Chapter11GUI;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.ActiveEvent;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class TempConverter extends JFrame {
//declare instance varibles
JLabel label1;
JLabel label2;
JTextField faherinheit;
//construto constrots GUI and habldes events
public TempConverter() {
super("Temperature Converter");
setLayout(new BorderLayout());
label1 = new JLabel("Eneter ferinheit to convert to celuis: ");
//get usre to enter temp i fioaherinet and trigger event andler to
//assign string to a integer value
//use annomonous class
faherinheit = new JTextField(10);
faherinheit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//get numbers enetered
int temp = Integer.parseInt(faherinheit.getText());
int celuis = (int) ((5 / 9) * (temp - 32));
label2.setText("Farinheit Temp " + temp + " in celuis is: " + celuis);
}//end actionEvent Listener annmonous classs
});//and eveny handling for textfiel
//add all compnments to JFrame using birder Layout
label2 = new JLabel("Temperature in Celuis is : ");
add(label1, BorderLayout.NORTH);
add(faherinheit, BorderLayout.CENTER);
add(label2, BorderLayout.SOUTH);
}//end constructor
}//end class TempCOnversion