-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCelsiusToFahrenheit.java
More file actions
96 lines (83 loc) · 3 KB
/
CelsiusToFahrenheit.java
File metadata and controls
96 lines (83 loc) · 3 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
89
90
91
92
93
94
95
96
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class CelsiusToFahrenheit extends JFrame implements ActionListener{
private JLabel lCelsjusz, lFahrenheit;
private JTextField tCelsjusz, tFahrenheit;
private JButton bKonwertuj;
private JRadioButton rCtoF, rFtoC;
private ButtonGroup radioPanel;
private double tempCelsjusz, tempFahrenheit;
public CelsiusToFahrenheit(){
setSize(700, 500);
setTitle("Przeliczanie stopni Celsjusza na Fahrenheita");
setLayout(null);
lCelsjusz = new JLabel("Stopnie Celsjusza: ");
lCelsjusz.setBounds(30, 30, 150, 150);
add(lCelsjusz);
tCelsjusz = new JTextField("");
tCelsjusz.setBounds(150, 100, 150, 20);
add(tCelsjusz);
tCelsjusz.addActionListener(this);
lFahrenheit = new JLabel("Stopnie Fahrenheit: ");
lFahrenheit.setBounds(30, 60, 300, 300);
add(lFahrenheit);
tFahrenheit = new JTextField("");
tFahrenheit.setBounds(150, 200, 150, 20);
add(tFahrenheit);
tFahrenheit.addActionListener(this);
bKonwertuj = new JButton("Oblicz");
bKonwertuj.setBounds(190, 250, 70, 30);
add(bKonwertuj);
bKonwertuj.addActionListener(this);
radioPanel = new ButtonGroup();
rCtoF = new JRadioButton("Celcius to Fahrenheit");
rCtoF.setBounds(250, 290, 150, 100);
rCtoF.setSelected(true);
radioPanel.add(rCtoF);
add(rCtoF);
rFtoC = new JRadioButton("Fahrenheit to Celcius");
rFtoC.setBounds(250, 350, 150, 100);
radioPanel.add(rFtoC);
add(rFtoC);
}
public static void main(String[] args){
CelsiusToFahrenheit c = new CelsiusToFahrenheit();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o==bKonwertuj){
if(rCtoF.isSelected()){
tempCelsjusz = Double.parseDouble(tCelsjusz.getText()); //stopnie celcjusza ze stringa konwertujemy na liczbe double zmiennoprzecinkowa
tempFahrenheit = 32.0 + (9.0/5.0)*tempCelsjusz;
tFahrenheit.setText(String.valueOf(tempFahrenheit));
}
else if(rFtoC.isSelected()){
tempFahrenheit = Double.parseDouble(tFahrenheit.getText());
tempCelsjusz = (tempFahrenheit - 32.0) * (5.0/9.0);
tCelsjusz.setText(String.valueOf(tempCelsjusz));
}
}
if (o==tCelsjusz){
tempCelsjusz = Double.parseDouble(tCelsjusz.getText()); //stopnie celcjusza ze stringa konwertujemy na liczbe double zmiennoprzecinkowa
tempFahrenheit = 32.0 + (9.0/5.0)*tempCelsjusz;
tFahrenheit.setText(String.valueOf(tempFahrenheit));
}
else if(o==tFahrenheit){
tempFahrenheit = Double.parseDouble(tFahrenheit.getText());
tempCelsjusz = (tempFahrenheit - 32.0) * (5.0/9.0);
tCelsjusz.setText(String.valueOf(tempCelsjusz));
}
}
}