import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TempConversions implements ActionListener
{
JFrame frame;
public JLabel in1, in2, out1, out2;
JRadioButton celin, celout, fahin, fahout, kelin, kelout;
JPanel top, left, right, bottom;
JTextField textfield;
JTextArea area;
double results;
TempConversions()
{
//Gets users desired temp to input
in1 = new JLabel("Original Temp: ");
textfield = new JTextField(10);
top = new JPanel();
top.add(in1);
top.add(textfield);
//Input Buttons
in2 = new JLabel(" Input Scale ");//Labels left side.
celin = new JRadioButton("Celcius (\u00B0C)");
fahin = new JRadioButton("Fahrenheit (\u00B0F)");
kelin = new JRadioButton("Kelvin (\u00B0K)");
ButtonGroup inputgroup = new ButtonGroup();
inputgroup.add(celin);
inputgroup.add(fahin);
inputgroup.add(kelin);
left = new JPanel(new GridLayout(5,1,8,5));
left.add(in2);
left.add(celin);
left.add(fahin);
left.add(kelin);
//Output Buttons
out2 = new JLabel(" Output Scale "); //Labels right side.
celout = new JRadioButton("Celcius (\u00B0C)");
fahout = new JRadioButton("Fahrenheit (\u00B0F)");
kelout = new JRadioButton("Kelvin (\u00B0K)");
ButtonGroup outputgroup = new ButtonGroup();
outputgroup.add(celout);
outputgroup.add(fahout);
outputgroup.add(kelout);
right = new JPanel(new GridLayout(5,1,8,5));
right.add(out2);
right.add(celout);
right.add(fahout);
right.add(kelout);
//Output results
out1 = new JLabel("Converted Temp: ");
area = new JTextArea(1,8);
area.setEditable(false);
bottom = new JPanel();
bottom.add(out1);
bottom.add(area);
//Frame layout. Borderlayout method preferred.
frame = new JFrame("Temp Converter");
frame.add(top, BorderLayout.NORTH);
frame.add(left, BorderLayout.WEST);
frame.add(right, BorderLayout.EAST);
frame.add(bottom, BorderLayout.SOUTH);
textfield.addActionListener(this);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
@SuppressWarnings("unused")
public void actionPerformed(ActionEvent act)
{
TempCalculation cal = new TempCalculation();
String s = textfield.getText();
double temps = Double.parseDouble(s);
String e = "No text";
if (act.getSource() == textfield)
{
if (celin.isSelected() && fahout.isSelected())
{
double uinput = Integer.parseInt(s);
uinput = temps;
temps=cal.CtoF(temps);
area.append(temps+ "\u00B0f");
}
else if(celin.isSelected() && kelout.isSelected())
{
double uinput = Integer.parseInt(s);
uinput = temps;
temps=cal.CtoK(temps);
area.append(temps+ "\u00B0k");
}
else if (fahin.isSelected() && celout.isSelected())
{
double uinput = Integer.parseInt(s);
uinput = temps;
temps=cal.FtoC(temps);
area.append(temps+ "\u00B0c");
}
else if(fahin.isSelected() && kelout.isSelected())
{
double uinput = Integer.parseInt(s);
uinput = temps;
temps=cal.FtoK(temps);
area.append(temps+ "\u00B0k");
}
else if (kelin.isSelected() && celout.isSelected())
{
double uinput = Integer.parseInt(s);
uinput = temps;
temps=cal.KtoC(temps);
area.append(temps+ "\u00B0c");
}
else if(kelin.isSelected() && fahout.isSelected())
{
double uinput = Integer.parseInt(s);
uinput = temps;
temps=cal.KtoF(temps);
area.append(temps+ "\u00B0f");
}
else
{
area.append(e);
}
}
}
@SuppressWarnings("unused")
public static void main(String[] args)
{
TempCalculation CTempCalcultion = new TempCalculation();
}
}