package TemperatureConverter; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.text.*; public class ConvertTempFrame extends JFrame { /** * */ private static final long serialVersionUID = 1L; private ActionListener earpiece; private JTextField inputbox = new JTextField(10); private JTextField outputbox = new JTextField(10); private JRadioButton CelsiusIn; private JRadioButton FahrenheitIn; private JRadioButton KelvinIn; private JRadioButton CelsiusOut; private JRadioButton FahrenheitOut; private JRadioButton KelvinOut; private JLabel inputTitle = new JLabel("Temp Value:"); private JLabel outputTitle = new JLabel("Converted Temp Value:"); DecimalFormat two = new DecimalFormat("0.00"); public ConvertTempFrame() { //creates 2 panels for border with the input and the output fields JPanel inputPanel = new JPanel(); inputPanel.add(inputTitle); inputPanel.add(inputbox); JPanel outputPanel = new JPanel(); outputPanel.add(outputTitle); outputPanel.add(outputbox); outputbox.setEditable(false); add(inputPanel, BorderLayout.NORTH); add(outputPanel, BorderLayout.SOUTH); class ChoiceListener implements ActionListener { public void actionPerformed(ActionEvent event) { outputConvertedTemp();//performed when a button is selected } } earpiece = new ChoiceListener(); createConversionTable(); setSize(400, 200); } public void createConversionTable() { //creates center panel to hold radio button panels JPanel leftPanel = RadioButtonInput(); JPanel rightPanel = RadioButtonOutput(); JPanel conversionTable = new JPanel(); conversionTable.setLayout(new GridLayout(1, 2)); conversionTable.add(leftPanel); conversionTable.add(rightPanel); add(conversionTable, BorderLayout.CENTER); } public JPanel RadioButtonInput() { //creates the input radio buttons, groups them, aligns them, puts a border on, returns the panel CelsiusIn = new JRadioButton("Celsius"); CelsiusIn.addActionListener(earpiece); FahrenheitIn = new JRadioButton("Fahrenheit"); FahrenheitIn.addActionListener(earpiece); KelvinIn = new JRadioButton("Kelvin"); KelvinIn.addActionListener(earpiece); ButtonGroup group = new ButtonGroup(); group.add(CelsiusIn); group.add(FahrenheitIn); group.add(KelvinIn); JPanel radioButtonsInput = new JPanel(); radioButtonsInput.setLayout(new GridLayout(3, 1)); radioButtonsInput.add(CelsiusIn); radioButtonsInput.add(FahrenheitIn); radioButtonsInput.add(KelvinIn); radioButtonsInput.setBorder( new TitledBorder(new EtchedBorder(), "Degree Input")); return radioButtonsInput; } public JPanel RadioButtonOutput() { //creates the output radio buttons, groups them, aligns them, puts a border on, returns the panel CelsiusOut = new JRadioButton("Celsius"); CelsiusOut.addActionListener(earpiece); FahrenheitOut = new JRadioButton("Fahrenheit"); FahrenheitOut.addActionListener(earpiece); KelvinOut = new JRadioButton("Kelvin"); KelvinOut.addActionListener(earpiece); ButtonGroup group2 = new ButtonGroup(); group2.add(CelsiusOut); group2.add(FahrenheitOut); group2.add(KelvinOut); JPanel radioButtonsOutput = new JPanel(); radioButtonsOutput.setLayout(new GridLayout(3, 1)); radioButtonsOutput.add(CelsiusOut); radioButtonsOutput.add(FahrenheitOut); radioButtonsOutput.add(KelvinOut); radioButtonsOutput.setBorder( new TitledBorder(new EtchedBorder(), "Degree Output")); return radioButtonsOutput; } public void outputConvertedTemp() { //reads buttons selected to determine output int temp; double newtemp = 0; temp = Integer.parseInt(inputbox.getText()); if (CelsiusIn.isSelected() & KelvinOut.isSelected()) newtemp = (temp + 273.15);//c to k else if (FahrenheitIn.isSelected() & KelvinOut.isSelected()) newtemp = ((temp + 459.67) * 0.556);//f to k else if (KelvinIn.isSelected() & FahrenheitOut.isSelected()) newtemp = (((temp * 1.8) - 459.67));//k to f else if (CelsiusIn.isSelected() & FahrenheitOut.isSelected()) newtemp = (((1.8) * temp) + 32);//c to f else if (FahrenheitIn.isSelected() & CelsiusOut.isSelected()) newtemp = ((.556) * (temp - 32));//f to c else if (KelvinIn.isSelected() & CelsiusOut.isSelected()) newtemp = (temp - 273.15);//k to c //the following are to ensure that the output stays what it should be else if (CelsiusIn.isSelected() & CelsiusOut.isSelected()) newtemp = temp; else if (FahrenheitIn.isSelected() & FahrenheitOut.isSelected()) newtemp = temp; else if (KelvinIn.isSelected() & KelvinOut.isSelected()) newtemp = temp; String degree = "" + (char)186; outputbox.setText(" " + two.format(newtemp) + degree); } }
package TemperatureConverter; import javax.swing.*; public class ConvertTemp { public static void main(String[] args) { //creates main box panel for everything to go in JFrame frame = new ConvertTempFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Temperature Converter"); frame.setVisible(true); } }