import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Mytest extends JFrame {
JLabel l1, l2;
JTextField t1, t2;
JButton b;
public Mytest() {
l1 = new JLabel("华氏温度");
t1 = new JTextField(5);
l2 = new JLabel("摄氏温度");
t2 = new JTextField(5);
b = new JButton("转换");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
int hua = Integer.parseInt(t1.getText());
t2.setText("" + (hua - 32) * 5 / 9);
} catch (NumberFormatException e) {
}
}
});
add(l1);
add(t1);
add(b);
add(l2);
add(t2);
setTitle("温度转换");
setLayout(new FlowLayout());
setVisible(true);
setSize(400, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Mytest();
}
}