So my coding is a temperature converter. I have gotten everything down the way that it is wanted by the assignment except for one part. When I run the program, put in a value and select the radio button for conversion, no output ever shows up. Was hoping that someone could take a look and see what is going on with it. Here is the coding, it is two separate classes within java.
import javax.swing.*;
/**
* This program allows the user to convert temperatures
*
*/
public class ConvertTemp {
public static void main(String[] args) {
JFrame frame = new ConvertTempFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Temperature Converter");
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
@SuppressWarnings("serial")
public class ConvertTempFrame extends JFrame
{
/**
* Constructs the frame
*/
public ConvertTempFrame()
{
//adding input panel
JPanel inputPanel = new JPanel();
inputPanel.add(inlabel);
inputPanel.add(infield);
//adding output panel
JPanel outputPanel = new JPanel();
outputPanel.add(outlabel);
outputPanel.add(outfield);
outfield.setEditable(false);//removes ability to enter a value in the output field
add(inputPanel, BorderLayout.NORTH); //adding input panel to frame
add(outputPanel, BorderLayout.SOUTH); //adding output panel to frame
add(createRadioButtons(), BorderLayout.CENTER);
//This is the listener shared among all components
class ChoiceListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
outputConvertedTemp();
}
}
listener = new ChoiceListener();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
/**
* Creates the panel for selecting converting option
*/
public JPanel createRadioButtons()
{
/**
* Creates radio buttons to select the conversion type
*/
CtoK = new JRadioButton("Celsius to Kelvin");
CtoK.addActionListener(listener);
FtoK = new JRadioButton("Fahrenheit to Kelvin");
FtoK.addActionListener(listener);
KtoF = new JRadioButton("Kelvin to Fahrenheit");
KtoF.addActionListener(listener);
CtoF = new JRadioButton("Celsius to Fahrenheit");
CtoF.addActionListener(listener);
FtoC = new JRadioButton("Fahrenheit to Celsius");
FtoC.addActionListener(listener);
KtoC = new JRadioButton("Kelvin to Celsius");
KtoC.addActionListener(listener);
ButtonGroup group = new ButtonGroup();
group.add(CtoK);
group.add(FtoK);
group.add(KtoF);
group.add(CtoF);
group.add(FtoC);
group.add(KtoC);
JPanel radioButtons = new JPanel();
radioButtons.setLayout(new GridLayout(3, 2));
radioButtons.add(CtoK);
radioButtons.add(FtoK);
radioButtons.add(KtoF);
radioButtons.add(CtoF);
radioButtons.add(FtoC);
radioButtons.add(KtoC);
return radioButtons;
}
public void outputConvertedTemp()
{
int temp;
double newtemp = 0;
String inputString;
inputString = infield.getText();//pulls the number that was entered in the input field
temp = Integer.parseInt(inputString);// makes a new variable to handle the input
//tells the output how to calculate the input based on the button that was pressed
if (CtoK.isSelected())
newtemp = (temp + 273);//Celsius to Kelvin equation
else if (FtoK.isSelected())
newtemp = ((.55555) * (temp - 32) + 273);//Fahrenheit to Kelvin equation
else if (KtoF.isSelected())
newtemp = (((temp - 273) * (1.8)) + 32);//Kelvin to Fahrenheit equation
else if (CtoF.isSelected())
newtemp = (((1.8) * temp) + 32);//Celsius to Fahrenheit equation
else if (FtoC.isSelected())
newtemp = ((.55555) * (temp - 32));//Fahrenheit to Celsius equation
else if (KtoC.isSelected())
newtemp = (temp - 273);//Kelvin to Celsius equation
//sets newtemp value to output field
outfield.setText(" "+ three.format(newtemp));
}
DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output
private ActionListener listener;
private JTextField infield = new JTextField(10);
private JTextField outfield = new JTextField(10);
private JLabel inlabel = new JLabel("Temperature Value:");
private JLabel outlabel = new JLabel("Converted Temperature Value:");
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 200;
private JRadioButton CtoK;
private JRadioButton FtoK;
private JRadioButton KtoF;
private JRadioButton CtoF;
private JRadioButton FtoC;
private JRadioButton KtoC;
}
I think it might have to do with the ActionListener but I am not sure. Thanks in advance.