|
| 1 | +import java.awt.event.*; |
| 2 | +import javax.swing.*; |
| 3 | +public class Convert |
| 4 | +{ |
| 5 | + Convert() |
| 6 | + { |
| 7 | + // Frame |
| 8 | + JFrame frame = new JFrame("Temperature Convertor"); // create reference of the frame |
| 9 | + frame.setSize(400, 400); // set size of the frame |
| 10 | + frame.setResizable(false); // size of the frame cannot be changed |
| 11 | + // Panel |
| 12 | + JPanel panel = new JPanel(); // create reference of the panel |
| 13 | + panel.setLayout(null); |
| 14 | + |
| 15 | + JLabel label1 = new JLabel("Enter Temperature: "); |
| 16 | + label1.setBounds(70, 30, 150, 50); |
| 17 | + |
| 18 | + JTextArea text = new JTextArea(""); |
| 19 | + text.setBounds(220, 30, 100, 50); |
| 20 | + |
| 21 | + JLabel label2 = new JLabel("Convert into: "); |
| 22 | + label2.setBounds(150, 130, 120, 30); |
| 23 | + |
| 24 | + JRadioButton radio1 = new JRadioButton("Farenheit",true); // choose Farenheit by default |
| 25 | + JRadioButton radio2 = new JRadioButton("Celsius"); |
| 26 | + radio1.setBounds(150, 160, 100, 20); |
| 27 | + radio2.setBounds(150, 180, 100, 20); |
| 28 | + |
| 29 | + ButtonGroup bg = new ButtonGroup(); // to enable choosing only one radio button |
| 30 | + |
| 31 | + JButton button = new JButton("CONVERT"); |
| 32 | + button.setBounds(150, 230, 100, 20); |
| 33 | + |
| 34 | + JLabel label3 = new JLabel(""); |
| 35 | + label3.setBounds(70, 280, 250,50); |
| 36 | + |
| 37 | + // the anonymous event |
| 38 | + button.addActionListener |
| 39 | + ( |
| 40 | + new ActionListener() |
| 41 | + { |
| 42 | + public void actionPerformed(ActionEvent e) |
| 43 | + { |
| 44 | + try |
| 45 | + { |
| 46 | + double temp = Double.parseDouble(text.getText()); |
| 47 | + double result; |
| 48 | + if(radio1.isSelected()) |
| 49 | + { |
| 50 | + result = 9.0/5*temp + 32; |
| 51 | + label3.setText("The temperature in Farenheit = "+result); |
| 52 | + } |
| 53 | + else if(radio2.isSelected()) |
| 54 | + { |
| 55 | + result = 5.0/9 * (temp - 32); |
| 56 | + label3.setText("The temperature in Farenheit = "+result); |
| 57 | + } |
| 58 | + } |
| 59 | + catch(NumberFormatException nfe) |
| 60 | + { |
| 61 | + JFrame newFrame = new JFrame(); |
| 62 | + JOptionPane.showMessageDialog(newFrame, "Please enter a number!"); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + |
| 69 | + // adding components to the button group |
| 70 | + bg.add(radio1); |
| 71 | + bg.add(radio2); |
| 72 | + |
| 73 | + // adding components to the panel |
| 74 | + panel.add(label1); |
| 75 | + panel.add(text); |
| 76 | + panel.add(label2); |
| 77 | + panel.add(radio1); |
| 78 | + panel.add(radio2); |
| 79 | + panel.add(button); |
| 80 | + panel.add(label3); |
| 81 | + |
| 82 | + // adding component(s) to the frame |
| 83 | + frame.add(panel); // add the panel to the frame |
| 84 | + |
| 85 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // to terminate the program on closing the frame |
| 86 | + frame.setVisible(true); // to make the frame visible |
| 87 | + } |
| 88 | +} |
0 commit comments