import java.awt.Container; import java.awt.Font; import java.awt.GridLayout; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.border.Border; /** * A simple application to convert Celsius degrees into Fahrenheit and back. * This is an adapted version from a similar example by Robi Malik * which itself was an adapted version from a similar example in the * Swing Tutorial. */ public class CelsiusConverter extends JFrame { /** * Creates a Celsius Converter window and initialises all its * components. */ CelsiusConverter() { super("Convert Celsius to Fahrenheit and back"); Container content = getContentPane(); LayoutManager layout = new GridLayout(3, 2); content.setLayout(layout); _celsiusLabel = new JLabel("Celsius"); _fahrenheitLabel = new JLabel("Fahrenheit"); _inputC = new JTextField(10); _inputF = new JTextField(10); _convertC2FButton = new JButton("C->F"); _convertF2CButton = new JButton("F->C"); Font font = new Font("SansSerif", 0, 20); content.setFont(font); _inputC.setFont(null); _inputF.setFont(null); _celsiusLabel.setFont(null); _fahrenheitLabel.setFont(null); _convertC2FButton.setFont(null); _convertF2CButton.setFont(null); Border borderCelsius = BorderFactory.createEmptyBorder(2, 5, 1, 0); _celsiusLabel.setBorder(borderCelsius); Border borderFahrenheit = BorderFactory.createEmptyBorder(1, 5, 2, 0); _fahrenheitLabel.setBorder(borderFahrenheit); ActionListener celsiusListener = new ConvertCelsiusListener(); _convertC2FButton.addActionListener(celsiusListener); _inputC.addActionListener(celsiusListener); ActionListener fahrenheitListener = new ConvertFahrenheitListener(); _convertF2CButton.addActionListener(fahrenheitListener); _inputF.addActionListener(fahrenheitListener); content.add(_celsiusLabel); content.add(_fahrenheitLabel); content.add(_inputC); content.add(_inputF); content.add(_convertC2FButton); content.add(_convertF2CButton); } /** * A main method to show and run the Celsius Converter. */ public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { CelsiusConverter gui = new CelsiusConverter(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.pack(); gui.setVisible(true); } } ); } /** * An action listener for the Convert button. * This action listener is associated both with the Convert * button and with the converter's text field, so the Fahrenheit display * gets updated whenever the button is pressed, or the user hits * <Enter> on the text field. */ private class ConvertCelsiusListener implements ActionListener { /** * Called when the Convert button is pressed. * This method reads the contents of the text field, converts * the number from Celsius degrees to Fahrenheit, and displays * the result in the Fahrenheit label. */ public void actionPerformed(ActionEvent event) { String celsiusText = _inputC.getText(); double celsius = Double.parseDouble(celsiusText); double fahrenheit = celsius * 9 / 5 + 32; String fahrenheitText = String.valueOf(fahrenheit); _inputF.setText(fahrenheitText); } } private class ConvertFahrenheitListener implements ActionListener { /** * Called when the Convert button is pressed. * This method reads the contents of the text field, converts * the number from Celsius degrees to Fahrenheit, and displays * the result in the Fahrenheit label. */ public void actionPerformed(ActionEvent event) { String fahrenheitText = _inputF.getText(); double fahrenheit = Double.parseDouble(fahrenheitText); double celsius = (fahrenheit-32) * 5 / 9; String celsiusText = String.valueOf(celsius); _inputC.setText(celsiusText); } } /** * The text field for entering the number of Celsius degrees to * be converted. */ private JTextField _inputC; private JTextField _inputF; /** * The label for the text field. * It always shows the text "Celsius". */ private JLabel _celsiusLabel; /** * The Convert button. * When clicked, the Celsius value is read from the text field and * converted. */ private JButton _convertC2FButton; private JButton _convertF2CButton; /** * The label that displays the number of Fahrenheit degrees that * corresponds to the number of Celsius degrees in the text field. */ private JLabel _fahrenheitLabel; }