import javax.swing.*; import java.awt.*; import java.awt.event.*; public class m
ID: 3657525 • Letter: I
Question
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class mpgGUIV3 extends JFrame implements ActionListener{ //properties private Container frameContent; private GridLayout panelLayout; private JLabel milesLabel, gallonsLabel, mpgLabel; private JTextField milesField, gallonsField; private JButton calcButton; private double miles, gallons, mpg; //for calculating results /* Construtor (default) * Purpose: create and place components, create event handler * Modifies: all properties */ public mpgGUIV3() { this.setTitle("MPG Calculator"); //set properties for this frame: ALSO DO THE PACK this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(200,200); //create the components frameContent=this.getContentPane(); panelLayout = new GridLayout(3,2); milesLabel = new JLabel("Enter miles:"); gallonsLabel = new JLabel("Enter gallons"); mpgLabel = new JLabel(); milesField = new JTextField(); gallonsField = new JTextField(); calcButton = new JButton("Calculate MPG"); //add the layout and components to the content pane //NOTE: order components added matters frameContent.setLayout(panelLayout); frameContent.add(milesLabel); frameContent.add(milesField); frameContent.add(gallonsLabel); frameContent.add(gallonsField); frameContent.add(calcButton); frameContent.add(mpgLabel); //create an event handler and add it to the button calcButton.addActionListener(this);//addActionListener //display the frame this.setVisible(true); }//constructor public void actionPerformed (ActionEvent event){//handler miles = Double.parseDouble(milesField.getText()); gallons = Double.parseDouble(gallonsField.getText()); if(miles < 0.0 || gallons <= 0.0) //error check mpg = 0; else mpg = miles/gallons; mpgLabel.setText("MPG is " + mpg); }//actionPerformed //main method to get things going public static void main(String args[]) { mpgGUIV3 MPGCalculator = new mpgGUIV3(); }//main }//mpgGUIExplanation / Answer
please format the code and paste ill help you use komodo edit
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.