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);
 }
}

Guidelines for the project are as follows. I forgot to use try/catch and set the column sizes to 10 and 40 and a few other things. Can only get so much done in a few hours including reading, rereading and then reading it all over again :P

Write a Java program to create a GUI using JFrame, JTextField, JLabel and
JButton objects. Write two valuereturning methods called farToCel() and celToFar(). These two methods will convert temperatures from Fahrenheit to Celsius and Celsius to Fahrenheit respectively. They will each take a single int parameter and return the converted value as an int.
In your window constructor, do the following:
 Create two labels “Temperature:” and “Results:”
 Create two text fields:
 The temperature text field is 10 columns wide and should be initially set to
zero.
 The results text field is 40 columns wide, has no initial value and should
not be an editable field.
 Create two buttons “Celsius to Fahrenhiet” and “Fahrenheit to Celsius”.
 Set the window title to “Celsius and Fahrenheit Conversion”
 Create the Container for the content pane:
 Set the layout to the Grid layout of 3 rows by 2 columns
 Add the temperature label and text field.
 Add the buttons.
 Add the result label and text field.
 Assign the button handlers (discussed below).
 Set the window size to 375 pixels wide by 120 pixels high.
 Set the default close operation to EXIT_ON_CLOSE and set the window visible.
For the button handlers, recall that you will need to create additional classes that
implement the ActionListener interface.For each actionPerformed() method,
you should:
● Read the temperature text field value.
● Convert the String temperature to an integer.
● Convert the temperature from one scale to the other.
● Display the relative information in the results text field.
The main() method should have only one line of code:
p8 window = new p8();
Which creates a new p8 object and assigns it to the reference variable window.
Use the try/catch and do not allow nonnumeric quantities for Temperature text field and display an error in the Result text field.

The instructions seem pretty clear to me, so I'm not sure what you're having problems with. Reread the part about what your action listeners need to do and try to do those steps in each of your action listeners. Also note the instructions are very explicit about what belongs in your main program. If there's something in the instructions you don't understand, please ask a more specific question.

Line 43, you assign value to cel = 0, and then you use it in line 44, you display the text accepted by the user and the cel (0). What you need to do is to accept the text from the user, convert it to whatever value you are supposed to do, and then place the value back to 'cel' before you display it. The same would go to line 63 & 64.

Here is most of project 8. Missing 2 things for full credit: set temp text feild to 10 columns wide and set results text field to 40 columns wide. Will try to get around to posting with that coded in too later. Trying to work on the extra credit part of including the try/catch. Going to do some reading... any help on the try/catch part would be great though since I know I'm not going to get it first try.

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;



	 cel = Integer.parseInt(inputTF.getText());
	 displayTF.setText(+ cel + " Celsius is " + (celToFah(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;



	 fah = Integer.parseInt(inputTF.getText());
	 displayTF.setText(+ fah + " Fahrenheit is " + (fahToCel(fah)) + " Celcius.");
 	}
 }

  public static void main(String[] args)
 {
   p8 win = new p8();
 }
}

Completed p8 with try/catch for extra credit. Thank you to everyone who helped us. :) :) :)

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(10);
  displayTF = new JTextField(40);
  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;
	 try
	 {
	  cel = Integer.parseInt(inputTF.getText());
	  displayTF.setText(+ cel + " Celsius is " + (celToFah(cel)) + " Fahrenheit");
	 }
	 catch(NumberFormatException f)
	 {
	  displayTF.setText("Please re-enter a number.");
	 }
   }
}
  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;
	 try
	{
	 fah = Integer.parseInt(inputTF.getText());
	 displayTF.setText(+ fah + " Fahrenheit is " + (fahToCel(fah)) + " Celcius.");
	}
	 catch(NumberFormatException f)
	{
		 displayTF.setText("Please re-enter a number.");
	}
  }
}
  public static void main(String[] args)
 {
   p8 win = new p8();
 }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.