- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.java
More file actions
Latest commit
88 lines (75 loc) · 2.43 KB
/
Copy pathConvert.java
File metadata and controls
88 lines (75 loc) · 2.43 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
85
86
87
88
importjava.awt.event.*;
importjavax.swing.*;
publicclassConvert
{
Convert()
{
// Frame
JFrameframe = newJFrame("Temperature Convertor"); // create reference of the frame
frame.setSize(400, 400); // set size of the frame
frame.setResizable(false); // size of the frame cannot be changed
// Panel
JPanelpanel = newJPanel(); // create reference of the panel
panel.setLayout(null);
JLabellabel1 = newJLabel("Enter Temperature: ");
label1.setBounds(70, 30, 150, 50);
JTextAreatext = newJTextArea("");
text.setBounds(220, 30, 100, 50);
JLabellabel2 = newJLabel("Convert into: ");
label2.setBounds(150, 130, 120, 30);
JRadioButtonradio1 = newJRadioButton("Farenheit",true); // choose Farenheit by default
JRadioButtonradio2 = newJRadioButton("Celsius");
radio1.setBounds(150, 160, 100, 20);
radio2.setBounds(150, 180, 100, 20);
ButtonGroupbg = newButtonGroup(); // to enable choosing only one radio button
JButtonbutton = newJButton("CONVERT");
button.setBounds(150, 230, 100, 20);
JLabellabel3 = newJLabel("");
label3.setBounds(70, 280, 250,50);
// the anonymous event
button.addActionListener
(
newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
try
{
doubletemp = Double.parseDouble(text.getText());
doubleresult;
if(radio1.isSelected())
{
result = 9.0/5*temp + 32;
label3.setText("The temperature in Farenheit = "+result);
}
elseif(radio2.isSelected())
{
result = 5.0/9 * (temp - 32);
label3.setText("The temperature in Farenheit = "+result);
}
}
catch(NumberFormatExceptionnfe)
{
JFramenewFrame = newJFrame();
JOptionPane.showMessageDialog(newFrame, "Please enter a number!");
}
}
}
);
// adding components to the button group
bg.add(radio1);
bg.add(radio2);
// adding components to the panel
panel.add(label1);
panel.add(text);
panel.add(label2);
panel.add(radio1);
panel.add(radio2);
panel.add(button);
panel.add(label3);
// adding component(s) to the frame
frame.add(panel); // add the panel to the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // to terminate the program on closing the frame
frame.setVisible(true); // to make the frame visible
}
}