Answered step by step
Verified Expert Solution
Link Copied!

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

I'm having trouble with running this program. The info is shared below.
package project3;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import tripcost.TripCost;
public class Project3 extends JFrame {
private final JTextField totalTripCostTxt;
private boolean isMiles;
private boolean isDolPerGallon;
private boolean isMilesPerGallon;
public Project3(){
setVisible("Trip Cost Estimator");
setSize(400,400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(9,3,5,5));
// Initialize the components
add(new JLabel("Distance:"));
JTextField distanceTxt = new JTextField();
distanceTxt.setHorizontalAlignment(SwingConstants.RIGHT);
add(distanceTxt);
isMiles = true;
JComboBox distanceUnitCmbx = new JComboBox>(new String[]{"miles", "kilometers"});
distanceUnitCmbx.addActionListener(a ->
isMiles = distanceUnitCmbx.getSelectedItem().equals("miles"));
add(distanceUnitCmbx);
add(new JLabel("Gasoline Cost:"));
JTextField gasCostTxt = new JTextField();
gasCostTxt.setHorizontalAlignment(SwingConstants.RIGHT);
add(gasCostTxt);
isDolPerGallon = true;
JComboBox gasolineCostUnitComboBox = new JComboBox>(new String[]{"dollars/gal", "dollars/liter"});
gasolineCostUnitComboBox.addActionListener(a ->
isDolPerGallon = gasolineCostUnitComboBox.getSelectedItem().equals("dollars/gal"));
add(gasolineCostUnitComboBox);
JLabel gasMileageLbl = new JLabel("Gas Mileage");
add(gasMileageLbl);
JTextField gasMilTxt = new JTextField();
gasMilTxt.setHorizontalAlignment(SwingConstants.RIGHT);
add(gasMilTxt);
isMilesPerGallon = true;
JComboBox gasMileageUnitComboBox = new JComboBox>(new String[]{"miles/gallon","km/liter"});
gasMileageUnitComboBox.addActionListener(a ->
isMilesPerGallon = gasMileageUnitComboBox.getSelectedItem().equals("miles/gallon"));
add(gasMileageUnitComboBox);
add(new JLabel("Number Of Days:"));
JTextField numberOfDays = new JTextField();
numberOfDays.setHorizontalAlignment(SwingConstants.RIGHT);
add(numberOfDays);
add(new JLabel()); // empty space
add(new JLabel("Hotel Cost"));
JTextField hotelCostTxt = new JTextField();
hotelCostTxt.setHorizontalAlignment(SwingConstants.RIGHT);
add(hotelCostTxt);
add(new JLabel()); // empty space
add(new JLabel("Food Cost"));
JTextField foodCostTxt = new JTextField();
foodCostTxt.setHorizontalAlignment(SwingConstants.RIGHT);
add(foodCostTxt);
add(new JLabel()); // empty space
add(new JLabel("Attractions Cost"));
JTextField attractionsCostTxt = new JTextField();
attractionsCostTxt.setHorizontalAlignment(SwingConstants.RIGHT);
add(attractionsCostTxt);
add(new JLabel()); // empty space
add(new JLabel()); // empty space
JButton calculateButton = new JButton("Calculate");
calculateButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
try {
double distance = Double.parseDouble(distanceTxt.getText());
double gasolineCost = Double.parseDouble(gasCostTxt.getText());
double gasMileage = Double.parseDouble(gasMilTxt.getText());
double hotelCost = Double.parseDouble(hotelCostTxt.getText());
double foodCost = Double.parseDouble(foodCostTxt.getText());
double attractionsCost = Double.parseDouble(attractionsCostTxt.getText());
int numOfDays = Integer.parseInt(numberOfDays.getText());
TripCost tripCost = new TripCost(distance, gasolineCost, gasMileage, hotelCost,
foodCost, numOfDays, attractionsCost, isMiles, isDolPerGallon, isMilesPerGallon);
totalTripCostTxt.setText(String.format("$%.2f", tripCost.calculateTotalCost()));
} catch (NumberFormatException ex){
JOptionPane.showMessageDialog(null, "Invalid input. Please enter valid numbers.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
add(calculateButton);
add(new JLabel()); // empty space
add(new JLabel("Total Trip Cost"));
totalTripCostTxt = new JTextField("0.0");
totalTripCostTxt.setHorizontalAlignment(SwingConstants.RIGHT);
totalTripCostTxt.setEditable(false);
add(totalTripCostTxt);
}
public static void main(String[] args){
SwingUtilities.invokeLate
(It is supposed to display the image below)
Please Help. Thank you!
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions