I'm in a rather unique situation based on what I've seen in other's coding. I'm working on a multi-frame GUI for a school project and it has four converters between various units: Mass, Volume, Distance, and Temperature. The problem I run into lies with the Text Listeners for my temperature conversions:
private class TempListener implements ActionListener{
public void actionPerformed (ActionEvent event){
double faren, cels;
String text = temperature.getText();
faren = Integer.parseInt(text);
cels = (faren-32) * 5/9;
rLabelT.setText(Double.toString(cels));
}
}
private class Temp1Listener implements ActionListener{
public void actionPerformed (ActionEvent event){
double faren, cels;
String text = temperature1.getText();
cels = Integer.parseInt(text);
faren = (cels+32) * 9/5;
rLabelT1.setText(Double.toString(faren));
}
}
The Integer.parseInt method catches a NumberFormatException.java and stops it from completing the calculation. Now, this coding is almost identical to a similar program from my textbook with the only edits being the variable type. What I worry about is that the other frames have fully functional calculations based on the same block:
private class MassListener implements ActionListener{
public void actionPerformed (ActionEvent event){
double pounds, kilograms;
String text = mass.getText();
pounds = Integer.parseInt(text);
kilograms = pounds/2.2;
rLabelM.setText(Double.toString(kilograms));
}
}
private class VolumeListener implements ActionListener{
public void actionPerformed (ActionEvent event){
double litres, gallons;
String text = volume.getText();
gallons = Integer.parseInt(text);
litres = .264 * gallons;
rLabelV.setText(Double.toString(litres));
}
}
private class DistListener implements ActionListener{
public void actionPerformed (ActionEvent event){
double miles, kilo;
String text = distance.getText();
miles = Integer.parseInt(text);
kilo = miles * 1.609;
rLabelD.setText(Double.toString(kilo));
}
}
All of these work with EXACTLY the same coding without any errors. How do I fix this? It's the last issue in my code to address.
The code to open the frame that contains the temp conversions:
private class ButtonTListener implements ActionListener{
public void actionPerformed (ActionEvent event){
JFrame dialogT = new JFrame ("Temperature");
dialogT.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialogT.setBounds(750,650,400,150);
dialogT.setVisible(true);
iLabelT = new JLabel ("Enter a Temperature in Farenheit: ");
oLabelT = new JLabel ("Temperature in Celsius: ");
rLabelT = new JLabel ("--");
temperature = new JTextField (5);
temperature.addActionListener(new TempListener());
iLabelT1 = new JLabel ("Enter a Temperature in Celsius: ");
oLabelT1 = new JLabel ("Temperature in Farenheit: ");
rLabelT1 = new JLabel ("--");
temperature1 = new JTextField (5);
temperature.addActionListener(new Temp1Listener());
Temperature1 = new JPanel();
Temperature1.setPreferredSize(new Dimension (400,100));
Temperature1.setLayout(new FlowLayout());
Temperature1.setBackground (Color.yellow);
Temperature1.add(iLabelT);
Temperature1.add(temperature);
Temperature1.add(oLabelT);
Temperature1.add(rLabelT);
Temperature1.add(iLabelT1);
Temperature1.add(temperature1);
Temperature1.add(oLabelT1);
Temperature1.add(rLabelT1);
dialogT.getContentPane().add(Temperature1);
}
}