Reputation: 41
My program runs and the window pop up but it will not evaluate the calculation of converting the user input into *c or *f when the they click the convertoC of converToF button. I tried to put the code in separate action listeners but I get an error. It looks right logically but maybe I have misplaced some of my code in the wrong places. Can anyone help me?
package edu.westga.TemperatureConverter.controller;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/**
*
* @author
*
*/
public class Temperature extends JFrame {
private double fahrenheit;
private double celsius;
private JButton fahrenheitButton;
private JButton celsiusBUtton;
private JTextField textBox;
private JLabel instructions;
private FlowLayout layout;
private Container container;
private JButton clearButton;
private JLabel results;
/**
* Temperature constructor
*/
public Temperature() {
super("temperature converter");
this.layout = new FlowLayout();
this.container = getContentPane();
setLayout(this.layout);
TemperatureConverter convertemp = new TemperatureConverter();
this.instructions = new JLabel("please enter the Temperture:");
add(this.instructions);
this.textBox = new JTextField(10);
add(this.textBox);
this.textBox.addActionListener(convertemp);
this.fahrenheitButton = new JButton(" convert to Fahrenheit");
add(this.fahrenheitButton);
// fahrenheitButton.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent event) {
//
// convertToC();
// }
// });
this.celsiusBUtton = new JButton("convert to Celsius ");
add(this.celsiusBUtton);
// celsiusBUtton.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent event) {
// String text = "";
// double num = 0;
// if (event.getSource() == celsiusBUtton) {
// num = Double.parseDouble(text);
// convertToC(num);
// results.setText("your aswer was converted to:" + num);
// }}
// });
this.clearButton = new JButton(" clear");
add(this.clearButton);
// clearButton.addActionListener(new ActionListener() {
// public void actionPerformed(ActionEvent event) {
//
// convertToC();
// }
// });
this.results = new JLabel("your aswer was converted to: ");
add(this.results);
}
/**
* convert to celsius
*
* @param num
* user input number
* @return converted answer the answer
*/
public double convertToC(double num) {
this.fahrenheit = num;
double convertedAnswer;
convertedAnswer = 5.0 / 9.0 * (this.fahrenheit - 32);
return convertedAnswer;
}
/**
* Convert to fahrenheit
*
* @param num
* user input number
* @return converted answer the answer
*/
public double convertToF(double num) {
this.celsius = num;
double convertedAnswer;
convertedAnswer = (32 + 5 / 9) * this.celsius;
return convertedAnswer;
}
/**
* clears the window of previous numbers
*/
public void clear() {
}
/**
* anonymous class that activates the conversion of the users input
*
* @author
* @version
*
*/
public class TemperatureConverter implements ActionListener {
public void actionPerformed(ActionEvent event) {
String text = "";
double num = 0;
if (event.getSource() == celsiusBUtton) {
num = Double.parseDouble(text);
convertToC(num);
results.setText("your answer was converted to:" + num + "C");
} else {
if (event.getSource() == fahrenheitButton) {
num = Double.parseDouble(text);
convertToF(num);
results.setText("your answer was converted to:" + num + "F");
}
}
}
}
}
Upvotes: 1
Views: 2412
Reputation: 8323
You have a couple things wrong here:
1) You're not saving your computed values in your ActionListener
if (event.getSource() == celsiusBUtton) {
num = Double.parseDouble(text);
num = convertToC(num);
results.setText("your answer was converted to:" + num + "C");
} else {
if (event.getSource() == fahrenheitButton) {
num = Double.parseDouble(text);
num = convertToF(num);
results.setText("your answer was converted to:" + num + "F");
}
}
2) You don't tie the action listener to your buttons.
this.fahrenheitButton = new JButton(" convert to Fahrenheit");
this.fahrenheitButton.addActionListener(convertemp); // make them do something!!
add(this.fahrenheitButton);
this.celsiusBUtton = new JButton("convert to Celsius ");
this.celsiusBUtton.addActionListener(convertemp); // make them do something!!
add(this.celsiusBUtton);
3) In your actionlistener, your text is always "", which will give you a NumberFormatException
Get the text from your text box and you should be good to go (though you will want to do more error checking than this...)
public void actionPerformed(ActionEvent event) {
String text = textBox.getText(); // you need to get the text from the text box
double num = 0;
if (event.getSource() == celsiusBUtton) {
num = Double.parseDouble(text);
num = convertToC(num);
results.setText("your answer was converted to:" + num + "C");
} else {
if (event.getSource() == fahrenheitButton) {
num = Double.parseDouble(text);
num = convertToF(num);
results.setText("your answer was converted to:" + num + "F");
}
}
}
I should probably also point out that the ActionListener
is not listening for actions made by other widgets on the object you place it on, it's listening for actions on that widget. You've got it placed on your text box, which will cause the listener to listen for events on the text box, not your buttons. Your method might be useful for catching the "Enter" key or something if the user was so inclined to do so.
Upvotes: 4