Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pleas note that I\'m using Dr-Java Widnwos Write a Java program to create a GUI

ID: 3836300 • Letter: P

Question

Pleas note that I'm using Dr-Java Widnwos

Write a Java program to create a GUI using JFrame, JTextField, JLabel and JButton objects. This is continuation of Project 7 which used the text-based menu driven model.

Write two value-returning 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. (These should already be done and can be taken from Project 7. No other code from Project 7 will be of any consequence.)

Here is a picture of what you will create:

In your window constructor, do the following:

Create two labels “Temperature:” and “Results:” (text centered).

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.

p8 window = new p8();

write a Java program to create a GUI usirgJFrane JTextF:eld, Jiabel and 1Button objects. This is continuation of Project 7 which used the textbasedmenu driven model. Write two value-returning methods called far ocelK and clTofar y.These two methods will convert temperatures from Fahrenheit to Calsius and Celsius to Fahrtnheit respectively. They will each take a single int parameter and retum the converted value as animt. These should already be done and can be taken from Project 7. No other code from Project 7 wil be of any consequence) Here is a picture of what you will create: B Celia and Fahrvrh Carversion In your window constructor, do the fallowing: Create two labels Temperature and Results (text centered. Create two text fields: The termperature text field is 10 columns wide and shouldbelnitiallyset to zero, The results text teld is 40 columns wide,has noinitial value and should not be an editable field. Create two buttons"Celsiusto Fahrenhier and Fahrenheit to Celsius" Set the window title to Cebius and Fahrenheit Conversion" Create the Container for the content pane: Set the layout to the Grid layout of 3rcws by 2 columns. Add the temperature label and text field. Add the buttons. Add the lt label and taxt fiald. e Assign the button handlers (discussed below). Set the window size to 375 pinels wide by 120 pixels high. Set the default close operation to EOT.ON.cLOSE and set the window visible. For the button handlers, recall that you will need to create additional dasses that implement the actlonL1sterer interface. Foreach actionperformed() method, you should: Read the temperature text field value Convert the String temperature to an integer. Convert the temperature from cne scale to the other. Display the relative information in the tesults bext field. The matn method should have only one ine of code: pa Mindon new p Which creates a new pa object and assigns itto the reference variable window.

Explanation / Answer

PROGRAM CODE:

package util;

import java.awt.Container;

import java.awt.GridLayout;

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;

public class p8{

  

   JButton celTofah, fahTocel;

   JTextField temperatureField, resultField;

   public p8() {

       JFrame frame = new JFrame("Celsius and Fahrenheit Conversion");

      

       JLabel temperature = new JLabel("Temperature:");

       JLabel result = new JLabel("Result;");

      

       temperatureField = new JTextField(10);

       temperatureField.setText("0");

       resultField = new JTextField(10);

       resultField.setText("0");

       resultField.setEditable(false);

      

       celTofah = new JButton("Celsius to Fahrenheit");

       fahTocel = new JButton("Fahrenheit to Celsius");

      

       Container container = new Container();

       container.setLayout(new GridLayout(3, 2));

       container.add(temperature);

       container.add(temperatureField);

       container.add(celTofah);

       container.add(fahTocel);

       container.add(result);

       container.add(resultField);

      

       celTofah.addActionListener(new ButtonListener());

       fahTocel.addActionListener(new ButtonListener());

       frame.getContentPane().add(container);

       frame.setSize(375, 120);

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.setVisible(true);

   }

      

   public int fahToCel(int temp)

   {

       return    (temp - 32 )*5/9;

   }

  

   public int celToFar(int temp)

   {

       return temp *9/5 + 32;

   }

  

   class ButtonListener implements ActionListener

   {

       @Override

       public void actionPerformed(ActionEvent e) {

           int temp = Integer.valueOf(temperatureField.getText());

           if(e.getSource()==celTofah)

           {

               resultField.setText(temp + " Celsius is " + celToFar(temp) + " Fahrenheit");

           }

           else

               resultField.setText(temp + " Fahrenheit is " + fahToCel(temp) + " Celsius");

       }

      

   }

   public static void main(String[] args) {

p8 window = new p8();

   }

}