-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext3.java
More file actions
84 lines (83 loc) · 2.26 KB
/
text3.java
File metadata and controls
84 lines (83 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
public class text3 extends JFrame implements ActionListener {
JButton button1 = new JButton("华氏温度");
JButton button2 = new JButton("摄氏温度");
JButton button3 = new JButton("K氏温度");
JTextField text1 = new JTextField(10);
JTextField text2 = new JTextField(10);
JTextField text3 = new JTextField(10);
float c,f,k;
public text3()
{
super("华氏摄氏K氏温度转换");
try
{
Init();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void Init()
{
button1.setBounds(30,30,70,25);
button2.setBounds(30,110,70,25);
button3.setBounds(30,190,70,25);
text1.setBounds(150,30,100,25);
text2.setBounds(150,110,100,25);
text3.setBounds(150,190,100,25);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
Container con = getContentPane();
con.setLayout(null);
con.add(text1);
con.add(text2);
con.add(text3);
con.add(button1);
con.add(button2);
con.add(button3);
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args)
{
new text3();
}
public void actionPerformed(ActionEvent e) {
//华氏温度f,摄氏度c,K氏温度k
//c=(f-32)*5/9
//k=c+273
if (e.getSource() == button2) {
try
{
//华氏转摄氏
f=Float.parseFloat(text1.getText());
c=(f-32)*5/9;
text2.setText(String.valueOf(c));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
if (e.getSource() == button3) {
try
{
//摄氏转K氏
f=Float.parseFloat(text2.getText());
k=f+273;
text3.setText(String.valueOf(k));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}