|
| 1 | +### TemperatureConverterGUI |
| 2 | + |
| 3 | +#### Table of Contents |
| 4 | +1. [Execution at the command line](#execution-at-the-command-line) |
| 5 | +2. [`TemperatureConverter` Class](#temperatureconverter-class) |
| 6 | +3. [Requirements](#requirements) |
| 7 | + |
| 8 | +#### Execution at the command line |
| 9 | + |
1 | 10 | ```
|
2 | 11 | javac TemperatureConverter.java &&
|
3 | 12 | java TemperatureConverter
|
4 | 13 | ```
|
5 | 14 | 
|
6 | 15 | 
|
| 16 | + |
| 17 | +#### `TemperatureConverter` Class |
| 18 | + |
| 19 | +```java |
| 20 | +import java.awt.Color; |
| 21 | +import java.awt.event.ActionEvent; |
| 22 | +import java.awt.event.ActionListener; |
| 23 | +import java.awt.FlowLayout; |
| 24 | +import java.awt.Font; |
| 25 | +import java.awt.GridLayout; |
| 26 | +import javax.swing.ButtonGroup; |
| 27 | +import javax.swing.JButton; |
| 28 | +import javax.swing.JFrame; |
| 29 | +import javax.swing.JLabel; |
| 30 | +import javax.swing.JPanel; |
| 31 | +import javax.swing.JRadioButton; |
| 32 | +import javax.swing.JTextField; |
| 33 | +import javax.swing.SwingConstants; |
| 34 | + |
| 35 | +public class TemperatureConverter extends JFrame { |
| 36 | + private final ButtonGroup radioGroup; |
| 37 | + private final JButton button; |
| 38 | + private final JLabel label1, label2, label3, label4, label5; |
| 39 | + private final JPanel panel1, panel2, panel3, panel4; |
| 40 | + private final JRadioButton radioButton1, radioButton2; |
| 41 | + private final JTextField textField; |
| 42 | + |
| 43 | + public TemperatureConverter () { |
| 44 | + super("Temperature Converter"); |
| 45 | + setLayout(new GridLayout(5, 1, 0, 0)); |
| 46 | + |
| 47 | + label1 = new JLabel("Temperature Converter", SwingConstants.CENTER); |
| 48 | + label1.setForeground(Color.RED); |
| 49 | + label1.setFont(new Font(label1.getFont().getName(), Font.PLAIN, 18)); |
| 50 | + label2 = new JLabel("Choose Conversion Type:", SwingConstants.CENTER); |
| 51 | + label3 = new JLabel("Enter the temperature to convert:", SwingConstants.CENTER); |
| 52 | + label4 = new JLabel("The temperature converts to:", SwingConstants.CENTER); |
| 53 | + label5 = new JLabel("", SwingConstants.CENTER); |
| 54 | + |
| 55 | + radioGroup = new ButtonGroup(); |
| 56 | + radioButton1 = new JRadioButton("Fahrenheit to Celsius"); |
| 57 | + radioButton1.setHorizontalAlignment(SwingConstants.CENTER); |
| 58 | + radioButton2 = new JRadioButton("Celsius to Fahrenheit"); |
| 59 | + radioButton2.setHorizontalAlignment(SwingConstants.CENTER); |
| 60 | + radioGroup.add(radioButton1); |
| 61 | + radioGroup.add(radioButton2); |
| 62 | + |
| 63 | + textField = new JTextField("", 4); |
| 64 | + textField.setEditable(true); |
| 65 | + |
| 66 | + ButtonHandler handler = new ButtonHandler(); |
| 67 | + button = new JButton("Convert Temperature"); |
| 68 | + button.addActionListener(handler); |
| 69 | + |
| 70 | + panel1 = new JPanel(new GridLayout(3, 1, 0, 0)); |
| 71 | + panel1.add(label2); |
| 72 | + panel1.add(radioButton1); |
| 73 | + panel1.add(radioButton2); |
| 74 | + panel2 = new JPanel(); |
| 75 | + panel2.add(label3); |
| 76 | + panel2.add(textField); |
| 77 | + panel3 = new JPanel(); |
| 78 | + panel3.add(label4); |
| 79 | + panel3.add(label5); |
| 80 | + panel4 = new JPanel(new FlowLayout()); |
| 81 | + panel4.add(button); |
| 82 | + |
| 83 | + add(label1); |
| 84 | + add(panel1); |
| 85 | + add(panel2); |
| 86 | + add(panel3); |
| 87 | + add(panel4); |
| 88 | + } |
| 89 | + |
| 90 | + private class ButtonHandler implements ActionListener { |
| 91 | + @Override |
| 92 | + public void actionPerformed (ActionEvent e) { |
| 93 | + if (radioButton1.isSelected()) label5.setText(String.format("%.1f degrees Celsius", (5.0 / 9) * (Double.parseDouble(textField.getText()) - 32))); |
| 94 | + else if (radioButton2.isSelected()) label5.setText(String.format("%.1f degrees Fahrenheit", (9.0 / 5) * Double.parseDouble(textField.getText()) + 32)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public static void main (String[] args) { |
| 99 | + TemperatureConverter temperatureConverter = new TemperatureConverter(); |
| 100 | + temperatureConverter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 101 | + temperatureConverter.setSize(350, 300); |
| 102 | + temperatureConverter.setVisible(true); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +#### Requirements |
| 108 | + |
| 109 | +Write a GUI program, using the NetBeans GUI generator, that will convert temperatures from fahrenheit to celsius and from celsius to fahrenheit. The user should be able to enter a temperature to convert, click on a radio button to specify which conversion to do, and then click on a button to see the converted temperature displayed. |
| 110 | + |
| 111 | +Use the following formulas for the temperature conversions: |
| 112 | + |
| 113 | +`F = 9 / 5 * C + 32` |
| 114 | + |
| 115 | +and |
| 116 | + |
| 117 | +`C = 5 / 9 * (F - 32)` |
| 118 | + |
| 119 | +where F is the Fahrenheit temperature and C is the Celsius temperature. |
| 120 | + |
| 121 | +The temperatures should be displayed to 1 decimal place, e.g. 93.7 degrees Fahrenheit. |
0 commit comments