Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I ' m having trouble with running this program. The info is shared below. package project 3 ; import java.awt. * ; import java.awt.event.ActionEvent; import
Im having trouble with running this program. The info is shared below.
package project;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import tripcost.TripCost;
public class Project extends JFrame
private final JTextField totalTripCostTxt;
private boolean isMiles;
private boolean isDolPerGallon;
private boolean isMilesPerGallon;
public Project
setVisibleTrip Cost Estimator";
setSize;
setDefaultCloseOperationJFrameDISPOSEONCLOSE;
setLayoutnew GridLayout;
Initialize the components
addnew JLabelDistance:;
JTextField distanceTxt new JTextField;
distanceTxt.setHorizontalAlignmentSwingConstantsRIGHT;
adddistanceTxt;
isMiles true;
JComboBox distanceUnitCmbx new JComboBoxnew Stringmiles "kilometers";
distanceUnitCmbx.addActionListenera
isMiles distanceUnitCmbx.getSelectedItemequalsmiles;
adddistanceUnitCmbx;
addnew JLabelGasoline Cost:";
JTextField gasCostTxt new JTextField;
gasCostTxt.setHorizontalAlignmentSwingConstantsRIGHT;
addgasCostTxt;
isDolPerGallon true;
JComboBox gasolineCostUnitComboBox new JComboBoxnew Stringdollarsgal "dollarsliter;
gasolineCostUnitComboBox.addActionListenera
isDolPerGallon gasolineCostUnitComboBox.getSelectedItemequalsdollarsgal;
addgasolineCostUnitComboBox;
JLabel gasMileageLbl new JLabelGas Mileage";
addgasMileageLbl;
JTextField gasMilTxt new JTextField;
gasMilTxt.setHorizontalAlignmentSwingConstantsRIGHT;
addgasMilTxt;
isMilesPerGallon true;
JComboBox gasMileageUnitComboBox new JComboBoxnew Stringmilesgallonkmliter;
gasMileageUnitComboBox.addActionListenera
isMilesPerGallon gasMileageUnitComboBox.getSelectedItemequalsmilesgallon;
addgasMileageUnitComboBox;
addnew JLabelNumber Of Days:";
JTextField numberOfDays new JTextField;
numberOfDays.setHorizontalAlignmentSwingConstantsRIGHT;
addnumberOfDays;
addnew JLabel; empty space
addnew JLabelHotel Cost";
JTextField hotelCostTxt new JTextField;
hotelCostTxt.setHorizontalAlignmentSwingConstantsRIGHT;
addhotelCostTxt;
addnew JLabel; empty space
addnew JLabelFood Cost";
JTextField foodCostTxt new JTextField;
foodCostTxt.setHorizontalAlignmentSwingConstantsRIGHT;
addfoodCostTxt;
addnew JLabel; empty space
addnew JLabelAttractions Cost";
JTextField attractionsCostTxt new JTextField;
attractionsCostTxt.setHorizontalAlignmentSwingConstantsRIGHT;
addattractionsCostTxt;
addnew JLabel; empty space
addnew JLabel; empty space
JButton calculateButton new JButtonCalculate;
calculateButton.addActionListenernew ActionListener
@Override
public void actionPerformedActionEvent e
try
double distance Double.parseDoubledistanceTxtgetText;
double gasolineCost Double.parseDoublegasCostTxtgetText;
double gasMileage Double.parseDoublegasMilTxtgetText;
double hotelCost Double.parseDoublehotelCostTxtgetText;
double foodCost Double.parseDoublefoodCostTxtgetText;
double attractionsCost Double.parseDoubleattractionsCostTxtgetText;
int numOfDays Integer.parseIntnumberOfDaysgetText;
TripCost tripCost new TripCostdistance gasolineCost, gasMileage, hotelCost,
foodCost, numOfDays, attractionsCost, isMiles, isDolPerGallon, isMilesPerGallon;
totalTripCostTxt.setTextStringformat$f tripCost.calculateTotalCost;
catch NumberFormatException ex
JOptionPane.showMessageDialognull "Invalid input. Please enter valid numbers.", "Error", JOptionPane.ERRORMESSAGE;
;
addcalculateButton;
addnew JLabel; empty space
addnew JLabelTotal Trip Cost";
totalTripCostTxt new JTextField;
totalTripCostTxt.setHorizontalAlignmentSwingConstantsRIGHT;
totalTripCostTxt.setEditablefalse;
addtotalTripCostTxt;
public static void mainString args
SwingUtilities.invokeLate
It is supposed to display the image below
Please Help. Thank you!
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