JButton user input problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: JButton user input problem

  1. #1
    Join Date
    Oct 2011

    JButton 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:

    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 );
    	    // call buildPanel() method and add panel to frame
    	    // 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( days );
    		    panel.add( airfare);
    		    panel.add( fees );
    		    panel.add( miles );
    		    panel.add( parkfees );
    		    panel.add( taxi );
    		    panel.add( regfees );
    		    panel.add( lodg );
    	 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
    Join Date
    May 2006

    Re: JButton user input problem

    Please post the actual compiler error messages
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)