Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Convert the following code into a Graphic Component with Matisse 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

Convert the following code into a Graphic Component with Matisse

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_2

Step: 3

blur-text-image_3

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

Visual Basic Net Database Programming

Authors: Rod Stephens

1st Edition

0789726815, 978-0789726810

More Books

Students also viewed these Databases questions

Question

=+2 How does the preparation and support for each type of IE vary?

Answered: 1 week ago

Question

=+What is the extent of the use of each type of IE?

Answered: 1 week ago