Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0

    Java help in formatting how my program is displaying


    my code isn't displaying right. It seems it is displaying my questionPanel horizontally instead of vertically. I have it set for a gridlayout of 7 rows and 2 cols but it is displaying straight across horizontally. I can't show it all on this posting. So I will post what I think is relevant and then if you want the whole file, please just let me know!! Thanks so much in advance!!


    package travelExpenses;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;


    public class TravelExpenses extends JFrame
    {
    private final double RENTALCAR = 40;
    private final double PARKING = 20;
    private final double FOOD = 40;
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu editMenu;
    private JMenuItem exit;
    private JMenuItem cut;
    private JMenuItem copy;
    private JMenuItem paste;
    private JLabel numDays;
    private JLabel airfareCost;
    private JLabel rentalCarCost;
    private JLabel parkingFees;
    private JLabel classFee;
    private JLabel lodgingFee;
    private JLabel foodCost;
    private JTextField numDaysF, airfareCostF, rentalCarCostF, parkingFeesF, classFeeF;
    private JTextField lodgingFeeF, foodCostF;
    private JPanel questionPanel;
    private JPanel buttonPanel;
    private JButton calcButton;
    private JButton exitButton;


    public TravelExpenses()
    {
    super("Travel Expenses");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridLayout(2,1));
    buildMenuBar();
    buildQuestionPanel();
    buildButtonPanel();



    add(questionPanel);
    add(buttonPanel);
    pack();
    setVisible(true);
    }



    public void buildMenuBar()
    {
    menuBar = new JMenuBar();
    fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    exit = new JMenuItem("Exit");
    exit.setMnemonic(KeyEvent.VK_E);
    exit.addActionListener(new ExitButtonListener());
    fileMenu.add(exit);
    editMenu = new JMenu("Edit");
    editMenu.setMnemonic(KeyEvent.VK_E);
    cut = new JMenuItem("Cut");
    cut.setMnemonic(KeyEvent.VK_X);
    copy = new JMenuItem("Copy");
    copy.setMnemonic(KeyEvent.VK_C);
    paste = new JMenuItem("Paste");
    paste.setMnemonic(KeyEvent.VK_P);
    editMenu.add(cut);
    editMenu.add(copy);
    editMenu.add(paste);
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    setJMenuBar(menuBar);
    }


    public void buildQuestionPanel()
    {
    questionPanel = new JPanel();
    setLayout(new GridLayout(7,2));
    numDays = new JLabel("Days on Trip");
    numDaysF = new JTextField(6);
    airfareCost = new JLabel("Airfare Cost");
    airfareCostF = new JTextField(6);
    rentalCarCost = new JLabel("Rental Car Cost");
    rentalCarCostF = new JTextField(6);
    parkingFees = new JLabel("Parking Cost");
    parkingFeesF = new JTextField(6);
    classFee = new JLabel("Conference / Training Cost");
    classFeeF = new JTextField(6);
    lodgingFee = new JLabel("Lodging Cost");
    lodgingFeeF = new JTextField(6);
    foodCost = new JLabel("Food Cost");
    foodCostF = new JTextField(6);



    questionPanel.add(numDays);
    questionPanel.add(numDaysF);
    questionPanel.add(airfareCost);
    questionPanel.add(airfareCostF);
    questionPanel.add(rentalCarCost);
    questionPanel.add(rentalCarCostF);
    questionPanel.add(parkingFees);
    questionPanel.add(parkingFeesF);
    questionPanel.add(classFee);
    questionPanel.add(classFeeF);
    questionPanel.add(lodgingFee);
    questionPanel.add(lodgingFeeF);
    questionPanel.add(foodCost);
    questionPanel.add(foodCostF);
    }



    public void buildButtonPanel()
    {
    calcButton = new JButton("Calculate Total");
    exitButton = new JButton("Exit");
    buttonPanel = new JPanel();
    calcButton.addActionListener(new CalcButtonListener());
    buttonPanel.add(calcButton);
    buttonPanel.add(exitButton);
    }


    private class CalcButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    double days, airfare, rentalCar, parking, classFee, lodging, food;
    double total, allowableTotal, overage;
    double rentalOverage, parkingOverage, foodOverage;
    double rentalAllowance, parkingAllowance, foodAllowance;
    String str;



    str = numDays.getText();
    days = Double.parseDouble(str);
    str = airfareCostF.getText();
    airfare = Double.parseDouble(str);
    str = rentalCarCostF.getText();
    rentalCar = Double.parseDouble(str);
    str = parkingFeesF.getText();
    parking = Double.parseDouble(str);
    str = classFeeF.getText();
    classFee = Double.parseDouble(str);
    str = lodgingFeeF.getText();
    lodging = Double.parseDouble(str);
    str = foodCostF.getText();
    food = Double.parseDouble(str);
    rentalAllowance = RENTALCAR * days;
    parkingAllowance = PARKING * days;
    foodAllowance = FOOD * days;
    total = airfare + rentalCar + parking + classFee + lodging + food;



    if (rentalCar > rentalAllowance)
    rentalOverage = rentalCar - rentalAllowance;
    else
    rentalOverage = 0;
    if (parking > parkingAllowance)
    parkingOverage = parking - parkingAllowance;
    else
    parkingOverage = 0;
    if (food > foodAllowance)
    foodOverage = food - foodAllowance;
    else
    foodOverage = 0;



    overage = rentalOverage + parkingOverage + foodOverage;
    allowableTotal = total - overage;



    JOptionPane.showMessageDialog(null, "Total Charges: $" + total + "\n\n" +
    "Non-Reimburstable Charges: $" + overage + "\n\n" +
    "Your reimburstable amount: $" + allowableTotal,
    "Total", JOptionPane.PLAIN_MESSAGE);
    }
    }


    private class ExitButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);



    }
    }

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


    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please edit the post and wrap the code in code tags to preserve formatting.

    Does the posted code compile, execute and show the problem? If not can you fix it so it does?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Ok I'm new to this. Can I ask you how to wrap the code in code tags? But ya, the code compiles and runs, etc. Just the displaying format is screwed up.

    Originally Posted by NormR
    Please edit the post and wrap the code in code tags to preserve formatting.

    Does the posted code compile, execute and show the problem? If not can you fix it so it does?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Put [code ] before the code (without the space)
    and [/code] after the code
    Should look like this when saved:
    Code:
    the code here
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Put [code ] before the code (without the space)
    and [/code] after the code
    Should look like this when saved:
    Code:
    the code here

    Code:
    package travelExpenses;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    
    public class TravelExpenses extends JFrame
    {
    private final double RENTALCAR = 40;
    private final double PARKING = 20;
    private final double FOOD = 40;
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu editMenu;
    private JMenuItem exit;
    private JMenuItem cut;
    private JMenuItem copy;
    private JMenuItem paste; 
    private JLabel numDays;
    private JLabel airfareCost;
    private JLabel rentalCarCost;
    private JLabel parkingFees;
    private JLabel classFee;
    private JLabel lodgingFee;
    private JLabel foodCost; 
    private JTextField numDaysF, airfareCostF, rentalCarCostF, parkingFeesF, classFeeF;
    private JTextField lodgingFeeF, foodCostF;
    private JPanel questionPanel;
    private JPanel buttonPanel;
    private JButton calcButton;
    private JButton exitButton;
    
    
    public TravelExpenses() 
    {
    super("Travel Expenses");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new GridLayout(2,1));
    buildMenuBar();
    buildQuestionPanel();
    buildButtonPanel();
    
    
    
    add(questionPanel);
    add(buttonPanel);
    pack();
    setVisible(true);
    }
    
    
    
    public void buildMenuBar()
    {
    menuBar = new JMenuBar(); 
    fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    exit = new JMenuItem("Exit");
    exit.setMnemonic(KeyEvent.VK_E);
    exit.addActionListener(new ExitButtonListener());
    fileMenu.add(exit);
    editMenu = new JMenu("Edit");
    editMenu.setMnemonic(KeyEvent.VK_E);
    cut = new JMenuItem("Cut");
    cut.setMnemonic(KeyEvent.VK_X);
    copy = new JMenuItem("Copy");
    copy.setMnemonic(KeyEvent.VK_C);
    paste = new JMenuItem("Paste");
    paste.setMnemonic(KeyEvent.VK_P);
    editMenu.add(cut);
    editMenu.add(copy);
    editMenu.add(paste);
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    setJMenuBar(menuBar);
    }
    
    
    public void buildQuestionPanel()
    { 
    questionPanel = new JPanel();
    setLayout(new GridLayout(7,2));
    numDays = new JLabel("Days on Trip");
    numDaysF = new JTextField(6);
    airfareCost = new JLabel("Airfare Cost");
    airfareCostF = new JTextField(6);
    rentalCarCost = new JLabel("Rental Car Cost");
    rentalCarCostF = new JTextField(6);
    parkingFees = new JLabel("Parking Cost");
    parkingFeesF = new JTextField(6);
    classFee = new JLabel("Conference / Training Cost");
    classFeeF = new JTextField(6);
    lodgingFee = new JLabel("Lodging Cost");
    lodgingFeeF = new JTextField(6);
    foodCost = new JLabel("Food Cost");
    foodCostF = new JTextField(6);
    
    
    
    questionPanel.add(numDays);
    questionPanel.add(numDaysF);
    questionPanel.add(airfareCost);
    questionPanel.add(airfareCostF);
    questionPanel.add(rentalCarCost);
    questionPanel.add(rentalCarCostF);
    questionPanel.add(parkingFees);
    questionPanel.add(parkingFeesF);
    questionPanel.add(classFee);
    questionPanel.add(classFeeF);
    questionPanel.add(lodgingFee);
    questionPanel.add(lodgingFeeF);
    questionPanel.add(foodCost);
    questionPanel.add(foodCostF); 
    }
    
    
    
    public void buildButtonPanel()
    {
    calcButton = new JButton("Calculate Total");
    exitButton = new JButton("Exit");
    buttonPanel = new JPanel();
    calcButton.addActionListener(new CalcButtonListener());
    buttonPanel.add(calcButton);
    buttonPanel.add(exitButton);
    }
    
    
    private class CalcButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    double days, airfare, rentalCar, parking, classFee, lodging, food;
    double total, allowableTotal, overage;
    double rentalOverage, parkingOverage, foodOverage;
    double rentalAllowance, parkingAllowance, foodAllowance;
    String str;
    
    
    
    str = numDays.getText();
    days = Double.parseDouble(str); 
    str = airfareCostF.getText();
    airfare = Double.parseDouble(str);
    str = rentalCarCostF.getText();
    rentalCar = Double.parseDouble(str);
    str = parkingFeesF.getText();
    parking = Double.parseDouble(str);
    str = classFeeF.getText();
    classFee = Double.parseDouble(str);
    str = lodgingFeeF.getText();
    lodging = Double.parseDouble(str);
    str = foodCostF.getText();
    food = Double.parseDouble(str);
    rentalAllowance = RENTALCAR * days;
    parkingAllowance = PARKING * days;
    foodAllowance = FOOD * days;
    total = airfare + rentalCar + parking + classFee + lodging + food;
    
    
    
    if (rentalCar > rentalAllowance)
    rentalOverage = rentalCar - rentalAllowance;
    else
    rentalOverage = 0; 
    if (parking > parkingAllowance)
    parkingOverage = parking - parkingAllowance;
    else
    parkingOverage = 0;
    if (food > foodAllowance)
    foodOverage = food - foodAllowance;
    else
    foodOverage = 0;
    
    
    
    overage = rentalOverage + parkingOverage + foodOverage;
    allowableTotal = total - overage;
    
    
    
    JOptionPane.showMessageDialog(null, "Total Charges: $" + total + "\n\n" + 
    "Non-Reimburstable Charges: $" + overage + "\n\n" +
    "Your reimburstable amount: $" + allowableTotal, 
    "Total", JOptionPane.PLAIN_MESSAGE); 
    }
    }
    
    
    private class ExitButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    
    
    
    }
    }
    
    public static void main(String[] args) 
    {
    new TravelExpenses();
    }
    
    
    }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    You got the code tags OK, but lost the formatting.
    Please format the code to have indentations for nested levels of code.
    The statements should NOT all start in the first column.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    You got the code tags OK, but lost the formatting.
    Please format the code to have indentations for nested levels of code.
    The statements should NOT all start in the first column.
    Ok how about this!?

    Code:
    package travelExpenses;
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.*;
    
    
    public class TravelExpenses extends JFrame
    {
    	private final double RENTALCAR = 40;
    	private final double PARKING = 20;
    	private final double FOOD = 40;
    	
    	//private QuestionPanel question;
    
    	private JMenuBar menuBar;
    	private JMenu fileMenu;
    	private JMenu editMenu;
    	private JMenuItem exit;
    	private JMenuItem cut;
    	private JMenuItem copy;
    	private JMenuItem paste;
    	
    	private JLabel numDays;
    	private JLabel airfareCost;
    	private JLabel rentalCarCost;
    	private JLabel parkingFees;
    	private JLabel classFee;
    	private JLabel lodgingFee;
    	private JLabel foodCost;
    	
    	private JTextField numDaysF, airfareCostF, rentalCarCostF, parkingFeesF, classFeeF;
    	private JTextField lodgingFeeF, foodCostF;
    	
    	private JPanel questionPanel;
    	private JPanel buttonPanel;
    	private JButton calcButton;
    	private JButton exitButton;
    
    	public TravelExpenses() 
    	{
    		super("Travel Expenses");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		setLayout(new GridLayout(2,1));
    		//setLayout(new BorderLayout());
    	
    		buildMenuBar();
    		buildQuestionPanel();
    		buildButtonPanel();
    		
    		add(questionPanel);
    		add(buttonPanel);
    		//add(questionPanel, BorderLayout.WEST);
    		//add(buttonPanel, BorderLayout.SOUTH);
    		
    		pack();
    		setVisible(true);
    		
    	}
    	
    	
    	public void buildMenuBar()
    	{
    		menuBar = new JMenuBar();
    		
    		fileMenu = new JMenu("File");
    		fileMenu.setMnemonic(KeyEvent.VK_F);
    		
    		exit = new JMenuItem("Exit");
    		exit.setMnemonic(KeyEvent.VK_E);
    		exit.addActionListener(new ExitButtonListener());
    	
    		fileMenu.add(exit);
    		
    		
    		editMenu = new JMenu("Edit");
    		editMenu.setMnemonic(KeyEvent.VK_E);
    		
    		cut = new JMenuItem("Cut");
    		cut.setMnemonic(KeyEvent.VK_X);
    		
    		copy = new JMenuItem("Copy");
    		copy.setMnemonic(KeyEvent.VK_C);
    		
    		paste = new JMenuItem("Paste");
    		paste.setMnemonic(KeyEvent.VK_P);
    		
    		editMenu.add(cut);
    		editMenu.add(copy);
    		editMenu.add(paste);
    		
    		
    		menuBar.add(fileMenu);
    		menuBar.add(editMenu);
    		
    		setJMenuBar(menuBar);
    	}
    
    	
    	public void buildQuestionPanel()
    	{
    		
    		questionPanel = new JPanel();
    		setLayout(new GridLayout(7,2));
    		
    		
    		numDays = new JLabel("Days on Trip");
    		numDaysF = new JTextField(6);
    		
    		airfareCost = new JLabel("Airfare Cost");
    		airfareCostF = new JTextField(6);
    		
    		rentalCarCost = new JLabel("Rental Car Cost");
    		rentalCarCostF = new JTextField(6);
    		
    		parkingFees = new JLabel("Parking Cost");
    		parkingFeesF = new JTextField(6);
    		
    		classFee = new JLabel("Conference / Training Cost");
    		classFeeF = new JTextField(6);
    		
    		lodgingFee = new JLabel("Lodging Cost");
    		lodgingFeeF = new JTextField(6);
    		
    		foodCost = new JLabel("Food Cost");
    		foodCostF = new JTextField(6);
    		
    		questionPanel.add(numDays);
    		questionPanel.add(numDaysF);
    		questionPanel.add(airfareCost);
    		questionPanel.add(airfareCostF);
    		questionPanel.add(rentalCarCost);
    		questionPanel.add(rentalCarCostF);
    		questionPanel.add(parkingFees);
    		questionPanel.add(parkingFeesF);
    		questionPanel.add(classFee);
    		questionPanel.add(classFeeF);
    		questionPanel.add(lodgingFee);
    		questionPanel.add(lodgingFeeF);
    		questionPanel.add(foodCost);
    		questionPanel.add(foodCostF);
    		
    	}
    	
    	
    	public void buildButtonPanel()
    	{
    		calcButton = new JButton("Calculate Total");
    		exitButton = new JButton("Exit");
    		buttonPanel = new JPanel();
    		
    		
    		calcButton.addActionListener(new CalcButtonListener());
    		buttonPanel.add(calcButton);
    		buttonPanel.add(exitButton);
    	
    	}
    	
    	
    	private class CalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			double days, airfare, rentalCar, parking, classFee, lodging, food;
    			double total, allowableTotal, overage;
    			double rentalOverage, parkingOverage, foodOverage;
    			double rentalAllowance, parkingAllowance, foodAllowance;
    			
    			String str;
    			
    			str = numDays.getText();
    			days = Double.parseDouble(str);
    			
    			str = airfareCostF.getText();
    			airfare = Double.parseDouble(str);
    			
    			str = rentalCarCostF.getText();
    			rentalCar = Double.parseDouble(str);
    			
    			str = parkingFeesF.getText();
    			parking = Double.parseDouble(str);
    			
    			str = classFeeF.getText();
    			classFee = Double.parseDouble(str);
    			
    			str = lodgingFeeF.getText();
    			lodging = Double.parseDouble(str);
    			
    			str = foodCostF.getText();
    			food = Double.parseDouble(str);
    			
    			rentalAllowance = RENTALCAR * days;
    			parkingAllowance = PARKING * days;
    			foodAllowance = FOOD * days;
    			
    			total = airfare + rentalCar + parking + classFee + lodging + food;
    	
    			
    			if (rentalCar > rentalAllowance)
    				rentalOverage = rentalCar - rentalAllowance;
    			else
    				rentalOverage = 0;
    			
    			if (parking > parkingAllowance)
    				parkingOverage = parking - parkingAllowance;
    			else
    				parkingOverage = 0;
    			
    		
    			if (food > foodAllowance)
    				foodOverage = food - foodAllowance;
    			else
    				foodOverage = 0;
    			
    			
    			overage = rentalOverage + parkingOverage + foodOverage;
    			allowableTotal = total - overage;
    			
    			
    			JOptionPane.showMessageDialog(null, "Total Charges: $" + total + "\n\n" + 
    												"Non-Reimburstable Charges: $" + overage + "\n\n" +
    												"Your reimburstable amount: $" + allowableTotal, 
    												"Total", JOptionPane.PLAIN_MESSAGE);
    			
    		}
    		
    		
    	}
    	
    	private class ExitButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    						
    		}
    	}
    	
    	
    	
    	public static void main(String[] args) 
    	{
    		new TravelExpenses();
    	}
    
    }
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Much better.
    The code sets the layout two times. Which one is the one you want to be used?

    It'd be better to have the method that creates and builds a panel to return the panel instead of using a global variable.
    Last edited by NormR; November 30th, 2012 at 03:26 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Much better.
    The code sets the layout two times. Which one is the one you want to be used?

    It'd be better to have the method that creates and builds a panel to return the panel instead of using a global variable.
    Ok what I was intending was to set the jframe to a grid layout and the questionPanel to a grid layout of 7 rows and 2 Columns so that it would display each question and the textField next to each questions and then the next row for the next question. Then add that panel to the jframe along with a buttonPanel to the jframe. I want something like this:

    JLabel Jtextfield
    JLabel Jtextfield
    JLabel Jtextfield Calculate
    JLabel Jtextfield
    JLabel Jtextfield
    JLabel Jtextfield
    JLabel Jtextfield



    And the calculate being in a different panel (the buttonPanel)

    Should I set the layout only for the question panel? Not the jframe?

    Also, so should I declare all the Jlabels, JLabels, and JPanels in the questionPanel instead at the top as a global variable?

    Thanks so much for all your help

    -Chris
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Should I set the layout only for the question panel? Not the jframe?
    You should set the layout for each container to be what you want it to be for that container.
    Check that the code is setting it for the desired container.

    should I declare all the Jlabels, JLabels, and JPanels in the questionPanel
    If you need references to any of them in other methods, then they will need to be global. If no other methods use them, they can be local.
    Last edited by NormR; November 30th, 2012 at 04:11 PM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    You should set the layout for each container to be what you want it to be for that container.
    Check that the code is setting it for the desired container.
    Ok thanks. You aren't able to show me in my code what you would change to fix it would you?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Look at where there is code setting the layout and see if it is setting it for the container you want it set for.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Look at where there is code setting the layout and see if it is setting it for the container you want it set for.
    Ok, I've made some progress. Thanks for the tip on my layouts. Guess I should have specified that I wanted the questionPanel layout at 7,2 haha. Thanks!

    On a side note, now I've got it doing everything right up to where it converts my input from the user to doubles and then I get errors. Would you be able to see if you can tell why it is doing that? Here is my updated current code for it:

    Code:
    
    package travelExpenses;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    public class TravelExpenses extends JFrame
    {
    	public final double RENTALCAR = 40;
    	public final double PARKING = 20;
    	public final double FOOD = 40;
    
    	
    	private JLabel numDays, airfareCost, rentalCarCost, parkingFees, classFee, lodgingFee, foodCost;
    	private JTextField numDaysF, airfareCostF, rentalCarCostF, parkingFeesF, classFeeF, lodgingFeeF, foodCostF;
    	
    	private JPanel questionPanel;
    	private JPanel buttonPanel;
    
    	public TravelExpenses() 
    	{
    		super("Travel Expenses");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		setLayout(new GridLayout(1,2));
    	
    		buildMenuBar();
    		buildQuestionPanel();
    		buildButtonPanel();
    		
    		add(questionPanel);
    		add(buttonPanel);
    		
    		pack();
    		setVisible(true);
    	}
    	
    	
    	public void buildMenuBar()
    	{
    		JMenuBar menuBar;
    		JMenu fileMenu;
    		JMenu editMenu;
    		JMenuItem exit;
    		JMenuItem cut;
    		JMenuItem copy;
    		JMenuItem paste;
    		
    		menuBar = new JMenuBar();
    		
    		fileMenu = new JMenu("File");
    		fileMenu.setMnemonic(KeyEvent.VK_F);
    		
    		exit = new JMenuItem("Exit");
    		exit.setMnemonic(KeyEvent.VK_E);
    		exit.addActionListener(new ExitButtonListener());
    	
    		fileMenu.add(exit);
    		
    		
    		editMenu = new JMenu("Edit");
    		editMenu.setMnemonic(KeyEvent.VK_E);
    		
    		cut = new JMenuItem("Cut");
    		cut.setMnemonic(KeyEvent.VK_X);
    		
    		copy = new JMenuItem("Copy");
    		copy.setMnemonic(KeyEvent.VK_C);
    		
    		paste = new JMenuItem("Paste");
    		paste.setMnemonic(KeyEvent.VK_P);
    		
    		editMenu.add(cut);
    		editMenu.add(copy);
    		editMenu.add(paste);
    		
    		
    		menuBar.add(fileMenu);
    		menuBar.add(editMenu);
    		
    		setJMenuBar(menuBar);
    	}
    
    	
    	public void buildQuestionPanel()
    	{	
    		questionPanel = new JPanel();
    		questionPanel.setLayout(new GridLayout(7,2));
    		
    		
    		numDays = new JLabel("Days on Trip");
    		numDaysF = new JTextField(10);
    		
    		airfareCost = new JLabel("Airfare Cost");
    		airfareCostF = new JTextField(10);
    		
    		rentalCarCost = new JLabel("Rental Car Cost");
    		rentalCarCostF = new JTextField(10);
    		
    		parkingFees = new JLabel("Parking Cost");
    		parkingFeesF = new JTextField(10);
    		
    		classFee = new JLabel("Conference / Training Cost");
    		classFeeF = new JTextField(10);
    		
    		lodgingFee = new JLabel("Lodging Cost");
    		lodgingFeeF = new JTextField(10);
    		
    		foodCost = new JLabel("Food Cost");
    		foodCostF = new JTextField(10);
    		
    		questionPanel.add(numDays);
    		questionPanel.add(numDaysF);
    		questionPanel.add(airfareCost);
    		questionPanel.add(airfareCostF);
    		questionPanel.add(rentalCarCost);
    		questionPanel.add(rentalCarCostF);
    		questionPanel.add(parkingFees);
    		questionPanel.add(parkingFeesF);
    		questionPanel.add(classFee);
    		questionPanel.add(classFeeF);
    		questionPanel.add(lodgingFee);
    		questionPanel.add(lodgingFeeF);
    		questionPanel.add(foodCost);
    		questionPanel.add(foodCostF);
    		
    	}
    	
    	
    	public void buildButtonPanel()
    	{
    		buttonPanel = new JPanel();
    		JButton calcButton;
    		JButton exitButton;
    		calcButton = new JButton("Calculate Total");
    		exitButton = new JButton("Exit");
    		
    		
    		
    		calcButton.addActionListener(new CalcButtonListener());
    		exitButton.addActionListener(new ExitButtonListener());
    		buttonPanel.add(calcButton);
    		buttonPanel.add(exitButton);
    	
    	}
    	
    	
    	private class CalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.out.println("beginning of calcButtonListener");
    			Scanner keyboard = new Scanner(System.in);
    			DecimalFormat fmt = new DecimalFormat("#,##0.00");
    			double days;
    			double airfare, rentalCar, parking, classFee, lodging, food;
    			double total, allowableTotal, overage;
    			double rentalOverage, parkingOverage, foodOverage;
    			double rentalAllowance, parkingAllowance, foodAllowance;
    			
    			String str;
    			
    			//THIS IS WHERE I'M LOSING IT. NOT SURE IF SOMETHING IS WRONG WITH HOW I'M CONVERTING THE 
    			//USER INPUT TO A DOUBLE OR WHAT IS HAPPENING???
    			System.out.println("Right before converting results to numbers");
    			str = numDays.getText();
    			days = Double.parseDouble(str);
    			
    			System.out.println("Right after numdays conversion before the airfare conversion");
    			str = airfareCostF.getText();
    			airfare = Double.parseDouble(str);
    			
    			str = rentalCarCostF.getText();
    			rentalCar = Double.parseDouble(str);
    			
    			str = parkingFeesF.getText();
    			parking = Double.parseDouble(str);
    			
    			str = classFeeF.getText();
    			classFee = Double.parseDouble(str);
    			
    			str = lodgingFeeF.getText();
    			lodging = Double.parseDouble(str);
    			
    			str = foodCostF.getText();
    			food = Double.parseDouble(str);
    			
    			System.out.println("Running right before calculating for overages");
    			
    			
    			rentalAllowance = RENTALCAR * days;
    			parkingAllowance = PARKING * days;
    			foodAllowance = FOOD * days;
    			
    			total = airfare + rentalCar + parking + classFee + lodging + food;
    	
    			
    			if (rentalCar > rentalAllowance)
    				rentalOverage = rentalCar - rentalAllowance;
    			else
    				rentalOverage = 0;
    			
    			if (parking > parkingAllowance)
    				parkingOverage = parking - parkingAllowance;
    			else
    				parkingOverage = 0;
    			
    		
    			if (food > foodAllowance)
    				foodOverage = food - foodAllowance;
    			else
    				foodOverage = 0;
    			
    			
    			overage = rentalOverage + parkingOverage + foodOverage;
    			allowableTotal = total - overage;
    			
    			System.out.println("Running right before the results being displayed");
    			
    			
    			JOptionPane.showMessageDialog(null, "Total Charges: $" + fmt.format(total) + "\n\n" + 
    												"Non-Reimburstable Charges: $" + fmt.format(overage) + "\n\n" +
    												"Your reimburstable amount: $" + fmt.format(allowableTotal), 
    												"Total", JOptionPane.PLAIN_MESSAGE);
    			
    		}
    		
    		
    	}
    	
    	private class ExitButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    						
    		}
    	}
    	
    	
    	
    	public static void main(String[] args) 
    	{
    		new TravelExpenses();
    	}
    
    }
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I get errors.
    Please post the full text of the error message that shows what and where happened.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Please post the full text of the error message that shows what and where happened.
    Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Days on Trip"
    	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    	at java.lang.Double.parseDouble(Double.java:540)
    	at travelExpenses.TravelExpenses$CalcButtonListener.actionPerformed(TravelExpenses.java:168)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6505)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:682)
    	at java.awt.EventQueue$3.run(EventQueue.java:680)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:696)
    	at java.awt.EventQueue$4.run(EventQueue.java:694)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo