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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started