Question: Intro to Java Convert the code below as an Applet *It is okay if the Applet can only be executed in AppletViewer* import javax.swing.*; import

Intro to Java

Convert the code below as an Applet

*It is okay if the Applet can only be executed in AppletViewer*

import javax.swing.*; import java.awt.event.*;

class MilesPerGallonCalculator extends JFrame

{ private JPanel panel; private JLabel messageLabel_gallons; private JLabel messageLabel_miles; private JTextField gallonsTextField; private JTextField milesTextField; private JButton mpgButton; private final int WINDOW_WIDTH = 400; private final int WINDOW_HEIGHT = 400;

public MilesPerGallonCalculator ()

{ setTitle ("Miles-Per-Gallon (MPG) Calculator");

setSize (WINDOW_WIDTH, WINDOW_HEIGHT);

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

buildPanel (); add (panel); setVisible(true);

}

private void buildPanel ()

{ messageLabel_gallons = new JLabel ("Enter number of gallons"); gallonsTextField = new JTextField (10);

messageLabel_miles = new JLabel ("Enter number of miles"); milesTextField = new JTextField (10);

mpgButton = new JButton ("Calculate MPG"); mpgButton.addActionListener (new CalculateMPG());

panel = new JPanel (); panel.add (messageLabel_gallons); panel.add (gallonsTextField); panel.add (messageLabel_miles); panel.add (milesTextField); panel.add (mpgButton);

}

public class CalculateMPG implements ActionListener { public void actionPerformed (ActionEvent e)

{ String inputone; String inputtwo; double mpg;

inputone = gallonsTextField.getText (); inputtwo = milesTextField.getText (); mpg = (Double.parseDouble (inputtwo) /Double.parseDouble (inputone));

JOptionPane.showMessageDialog (null, "The Miles-Per-Gallon (MPG) is:" + mpg);

}

}

} *******************************************************************************

public class MPGCalculatorApp { public static void main (String[] args) { MilesPerGallonCalculator kc = new MilesPerGallonCalculator();

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!