5 Replies - 3016 Views - Last Post: 08 August 2011 - 06:55 AM Rate Topic: -----

#1 mason10510  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-August 11

creating my first GUI application

Posted 07 August 2011 - 02:39 PM

Hello,

I've seen some other posts out here regarding the same assignment, but I'm not following. I'm a newbie, in Java I. I've finally gotten my window/panel to appear, but I'm having trouble trying to wrap my brain around the calcbutton and how to program it. I've pasted my code inside the brackets at instructed [ code ]. Here is the assignment-any help is appreciated...and again I apologize if my efforts offend your trade-

Create a GUI application that calculates and displays the total travel expenses of a business person on a trip. Here is the information that the user must provide:

- number of days on the trip
-amount of airfare, if any
-amount of car rental fees, if any
-number of miles driven, if a private vehicle was used
-Amount of parking fees, if any
-Amount of tax charges, if any
-Conference or seminar registration fees, if any
-Lodging charges per night

The company reimburses travel expenses according to the following policy;
-$37 per day for meals
-Parking fees up to $10.00 per day
-Taxi charges up to $20 per day
-Lodging charges up to $95.00 per day
-If a private vehicle is used, $.27 per mile driven

The application should calculate and display the following
-Total expenses incurred by the business person
-The total allowable expenses for the trip
-The excess that must be paid by the business person, if any
-The amount saved by the business person if the expenses are under the total allowed



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

/**
* The TravelExpense class creates the GUI for the
* Travel Expenses application.
*/

public class TravelExpense extends JFrame
{

private JPanel panel; 
private JLabel messageLabel_numDaysOnTripLabel;
private JLabel messageLabel_amountAirfairLabel;
private JLabel messageLabel_amountCarRentalLabel;
private JLabel messageLabel_milesDrivenLabel;
private JLabel messageLabel_parkingFeesLabel;
private JLabel messageLabel_taxiFeesLabel;
private JLabel messageLabel_confRegLabel;
private JLabel messageLabel_lodgingChargesPerNightLabel;


private JTextField TextField_numDaysOnTripTextField;
private JTextField TextField_amountAirfairTextField;
private JTextField TextField_amountCarRentalTextField;
private JTextField TextField_milesDrivenTextField;
private JTextField TextField_parkingFeesTextField;
private JTextField TextField_taxiFeesTextField;
private JTextField TextField_confRegTextField;
private JTextField TextField_lodgingChargesPerNightTextField;


private JButton resetButton;
private JButton calcButton;


private double mealsAmount = 37.00; 
private double parkingFeesReimbursed = 10.00;
private double taxiChargesReimbursed = 20.00; 
private double lodgingChargesReimbursed = 95.00; 
private double prVechiclePerMileReimbursed = 0.27; 
private final int WINDOW_WIDTH = 375;
private final int WINDOW_HEIGHT = 250;


public TravelExpense()
{

setTitle("Travel Expense");



setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




buildpanel();



add(panel);




setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setVisible(true);
}



private void buildpanel()
{

	messageLabel_numDaysOnTripLabel = new JLabel("Number of days on trip: ");
	messageLabel_amountAirfairLabel = new JLabel("Amount of airfair: ");
	messageLabel_amountCarRentalLabel = new JLabel("Amount of car rental: ");
	messageLabel_milesDrivenLabel = new JLabel("Miles driven: ");
	messageLabel_parkingFeesLabel = new JLabel("Parking Fees: ");
	messageLabel_taxiFeesLabel = new JLabel("Taxi fees: ");
	messageLabel_confRegLabel = new JLabel("Conference or Seminar registration fees: ");
	messageLabel_lodgingChargesPerNightLabel = new JLabel("Lodging charges per night: ");


	TextField_numDaysOnTripTextField = new JTextField(3);
	TextField_amountAirfairTextField = new JTextField(8);
	TextField_amountCarRentalTextField = new JTextField(8);
	TextField_milesDrivenTextField = new JTextField(4);
	TextField_parkingFeesTextField = new JTextField(6);
	TextField_taxiFeesTextField = new JTextField(6);
	TextField_confRegTextField = new JTextField(8);
	TextField_lodgingChargesPerNightTextField = new JTextField(6);

// Create a panel 
panel = new JPanel();




panel.add(messageLabel_numDaysOnTripLabel);
panel.add(TextField_numDaysOnTripTextField);
panel.add(messageLabel_amountAirfairLabel);
panel.add(TextField_amountAirfairTextField);
panel.add(messageLabel_amountCarRentalLabel);
panel.add(TextField_amountCarRentalTextField);
panel.add(messageLabel_milesDrivenLabel);
panel.add(TextField_milesDrivenTextField);
panel.add(messageLabel_parkingFeesLabel);
panel.add(TextField_parkingFeesTextField);
panel.add(messageLabel_taxiFeesLabel);
panel.add(TextField_taxiFeesTextField);
panel.add(messageLabel_confRegLabel);
panel.add(TextField_confRegTextField);
panel.add(messageLabel_lodgingChargesPerNightLabel);
panel.add(TextField_lodgingChargesPerNightTextField);


calcButton = new JButton("Calculate");


calcButton.addActionListener(new CalcButtonListener());


resetButton = new JButton("Reset");


panel.add(resetButton);
panel.add(calcButton);


}


private class CalcButtonListener implements ActionListener
{
// Declare variables 
String input; 
int days; 
double air; 
double carRental; 
double miles; 
double parking; 
double taxi; 
double confReg; 
double lodging; 
double mealsAmount;

