Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions: Convert the following code into a Graphic Component (J frame panrl) package temperatureconverter; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JTextField; import

Instructions: Convert the following code into a Graphic Component (J frame panrl)

package temperatureconverter;

import java.awt.Dimension;

import java.awt.GridLayout;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.event.DocumentEvent;

import javax.swing.event.DocumentListener;

public class TemperatureConverter{

public static void main(String[] args){ //main class

final JFrame f=new JFrame("Temp Conversion"); //we are using jframe //jf = new JFrame("Temperature Conversion"); //ADD TEXT FIELDS

final JTextField leftSide=new JTextField(20);

final JTextField rightSide=new JTextField(20);

// SET LAYOUT TO GRID

f.setLayout(new GridLayout(2, 2));

//ADD COMBO BOXES

final JComboBox jc=new JComboBox();

jc.addItem("Celsius");

jc.addItem("Fahrenheit");

jc.addItem("Kelvin");

final JComboBox jc2=new JComboBox();

jc2.addItem("Celsius");

jc2.addItem("Fahrenheit");

jc2.addItem("Kelvin");

f.add(leftSide);

f.add(rightSide);

f.add(jc);

f.add(jc2);

rightSide.setPreferredSize(new Dimension(100, 50));

f.setVisible(true);

f.pack();

//ADD ON CHANGE EVENT

// Listen for changes in the text

leftSide.getDocument().addDocumentListener(new DocumentListener() {

public void changedUpdate(DocumentEvent e) {

doCalculation();

}

public void removeUpdate(DocumentEvent e) {

doCalculation();

}

public void insertUpdate(DocumentEvent e) {

doCalculation();

}

public void doCalculation() {

//DO CALCULATIONS BASED ON INPUT AND OUTPUT

try{

double fromData = Double.parseDouble(leftSide.getText());

String from = jc.getSelectedItem().toString();

String to = jc2.getSelectedItem().toString();

if(to.equalsIgnoreCase("Celsius") && from.equalsIgnoreCase("Fahrenheit")){

double result =(fromData - 32) * 5 / 9;

rightSide.setText(result+"");

}else if(to.equalsIgnoreCase("Celsius") && from.equalsIgnoreCase("Kelvin")){

double result = fromData - 273.15;

rightSide.setText(result+"");

}else if(to.equalsIgnoreCase("Fahrenheit") && from.equalsIgnoreCase("Kelvin")){

double result = 1.8*(fromData - 273.15) + 32;

rightSide.setText(result+"");

}else if(to.equalsIgnoreCase("Fahrenheit") && from.equalsIgnoreCase("Celsius")){

double result = fromData*1.8+32;

rightSide.setText(result+"");

}else if(to.equalsIgnoreCase("Kelvin") && from.equalsIgnoreCase("Fahrenheit")){

double result = (fromData + 459.67)*5/9;

rightSide.setText(result+"");

}else if(to.equalsIgnoreCase("Kelvin") && from.equalsIgnoreCase("Celsius")){

double result = fromData +273.15;

rightSide.setText(result+"");

}else{

rightSide.setText(fromData+"");

}

}catch(Exception e){

}

}

});

// Listen for changes in the text

rightSide.getDocument().addDocumentListener(new DocumentListener() {

public void changedUpdate(DocumentEvent e) {

doCalculation();

}

public void removeUpdate(DocumentEvent e) {

doCalculation();

}

public void insertUpdate(DocumentEvent e) {

doCalculation();

}

public void doCalculation() {

try{

double fromData = Double.parseDouble(rightSide.getText());

String from = jc2.getSelectedItem().toString();

String to = jc.getSelectedItem().toString();

if(to.equalsIgnoreCase("Celsius") && from.equalsIgnoreCase("Fahrenheit")){

double result =(fromData - 32) * 5 / 9;

leftSide.setText(result+"");

}else if(to.equalsIgnoreCase("Celsius") && from.equalsIgnoreCase("Kelvin")){

double result = fromData - 273.15;

leftSide.setText(result+"");

}else if(to.equalsIgnoreCase("Fahrenheit") && from.equalsIgnoreCase("Kelvin")){

double result = 1.8*(fromData - 273.15) + 32;

leftSide.setText(result+"");

}else if(to.equalsIgnoreCase("Fahrenheit") && from.equalsIgnoreCase("Celsius")){

double result = fromData*1.8+32;

leftSide.setText(result+"");

}else if(to.equalsIgnoreCase("Kelvin") && from.equalsIgnoreCase("Fahrenheit")){

double result = (fromData + 459.67)*5/9;

leftSide.setText(result+"");

}else if(to.equalsIgnoreCase("Kelvin") && from.equalsIgnoreCase("Celsius")){

double result = fromData +273.15;

leftSide.setText(result+"");

}else{

leftSide.setText(fromData+"");

}

}catch(Exception e){

}

}

});

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions

Question

Write the binary number 1010101010101010 as a decimal number.

Answered: 1 week ago

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago