Here is the start of my program. I am having a little trouble linking the farToCel/celToFar user defined methods with the final equations in the action statements. The output is whatever the user enters and 0 for "fah" and "cel".
Example: displayTF.setText(inputTF.getText() + " Celsius is " + cel + " Fahrenheit");
Output: "user entered number" Celsius is 0 Fahrenheit.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class p8 extends JFrame
{
private JLabel temperature, temperaturedisplay;
private JTextField inputTF, displayTF;
private JButton submitA, submitB;
private SubmitAButtonHandler saHandler;
private SubmitBButtonHandler sbHandler;
public p8()
{
Container pane = getContentPane();
pane.setLayout(new GridLayout(3,2));
temperature = new JLabel("Temperature:");
temperaturedisplay = new JLabel("Result:");
inputTF = new JTextField("");
displayTF = new JTextField("");
submitA = new JButton("Celsius to Fahrenheit");
submitB = new JButton("Fahrenheit to Celsius");
saHandler = new SubmitAButtonHandler();
submitA.addActionListener(saHandler);
sbHandler = new SubmitBButtonHandler();
submitB.addActionListener(sbHandler);
pane.add(temperature);
pane.add(inputTF);
displayTF.setEditable(false);
pane.add(submitA);
pane.add(submitB);
pane.add(temperaturedisplay);
pane.add(displayTF);
setTitle("Celsius and Fahrenheit Conversion");
setSize(375,120);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private class SubmitAButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int cel = 0;
displayTF.setText(inputTF.getText() + " Celsius is " + cel + " Fahrenheit");
}
}
public static int celToFah(int cel)
{
return (cel) * 9 / 5 + 32;
}
public static int fahToCel(int fah)
{
return ((fah) - 32) * 5 / 9;
}
private class SubmitBButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int fah = 0;
displayTF.setText(+ fah + " Fahrenheit is " + inputTF.getText() + " Celcius.");
}
}
public static void main(String[] args)
{
int cel = 0, fah = 0;
p8 win = new p8();
celToFah(cel);
fahToCel(fah);
}
}