			public void actionPerformed(ActionEvent e)
				{
				//Declare variables  
				double actualExpenses;
				 double milesExpenses;
				double allowableExpenses;
				double excessAir;
				double excessCarRental;
				double excessParking;
				double excessTaxi;
				double excessLodging;
				double excessAmountTotal;
				double amountSaved;
				double paidBackAmount;


					DecimalFormat dollar = new DecimalFormat("$#,##0.00");
					}}

private void getData()

{

days = Integer.parseInt(numDaysOnTripTextField.getText());

air = Double.parseDouble(amountAirfairTextField.getText());

carRental = Double.parseDouble(amountCarRentalTextField.getText());

miles = Double.parseDouble(milesDrivenTextField.getText());

parking = Double.parseDouble(parkingFeesTextField.getText());

taxi = Double.parseDouble(taxiFeesTextField.getText());

confReg = Double.parseDouble(confRegTextField.getText());

lodging = Double.parseDouble(lodgingChargesPerNightTextField.getText());

private class ResetButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
    {
	TextField_numDaysOnTripTextField.setText("");
	TextField_amountAirfairTextField.setText("");
	TextField_amountCarRentalTextField.setText("");
	TextField_milesDrivenTextField.setText("");
	TextField_parkingFeesTextField.setText("");
	TextField_taxiFeesTextField.setText("");
	TextField_confRegTextField.setText("");
	TextField_lodgingChargesPerNightTextField.setText("");
}


    }
    
public static void main(String[] args)
{
new TravelExpense();
}}
    



Edited by macosxnerd101: Please use code tags. Like so: :code:.

Is This A Good Question/Topic? 0
  • +

Replies To: creating my first GUI application

#2 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: creating my first GUI application

Posted 07 August 2011 - 03:04 PM

don't paste your code were it says code :)

as for your code you are on the right track for setting up your variables so that you can get the values from your text fields.

i do see a few things wrong though right off.

  private void getData()
  {

    days = Integer.parseInt(numDaysOnTripTextField.getText());
    air = Double.parseDouble(amountAirfairTextField.getText());
    carRental = Double.parseDouble(amountCarRentalTextField.getText());
    miles = Double.parseDouble(milesDrivenTextField.getText());
    parking = Double.parseDouble(parkingFeesTextField.getText());
    taxi = Double.parseDouble(taxiFeesTextField.getText());
    confReg = Double.parseDouble(confRegTextField.getText());
    lodging = Double.parseDouble(lodgingChargesPerNightTextField.getText());
  }



