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

Write a Java GUI application to do temperature conversions between Celcius, Fahr

ID: 3563832 • Letter: W

Question

Write a Java GUI application to do temperature conversions between Celcius, Fahranheit, and Kelvin. The GUI display should look something like the following:

I think I just need to add the equations for conversion but I dont know how or where to add them, remember there are two classes, im using Eclipse, im offering full points if it runs with the code I have so far, alot of it is done, heres what I have so far...thanks..

// First class

import javax.swing.*;

//import EventHandlerSource.ActionEventHandler;
//import EventHandlerSource.ItemEventHandler;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

// This application inherits from the JFrame class and uses different layout managers
// to control the layouts of different panels of the GUI. The top level container is
// a JFrame which has a BorderLayout by default. This application creates four different
// JPanels, each with its own layout manager to manage the GUI components that will
// appear at the top and bottom of the display, and to the right and left of the display.
// The top and bottom panels will use FlowLayouts to manage horizontal components.
// The left panel uses a GridLayout and the right panel uses a BoxLayout
// to manage vertical components.

public class Temp extends JFrame
{
   JPanel left, right, top, bottom;
   JButton one, two, three, four;
   JLabel lone, ltwo, lthree, lfour, tlabel, blabel;
   JTextField tfone, tftwo;
   JRadioButton rradio1, rradio2, rradio3, lradio1, lradio2, lradio3;
   ButtonGroup leftGroup, rightGroup;
   static int outType, inType; //1 = celsius, 2 = farenheit, 3 = kelvin
  
  
   Temp( )
   {
       // Build four panels using different layouts and different alignments
       buildLeftPanel();
       buildRightPanel();
       buildTopPanel();
       buildBottomPanel();
       // JFrame has BorderLayout by default, so put one panel in each area
       add(left, BorderLayout.WEST);
       add(right, BorderLayout.EAST);
       add(top, BorderLayout.NORTH);
       add(bottom, BorderLayout.SOUTH);
      
       ActionEventHandler aehdlr = new ActionEventHandler();
       tfone.addActionListener(aehdlr);
       ItemEventHandler iehdlr = new ItemEventHandler();
       rradio1.addItemListener(iehdlr);
       rradio2.addItemListener(iehdlr);
       rradio3.addItemListener(iehdlr);
       lradio1.addItemListener(iehdlr);
       lradio2.addItemListener(iehdlr);
       lradio3.addItemListener(iehdlr);
   }
  
   private void buildLeftPanel()
   {
       // Create a dimension object to set the preferred size of the components
       Dimension dim = new Dimension(80, 25);
       // Label text is left justified by default, so lets center it
       lone = new JLabel("Input Scale", JLabel.CENTER);
       // Set the preferred size for all components to the same size
       // GridLayout uses this information, but does not always follow it
       lone.setPreferredSize(dim);
      
       lradio1 = new JRadioButton("Celcius");
       lradio2 = new JRadioButton ("Farenheit");
       lradio3 = new JRadioButton ("Kelvin");
      
       leftGroup = new ButtonGroup();
       leftGroup.add(lradio1);
       leftGroup.add(lradio2);
       leftGroup.add(lradio3);
           // Create the left panel, set the layout, and add components to the panel
       left = new JPanel();
       // Create a GridLayout for a column of 4 vertical components
       // Specify 5 pixels for horizontal and vertical spacing
       left.setLayout(new GridLayout(4,1,5,5));      
       left.add(lone);
       left.add(lradio1);
       left.add(lradio2);
       left.add(lradio3);
      
   }
  
   private void buildRightPanel()
   {
       // Create a dimension object to set the preferred size of the components
               Dimension dim = new Dimension(80, 25);
               // Label text is left justified by default, so lets center it
               lthree = new JLabel("Output Scale", JLabel.CENTER);
               // Set the preferred size for all components to the same size
               // GridLayout uses this information, but does not always follow it
               lthree.setPreferredSize(dim);
              
               rradio1 = new JRadioButton("Celcius");
               rradio2 = new JRadioButton ("Farenheit");
               rradio3 = new JRadioButton ("Kelvin");
              
               rightGroup = new ButtonGroup();
               rightGroup.add(rradio1);
               rightGroup.add(rradio2);
               rightGroup.add(rradio3);
                   // Create the left panel, set the layout, and add components to the panel
               right = new JPanel();
               // Create a GridLayout for a column of 4 vertical components
               // Specify 5 pixels for horizontal and vertical spacing
               right.setLayout(new GridLayout(4,1,5,5));      
               right.add(lthree);
               right.add(rradio1);
               right.add(rradio2);
               right.add(rradio3);
   }
  
