-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempcalculator.java
More file actions
62 lines (59 loc) · 2.24 KB
/
Copy pathTempcalculator.java
File metadata and controls
62 lines (59 loc) · 2.24 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
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Tempcalculator {
public static void main(String[] args) {
JFrame frame=new JFrame("Temperature Calculator");
frame.setSize(400,300);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Label
JLabel clabel=new JLabel("Celsius");
clabel.setBounds(30, 30, 100, 30);
JLabel flabel=new JLabel("Farenhite");
flabel.setBounds(30, 80, 100, 30);
//textfield
JTextField cfield=new JTextField();
cfield.setBounds(130, 30, 150, 30);
JTextField Ffield=new JTextField();
Ffield.setBounds(130, 80, 150, 30);
//buttons
JButton Convertor=new JButton("Conert");
Convertor.setBounds(30, 140, 100, 30);
JButton clear=new JButton("Clear");
clear.setBounds(140, 140, 100, 30);
//Event Hanlder
Convertor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try {
if(!cfield.getText().isEmpty()){
double value=Double.parseDouble(cfield.getText());
double f = (value * 9 / 5) + 32;
Ffield.setText(String.valueOf(f));
}
else if(!Ffield.getText().isEmpty()){
double f = Double.parseDouble(Ffield.getText());
double c = (f - 32) * 5 / 9;
cfield.setText(String.valueOf(c));
}
}
catch (Exception e1) {
JOptionPane.showMessageDialog(frame, "Invalid Input");
}
}
});
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cfield.setText("");
Ffield.setText("");
}
});
frame.add(clabel);
frame.add(flabel);
frame.add(cfield);
frame.add(Ffield);
frame.add(Convertor);
frame.add(clear);
frame.setVisible(true);
}
}