although the code is technically correct for what you are trying to do, it is not in the correct location. Those variables that you are trying to access are not class variables. The only location you have them is in your calcButtonListener. Java will not share variable names unless you tell it too. what i would do is move that do a different part of your code like this.

  private class CalcButtonListener implements ActionListener
  {
    // Declare variables 
    String input;
    int days;
    double air;
    double carRental;
    double miles;
    double parking;
    double taxi;
    double confReg;
    double lodging;
    double mealsAmount;

    @Override
    public void actionPerformed(ActionEvent e)
    {
      //Declare variables 
      double actualExpenses;
      double milesExpenses;
      double allowableExpenses;
      double excessAir;
      double excessCarRental;
      double excessParking;
      double excessTaxi;
      double excessLodging;
      double excessAmountTotal;
      double amountSaved;
      double paidBackAmount;

      DecimalFormat dollar = new DecimalFormat("$#,##0.00");
      days = Integer.parseInt(numDaysOnTripTextField.getText());
      air = Double.parseDouble(amountAirfairTextField.getText());
      carRental = Double.parseDouble(amountCarRentalTextField.getText());
      miles = Double.parseDouble(milesDrivenTextField.getText());
      parking = Double.parseDouble(parkingFeesTextField.getText());
      taxi = Double.parseDouble(taxiFeesTextField.getText());
      confReg = Double.parseDouble(confRegTextField.getText());
      lodging = Double.parseDouble(lodgingChargesPerNightTextField.getText());

    }
  }



and that won't work unless you make your TextFields part of the class variables like this.

public class TravelExpense extends JFrame
{
  private JTextField milesDrivenTextField;
  //and all the other JTextFields that you are trying to getText from
  //rest of your code here



so you have a long way to go, please post your code inbetween the tags, don't delete the word code and paste in your code.
Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,094
  • Joined: 27-December 08

Re: creating my first GUI application

Posted 07 August 2011 - 03:17 PM

Here's the way you are going to want to go about the calculateButton. To start, you already know you need an ActionListener. In the actionPerformed() method, you should get the information from the GUI Components like the number of miles, airfare, etc. Remember that objects like JTextField have a getText() method that returns a String. So you will have to parse it to the appropriate type (double, int).

After it calculates the appropriate values, it should update the GUI to display them. You have JLabels already, so using those you can invoke their setText() method to display them.
Was This Post Helpful? 0
  • +
  • -

#4 mason10510  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-August 11

Re: creating my first GUI application

Posted 07 August 2011 - 04:15 PM

when I moved the location to action method, I get a critical exception and it won't run anything now, because of my main.-trying to follow the methods of this text and understand your answers at the same time-I'm about over Java, and kudos to all of you who have mastered it. I'm just not following Gaddis.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

/**
* The TravelExpense class creates the GUI for the
* Travel Expenses application.
*/

public class TravelExpense extends JFrame
{

private JPanel panel; 
private JLabel messageLabel_numDaysOnTripLabel;
private JLabel messageLabel_amountAirfairLabel;
private JLabel messageLabel_amountCarRentalLabel;
private JLabel messageLabel_milesDrivenLabel;
private JLabel messageLabel_parkingFeesLabel;
private JLabel messageLabel_taxiFeesLabel;
private JLabel messageLabel_confRegLabel;
private JLabel messageLabel_lodgingChargesPerNightLabel;


private JTextField TextField_numDaysOnTripTextField;
private JTextField TextField_amountAirfairTextField;
private JTextField TextField_amountCarRentalTextField;
private JTextField TextField_milesDrivenTextField;
private JTextField TextField_parkingFeesTextField;
private JTextField TextField_taxiFeesTextField;
private JTextField TextField_confRegTextField;
private JTextField TextField_lodgingChargesPerNightTextField;


private JButton resetButton;
private JButton calcButton;


private double mealsAmount = 37.00; 
private double parkingFeesReimbursed = 10.00;
private double taxiChargesReimbursed = 20.00; 
private double lodgingChargesReimbursed = 95.00; 
private double prVechiclePerMileReimbursed = 0.27; 
private final int WINDOW_WIDTH = 375;
private final int WINDOW_HEIGHT = 250;


public TravelExpense()
{

setTitle("Travel Expense");



setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




buildpanel();



add(panel);




setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setVisible(true);
}



private void buildpanel()
{

	messageLabel_numDaysOnTripLabel = new JLabel("Number of days on trip: ");
	messageLabel_amountAirfairLabel = new JLabel("Amount of airfair: ");
	messageLabel_amountCarRentalLabel = new JLabel("Amount of car rental: ");
	messageLabel_milesDrivenLabel = new JLabel("Miles driven: ");
	messageLabel_parkingFeesLabel = new JLabel("Parking Fees: ");
	messageLabel_taxiFeesLabel = new JLabel("Taxi fees: ");
	messageLabel_confRegLabel = new JLabel("Conference or Seminar registration fees: ");
	messageLabel_lodgingChargesPerNightLabel = new JLabel("Lodging charges per night: ");


	TextField_numDaysOnTripTextField = new JTextField(3);
	TextField_amountAirfairTextField = new JTextField(8);
	TextField_amountCarRentalTextField = new JTextField(8);
	TextField_milesDrivenTextField = new JTextField(4);
	TextField_parkingFeesTextField = new JTextField(6);
	TextField_taxiFeesTextField = new JTextField(6);
	TextField_confRegTextField = new JTextField(8);
	TextField_lodgingChargesPerNightTextField = new JTextField(6);

// Create a panel 
panel = new JPanel();




panel.add(messageLabel_numDaysOnTripLabel);
panel.add(TextField_numDaysOnTripTextField);
panel.add(messageLabel_amountAirfairLabel);
panel.add(TextField_amountAirfairTextField);
panel.add(messageLabel_amountCarRentalLabel);
panel.add(TextField_amountCarRentalTextField);
panel.add(messageLabel_milesDrivenLabel);
panel.add(TextField_milesDrivenTextField);
panel.add(messageLabel_parkingFeesLabel);
panel.add(TextField_parkingFeesTextField);
panel.add(messageLabel_taxiFeesLabel);
panel.add(TextField_taxiFeesTextField);
panel.add(messageLabel_confRegLabel);
panel.add(TextField_confRegTextField);
panel.add(messageLabel_lodgingChargesPerNightLabel);
panel.add(TextField_lodgingChargesPerNightTextField);


calcButton = new JButton("Calculate");


calcButton.addActionListener(new CalcButtonListener());


resetButton = new JButton("Reset");


panel.add(resetButton);
panel.add(calcButton);


}


private class CalcButtonListener implements ActionListener
{
				// Declare variables 
				String input; 
				int days; 
				double air; 
				double carRental; 
				double miles; 
				double parking; 
				double taxi; 
				double confReg; 
				double lodging; 
				double mealsAmount;

public void actionPerformed(ActionEvent e)
				{
				//Declare variables  
				double actualExpenses;
				 double milesExpenses;
				double allowableExpenses;
				double excessAir;
				double excessCarRental;
				double excessParking;
				double excessTaxi;
				double excessLodging;
				double excessAmountTotal;
				double amountSaved;
				double paidBackAmount;
				double totallodging=days*lodging;
				double totalmeals=days*mealsAmount;
				double totalexpenses=miles+parking+taxi+carRental+air+confReg+totallodging+totalmeals;
				
				
				
					DecimalFormat dollar = new DecimalFormat("$#,##0.00");
				
				    days = Integer.parseInt(TextField_numDaysOnTripTextField.getText());
				
				    air = Double.parseDouble(TextField_amountAirfairTextField.getText());
			
				    carRental = Double.parseDouble(TextField_amountCarRentalTextField.getText());
			
				    miles = Double.parseDouble(TextField_milesDrivenTextField.getText());
				
				    parking = Double.parseDouble(TextField_parkingFeesTextField.getText());
			
				    taxi = Double.parseDouble(TextField_taxiFeesTextField.getText());
			
				    confReg = Double.parseDouble(TextField_confRegTextField.getText());
			
				    lodging = Double.parseDouble(TextField_lodgingChargesPerNightTextField.getText());
				  JOptionPane.showMessageDialog(null,"Total Expenses are:"+ totalexpenses);  
    }
    
public  void main(String[] args){
	
	new TravelExpense();
	}
}



}
    

 

Was This Post Helpful? 0
  • +
  • -

#5 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: creating my first GUI application

Posted 07 August 2011 - 04:30 PM

totalexpenses=miles+parking+taxi+carRental+air+confReg+totallodging+totalmeals;



you can't have that before you declare what those variables are. Java is a *little* bit proceedural, and you are thinking it is an equation. Java has to know what each of the variables are before it can run an equation. (basically move that to the end of your actionlistener.

also you are missing some of the textfield declarations in your code. I'm suprised your not getting null point exceptions and variables don't exist problems.
Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,094
  • Joined: 27-December 08

Re: creating my first GUI application

Posted 08 August 2011 - 06:55 AM

If you continue to encounter errors after fixing what immeraufdemhund pointed out, please post them along with your revised code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1