   private void buildTopPanel()
   {
       top = new JPanel();
       // Use the default FlowLayout of the JPanel for a row of components
       // Setup the FlowLayout of the JPanel to shift everything to the left side
       FlowLayout lm = (FlowLayout)top.getLayout();
       lm.setAlignment(FlowLayout.CENTER);
       tlabel = new JLabel("Input");
       // Create a text field which will hold 10 characters
       tfone = new JTextField(10);
       top.add(tlabel);
       top.add(tfone);
      
      
   }
  
   private void buildBottomPanel()
   {
       bottom = new JPanel();
       // Same setup as for the top panel, except we shift everything to the right
       FlowLayout lm = (FlowLayout)bottom.getLayout();
       lm.setAlignment(FlowLayout.CENTER);
       blabel = new JLabel("Output");
       tftwo = new JTextField(10);
       bottom.add(blabel);
       bottom.add(tftwo);
   }
  
   public static void main(String[] args)
   {
       // Create an object of our SimpleGUI class
       Temp app = new Temp();
       // Close the application when the window is closed
       app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // Set the size in pixels of the window... width, height
       app.setSize(300,200);
       // Make the window visible!
       app.setVisible(true);
   }
  
   // Nested inner class to handle ActionEvents from JButtons and the JTextField
       private class ActionEventHandler implements ActionListener
       {
          
           double value;
           public void actionPerformed(ActionEvent e)
           {
               // Use the getSource() method to determine which object caused the event
              
               if (e.getSource() == tfone)
               {
                   value=Double.parseDouble(tfone.getText());
                   tftwo.setText(Double.toString(value));
               }
           }
       }
      
      
       // Nested inner class to handle ItemEvents from JRadioButtons and JCheckBoxes
       private class ItemEventHandler implements ItemListener
       {
           public void itemStateChanged (ItemEvent e)
           {
              
              
               if(e.getSource() == rradio1)
               {
                   if(rradio1.isSelected())
                   {
                      
                       Temp.outType = 1;
                   }
               }
              
              
              
              
               else if(e.getSource() == rradio2)
               {
                   if(rradio2.isSelected())
                   {
                       Temp.outType = 2;
                   }
               }
              
              
              
              
               else if(e.getSource() == rradio3)
               {
                   if(rradio3.isSelected())
                   {
                       Temp.outType = 3;
                   }
               }
              
              
              
              
               else if(e.getSource() == lradio1)
               {
                   if(lradio1.isSelected())
                   {
                       Temp.outType = 1;
                   }
               }
              
              
              
              
               else if(e.getSource() == lradio2)
               {
                   if(lradio2.isSelected())
                   {
                       Temp.outType = 2;
                   }
               }
              
              
              
              
               else if(e.getSource() == lradio3)
               {
                   if(lradio3.isSelected())
                   {
                       Temp.outType = 3;
                   }
               }
              
              
              
              
              
              
              
                   }
               }
}
          

// Second Class

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
// This class demonstrates handling ActionEvents from JButtons and TextFields.
// It also demonstrates handling ItemEvents from RadioButtons and CheckBoxes.
// The ActionListener interface and the ItemListener interface are implemented
// by two different inner classes.
public class EventHandlerSource extends JFrame
{
   JButton button1, button2;
   JRadioButton rbutton;
   JCheckBox cbox;
   JTextField textfield;
   JLabel lcenter, lbottom;
   JTextArea area;
   JScrollPane scroller;
   JPanel top, center, bottom;
  

  

