Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Action Listener?user input problem

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Action Listener?user input problem

    Hi, I have alot of confusion with a program I am supposed to write. The GUI App is supposed to take information from the user, that information is: Number of days on trip, amount of airfare, amount of car rental fees, amount of miles driven if only driven in private car, amount of parking fees, amount of taxi charges, conference or seminar registration fees, and lodging charges.

    Now the companies reimbursement policy is as follows:
    37$ per day for meals
    up to 10$ dollars a day for parking fees
    up to 20 $ a day for taxi charges
    up to 95$ dollars a day for lodging
    and .27$ per mile driven.

    Now here is what I am supposed to calculate:

    1.Total expenses by the business person
    2.total allowable expenses for the trip(what the company will reimburse for trip depending on how many days it was)
    3.the excess amount that must be paid by business person if there is any
    4.the amount saved by business person if the expenses are under the total amount allowed

    The problem I am recieving is it is telling me my int values have to be declared but i want to use the int values from the user inputs to calcualte the things i have to calculate. I think I have the Buttons set up correctly but its telling me my values are never read. I need to take the user input given and calc. it accordingly. Also i beleive my 4th button is wrong I am kind of confused on how to write it. Here is what i have:

    HTML Code:
    package Travel_Expenses;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Expenses extends JFrame {
    	private JTextField days;
    	private JButton button1;
    	private JButton button2;
    	private JButton button3;
    	private JButton button4;
    	private JLabel messageLabel;
    	private JLabel gmessageLabel;
    	private JLabel cmessageLabel;
    	private JLabel dmessageLabel;
    	private JLabel emessageLabel;
    	private JLabel fmessageLabel;
    	private JLabel rmessageLabel;
    	private JLabel xmessageLabel;
    	private JTextField airfare;
    	private JTextField fees;
    	private JTextField miles;
    	private JTextField parkfees;
    	private JTextField taxi;
    	private JTextField regfees;
    	private JTextField lodg;
    	private JPanel panel;
    	
    	public static void main(String[] args) {
    	    JFrame frame = new Expenses();
    	    
    
    	  }
    	public Expenses(){
    		setLayout(new GridLayout(1, 3));
    		
    
    	    // call set methods to customize frame
    	    setTitle( "Expenses");
    	    setSize( 600, 400);
    	    setLocation( 500, 300 );
    	    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    	    // call buildPanel() method and add panel to frame
    	    buildPanel();
    	    add(panel);
    
    	    // display the frame
    	    setVisible( true );
    
    	  }
    	 private void buildPanel() {
    
    		    // Instantiate panel and GUI Components
    		    panel = new JPanel();
    		    messageLabel= new JLabel("How many days was the trip");
    		    days = new JTextField(10);
    		    gmessageLabel= new JLabel("How much was the airfare");
    		    airfare = new JTextField(10);
    		    cmessageLabel= new JLabel("How muchw ere the car rental fees");
    		     fees= new JTextField(10);
    		     dmessageLabel= new JLabel("How many miles driven if used private car");
    		    miles = new JTextField(10);
    		    emessageLabel= new JLabel("How much were parking fees");
    		    parkfees = new JTextField(10);
    		    fmessageLabel= new JLabel("How much were taxi charges");
    		    taxi = new JTextField(10);
    		    rmessageLabel= new JLabel("How much were seminar or conference registration fees");
    		    regfees = new JTextField(10);
    		    xmessageLabel= new JLabel("How much were lodging charges");
    		    lodg = new JTextField(10);
    		    button1 = new JButton("Total Expenses");
    		      button2 = new JButton("total allowable expenses");
    		      button3 = new JButton("excess amount owed");
    		      button4 = new JButton("amount saved");
    		      
    		      button1.addActionListener(new ButtonListener());
    		      button2.addActionListener(new ButtonListener());
    		      button3.addActionListener(new ButtonListener());
    		      button4.addActionListener(new ButtonListener());
    		   
    		    panel.add(messageLabel);
    		    panel.add( days );
    		    panel.add(gmessageLabel);
    		    panel.add( airfare);
    		    panel.add(cmessageLabel);
    		    panel.add( fees );
    		    panel.add(dmessageLabel);
    		    panel.add( miles );
    		    panel.add(emessageLabel);
    		    panel.add( parkfees );
    		    panel.add(fmessageLabel);
    		    panel.add( taxi );
    		    panel.add(rmessageLabel);
    		    panel.add( regfees );
    		    panel.add(xmessageLabel);
    		    panel.add( lodg );
    		    panel.add(button1);
    		      panel.add(button2);
    		      panel.add(button3);
    		      panel.add(button4);
    		      
    	 }
    	 private class ButtonListener implements ActionListener
    	   {
    		 public void actionPerformed(ActionEvent e)
    	      {int days;
    	      int airfare;
    	      int fees;
    	      int miles;
    	      int parkfees;
    	      int taxi;
    	      int regfees;
    	      int lodg;
    	      int meal=37;
    	      int park=10;
    	      int charg=20;
    	      int lodgcharg=95;
    	      double carmile=.27;
    	         // Get the action command.
    	        String actionCommand = e.getActionCommand();
    
    	         // Determine which button was clicked and display
    	         // a message.
    	         if (actionCommand.equals("button1"))
    	         {
    	          JOptionPane.showMessageDialog(null, "days"+"airfare"+"fees"+"miles"+"parkfees"+"taxi"+"regfees"+"lodg");
    	         }
    	        else if (actionCommand.equals("button2"))
    	         {
    	          JOptionPane.showMessageDialog(null, "meal*days+park*days+charg*days+lodgcharg*days+carmile*miles");
    	         }
    	         else if (actionCommand.equals("button3"))
    	         {
    	            JOptionPane.showMessageDialog(null, "(days+airfare+fees+miles+parkfees+taxi+regfees+lodg)-(meal*days+park*days+charg*days+lodgcharg*days+carmile*miles)");
    	         
    	         }
    	         else if (actionCommand.equals("button4"))
    	         {
    	            JOptionPane.showMessageDialog(null,"(days+airfare+fees+miles+parkfees+taxi+regfees+lodg)-(meal*days+park*days+charg*days+lodgcharg*days+carmile*miles)" );
    	         }
    	      }
    	   }
    }


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Wales
    Posts
    4
    My Mood
    Cynical
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Action Listener?user input problem

    Hi
    What are u trying to achieve here as u never seem to set int days. Also the calculation in JOptionPane.showMessageDialog(null,"days+airfare+f ees+miles+parkfees+taxi+regfees+lodg)-(meal*days+park*days+charg*days+lodgcharg*days+car mile*miles)" );
    will never happen as its inside speak marks and is therefore a string which will get outputted as typed.

    This might be deliberate but im unclear what your trying to do

Similar Threads

  1. Problem with JText Fields and using action listener
    By toble in forum AWT / Java Swing
    Replies: 2
    Last Post: October 27th, 2010, 04:44 PM
  2. converter program, problem with action listener
    By robertson.basil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 05:44 AM
  3. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  4. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM