/* This is the conversion for temperature * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import java.text.NumberFormat; public class ConvertTemperature extends JFrame { private JPanel mainpanel, txtpanel, buttonpanel, labelpanel; private JButton convert, exit, back, clear; private JTextField txtfield; private JLabel title; private JComboBox jcombo; private double number; private double fDegree = 0.0, cDegree = 0.0; private boolean value = true; public ConvertTemperature() { // Sets the title of the window setTitle("Convert Temperature"); // Sets the size of the window setSize(400, 400); // Sets the start location of the window setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Creates a main panel & set the layout to border layout mainpanel = new JPanel(); mainpanel.setLayout(new BorderLayout()); // Creates a text panel txtpanel = new JPanel(); // Create a label title = new JLabel("Convert From"); // Creates a text field txtfield = new JTextField("", 20); // Create a JCombo Box jcombo = new JComboBox(); // Add items to the JCombo Box jcombo.addItem("°F"); jcombo.addItem("°C"); // Add the text area, JCombo & the label to the text panel txtpanel.add(title); txtpanel.add(txtfield); txtpanel.add(jcombo); // Add the text panel to the main panel mainpanel.add(txtpanel, BorderLayout.NORTH); // Create the buttons convert = new JButton("Convert"); exit = new JButton("Exit"); back = new JButton("Back"); clear = new JButton("Clear"); // Add the buttons to the panel buttonpanel = new JPanel(); buttonpanel.add(convert); buttonpanel.add(exit); buttonpanel.add(clear); buttonpanel.add(back); // Create the action listener for the buttons convert.addActionListener(new ButtonListener()); exit.addActionListener(new ButtonListener()); back.addActionListener(new ButtonListener()); clear.addActionListener(new ButtonListener()); // Add the button panel to the main panel mainpanel.add(buttonpanel, BorderLayout.SOUTH); // Add the main panel to the content pane add(mainpanel); setVisible(value); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { /* All the calculations are done in here * */ try { NumberFormat formatter = new DecimalFormat("#0.000"); NumberFormat formatter2 = new DecimalFormat("#0.000000"); if(e.getSource() == convert) { String select = (String)jcombo.getSelectedItem(); if(select.equals("°F")) { number = Double.parseDouble(txtfield.getText()); cDegree =(5.0/9.0)*(number-32); JOptionPane.showMessageDialog(null, formatter.format(cDegree) + " °C\n"); number = 0.0; } if(select.equals("°C")) { number = Double.parseDouble(txtfield.getText()); fDegree = (9.0/5.0)*number+32; JOptionPane.showMessageDialog(null, formatter.format(fDegree) + " °F\n"); number = 0.0; } } if(e.getSource() == back) { value = false; setVisible(value); new ConvertSelection(); } if(e.getSource() == clear) { txtfield.setText(""); } if(e.getSource() == exit) { JOptionPane.showMessageDialog(null, "Thank you for using the program"); System.exit(0); } } catch(NumberFormatException nf) { } } } }