   EventHandlerSource()
   {
       super("Event Handling Demo");
       // Build the three panels for the GUI
       buildTopPanel();
       buildCenterPanel();
       buildBottomPanel();
       // Add the panels to the frame - JFrames have a BorderLayout by default
       add(top, BorderLayout.NORTH);
       add(center, BorderLayout.CENTER);
       add(bottom, BorderLayout.SOUTH);
       // Set up event handling for the JButtons and the JTextField
       // Create an ActionEventHandler object as the event handler
       // and register it with the JButtons and JTextField
       ActionEventHandler aehdlr = new ActionEventHandler();
       button1.addActionListener(aehdlr);
       button2.addActionListener(aehdlr);
       textfield.addActionListener(aehdlr);
       // Set up event handling for the JRadioButton and the JCheckBox
       // Create an ItemEventHandler object as the event handler and
       // register it with the JRadioButton and JCheckBox
       ItemEventHandler iehdlr = new ItemEventHandler();
       rbutton.addItemListener(iehdlr);
       cbox.addItemListener(iehdlr);      
       // Finish setting up the frame and make it visible
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       // Pack will make the display area as small as possible
       // based on the size of the components we added to the frame
       pack();
       setVisible(true);
   }
  
   private void buildTopPanel()
   {
       // Create two JButtons, a JRadioButton, and a JCheckBox for the top panel
       // JPanels have FlowLayout which arranges things left to right, top to bottom
       button1 = new JButton("I'm Button 1");
       button2 = new JButton("I'm Button 2");
       rbutton = new JRadioButton("I'm a RadioButton");
       cbox = new JCheckBox("I'm a CheckBox");
       // Create the JPanel and add the buttons to the panel
       top = new JPanel();
       top.add(button1);
       top.add(button2);
       top.add(rbutton);
       top.add(cbox);      
   }
  
   private void buildCenterPanel()
   {
       // Create a JLabel and a JTextField for the center panel
       lcenter = new JLabel("Enter some text then press enter!");
       // Setup the JTextField for 10 characters
       textfield = new JTextField(10);
       // Create the JPanel and add the components to it
       center = new JPanel();
       center.add(lcenter);
       center.add(textfield);      
   }
  
   private void buildBottomPanel()
   {
       // Create a JLabel and a scrollable JTextArea for the bottom panel
       lbottom = new JLabel("Event Report");
       // JTextArea will show 4 lines of text, 20 columns wide
       area = new JTextArea(4, 20);
       // Don't allow the user to enter text into the JTextArea
       area.setEditable(false);
       // Place JTextArea in a JScrollPane so we can scroll thru all text in the text area
       scroller = new JScrollPane(area);
       // Create the JPanel and add components to it
       bottom = new JPanel();
       bottom.add(lbottom);
       // Add the JScrollPane to the panel... don't add the JTextArea to the panel
       bottom.add(scroller);
   }
   // Nested inner class to handle ActionEvents from JButtons and the JTextField
   private class ActionEventHandler implements ActionListener
   {
       public void actionPerformed(ActionEvent e)
       {
           // Use the getSource() method to determine which object caused the event
           if (e.getSource() == button1)
              
           {
               // Report that Button 1 was clicked
               area.setText("Button 1 clicked ");
               // Use the getActionCommand() method to display the button's text
               area.append("GetActionCommand: " + e.getActionCommand() + " ");
           }
           else if (e.getSource() == button2)
           {
               // Report that Button 2 was clicked
               area.setText("Button 2 clicked ");
               // Use the component's getText() method to display the button's text
               area.append("GetText: " + button2.getText());          
           }
           else if (e.getSource() == textfield)
           {
               // Report that the enter key was typed in the text field
               area.setText("Enter key typed in the text field ");  
               // Use the component's getText() method to display the text field's text
               area.append("GetText: " + textfield.getText());  
               // Now clear the text field
               textfield.setText("");
           }
       }
   }
   // Nested inner class to handle ItemEvents from JRadioButtons and JCheckBoxes
   private class ItemEventHandler implements ItemListener
   {
       public void itemStateChanged(ItemEvent e)
       {
           String action;
           // Determine which button was the source of this event
           // Then check the state of that button object
           if (e.getSource() == rbutton)
           {
               action = "RadioButton was clicked! ";
               // Find out if the button is selected or not
               if(rbutton.isSelected())
               {
                   action += "RadioButton is turned on! ";
               }
               else
               {
                   action += "RadioButton is turned off! ";
               }
           }
           else
           {
               action = "CheckBox was clicked! ";
               if(cbox.isSelected())
               {
                   action += "CheckBox is turned on! ";
               }
               else
               {
                   action += "CheckBox is turned off! ";
               }
           }
           // Display the string in the JTextArea
           area.setText(action);
       }
   }
}


           

