Java Programming Write a KiloConverter class that convert ki

*Java Programming*

Write a KiloConverter class that convert kilometers into miles (mi = .6214 *km). This class must extend JFrame. Create one JPanel with a JLabel (to prompt the user to enter a distance in kilometers), a JTextField (to allow the user to enter the kilometers), a JButton (to do the calculation). Also, needs a private class that implements ActionListener (to actually do thecalculation) The user should be able to enter miles followed by the return key OR enter the miles and click the calculate button. Create a second JPanelwith an uneditable text field with a light gray background (to display the mileage), another JLabel, and another JButton (to exit). You will need a private class that implements ActionListener (to actually exit). Use the(default) BorderLayout to place the panel components in the window. Use the (default) FlowLayout for each panel. Provide a driver in a separate sourcefile to test your class.

Solution

import javax.swing.*;   

import java.awt.event.*;

public class KiloConverterWindow extends JFrame

{

   private JPanel panel;          

   private final int WINDOW_WIDTH = 310;

   private final int WINDOW_HEIGHT = 100;

   private JButton calcButton;

   private JLabel messageLabel;     

   private JTextField kiloTextField;

   public KiloConverterWindow()

   {

      setTitle(\"Kilometer Converter\");

      setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      buildPanel();

      add(panel);

      setVisible(true);

   }

   private void buildPanel()

   {

      messageLabel = new JLabel(\"Enter a distance \" +\"in kilometers\");

      kiloTextField = new JTextField(10);

      calcButton = new JButton(\"Calculate\");

      calcButton.addActionListener(new CalcButtonListener());

      panel = new JPanel();

      panel.add(messageLabel);

      panel.add(kiloTextField);

      panel.add(calcButton);

   }

   private class CalcButtonListener implements ActionListener

   {

      public void actionPerformed(ActionEvent e)

      {

         String input;

         double miles;

         input = kiloTextField.getText();

         System.out.println(\"Reading \" + input +\" from the text field.\");

         System.out.println(\"Converted value: \" +Double.parseDouble(input));

         miles = Double.parseDouble(input) * 0.6214;

         JOptionPane.showMessageDialog(null, input +\" kilometers is \" + miles + \" miles.\");

         System.out.println(\"Ready for the next input.\");

      }

   }

   public static void main(String[] args)

   {

      new KiloConverterWindow();

   }

}

*Java Programming* Write a KiloConverter class that convert kilometers into miles (mi = .6214 *km). This class must extend JFrame. Create one JPanel with a JLab
*Java Programming* Write a KiloConverter class that convert kilometers into miles (mi = .6214 *km). This class must extend JFrame. Create one JPanel with a JLab

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site