Explanation / Answer

public class M4L1T1 { static JTextField celInput = new JTextField("0"); static JTextField Fahrenheit, Celsius, Kelvin; private static class EventHandler implements ActionListener { /** * this method gets called when an object we are listening to is interacted with * * @param evt ActionEvent that interacted with */ public void actionPerformed(ActionEvent evt) { //creates the formating we would like for the numbers DecimalFormat df = new DecimalFormat("#.##"); //if the event triggered was celInput than if (evt.getSource() == celInput) { try { //get the input from the JTextField String num = Celsius.getText(); //convert the String to a number double number = Double.parseDouble(num); //set the JTextFields to the formated number of the converted temps String fNum = df.format(convertCtoF(number)); String kNum = df.format(convertCtoK(number)); Fahrenheit.setText(fNum); Kelvin.setText(kNum); } catch (NumberFormatException e) { //this happens if Java CANNOT convert the String to a number celInput.setText("Illegal data"); } } else if (evt.getSource() == celInput) { try { //get the input from the JTextField String num = Fahrenheit.getText(); //convert the String to a number double number = Double.parseDouble(num); //set the JTextFields to the formated number of the converted temps String cNum = df.format(convertFtoC(number)); String kNum = df.format(convertCtoK(convertFtoC(number))); Celsius.setText(cNum); Kelvin.setText(kNum); } catch (NumberFormatException e) { //this happens if Java CANNOT convert the String to a number celInput.setText("Illegal data"); } } else { try { //get the input from the JTextField String num = Kelvin.getText(); //convert the String to a number double number = Double.parseDouble(num); //set the JTextFields to the formated number of the converted temps String fNum = df.format(convertCtoF(convertKtoC(number))); String cNum = df.format(convertKtoC(number)); Fahrenheit.setText(fNum); Celsius.setText(cNum); } catch (NumberFormatException e) { //this happens if Java CANNOT convert the String to a number celInput.setText("Illegal data"); } } }//end actionPerformed method /** * ... * * @param c Degrees Celsius * @return f Degrees Fahrenheit */ private double convertCtoF (double c) { double f = c * 9/5 + 32; return f; } //end convertCtoF method /** * ... * * @param c Degrees Celsius * @return k Kelvin */ private double convertCtoK (double c) { double k = c + 273.15; return k; } //end convertCtoK method /** * ... * * @param f Degrees Fahrenheit * @return c Degrees Celsius */ private double convertFtoC (double f) { double c = (f - 32) * 5/9; return f; } //end convertFtoC method /** * ... * * @param k Kelvin * @return c Degrees Celsius */ private double convertKtoC (double k) { double c = k - 273.15; return c; } //end convertTtoC method } public static void main(String args[]) { //creates a new window JFrame window = new JFrame("Temperature Conversion"); //create JPanels here JPanel main = new JPanel(); JPanel xPanel = new JPanel(); JPanel yPanel = new JPanel(); JPanel zPanel = new JPanel(); JPanel ansPanel = new JPanel(); main.add(xPanel); main.add(yPanel); main.add(zPanel); main.add(ansPanel); //create and initialize JLabels here Celsius = new JTextField("0", 5); Fahrenheit = new JTextField("32", 4); Kelvin = new JTextField("273.15", 5); JLabel celLabel = new JLabel("Celsius:"); JLabel fahLabel = new JLabel("Fahrenheit:"); JLabel kelLabel = new JLabel("Kelvin:"); EventHandler listener = new EventHandler(); Celsius.addActionListener(listener); Fahrenheit.addActionListener(listener); Kelvin.addActionListener(listener); main.setLayout(new GridLayout(3,1)); xPanel.add(celLabel); xPanel.add(Celsius); yPanel.add(fahLabel); yPanel.add(Fahrenheit); zPanel.add(kelLabel); zPanel.add(Kelvin); //paints the main panel to the jframe and //displays the jframe window.setContentPane(main); window.setSize(250,110); window.setLocation(100,100); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setVisible(true); }//end main method } //end class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote