Results 1 to 8 of 8
  1. #1
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default simple app-help please

    All im doing is clicking on a 'button' and the value associated with the button is then multiplied and tax is added as well. It compiles and clears, but it doesnt grab my value nor does the try-catch error popup work.


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.text.*; // add import statement
    
    public class Fuel extends JApplet implements ActionListener
    {
    
    // objects
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    
    	DecimalFormat decimalFormat; // declaration statement
    
    
    	// components
    	JLabel select;
    	JRadioButton gasoline;
    	JRadioButton diesel;
    	JRadioButton hydrogen;
    
    	
    	JLabel gallons;
    	JTextField gallonsText;
    	
    	JLabel amount;
    	JTextField amountText;
    	
    	JButton enter;
    	JButton clear;
    	
    	JTextField displayGallonsText;
    	JTextField displayAmountText;
    	JTextField displayTotalAmount;
    	
    	
    	// variables
    	String message = "Walker";
    	final double GASOLINE_PRICE = 1.90;
    	final double DIESEL_PRICE = 2.00;
    	final double HYDROGEN_PRICE = 2.25;
    	double priceOfFuel;
    	int numberOfGallons;
    	double totalAmount;
    	
    	
    	
    	public void init()
    	{
    		setLayout(null);
    		setSize(400, 400);
     
     	// initialization
    		select = new JLabel ();
    		select.setBounds(75, 50, 150, 20);
    		select.setFont(new Font("Default", Font.PLAIN, 12));
    		select.setText("Select Fuel Type");
    		select.setForeground(black);
    		select.setHorizontalAlignment(JLabel.CENTER);
    		add(select);
    	
    		gasoline = new JRadioButton();
    		gasoline.setBounds(100, 75, 150, 20);
    		gasoline.setFont(new Font("Default", Font.PLAIN, 12));
    		gasoline.setText("Gasoline");
    		gasoline.setForeground(black);
    		gasoline.setHorizontalAlignment(JLabel.LEFT);
    		add(gasoline);
    	
    		diesel = new JRadioButton();
    		diesel.setBounds(100, 100, 150, 20);
    		diesel.setFont(new Font("Default", Font.PLAIN, 12));
    		diesel.setText("Diesel");
    		diesel.setForeground(black);
    		diesel.setHorizontalAlignment(JLabel.LEFT);
    		add(diesel);
    	
    		hydrogen = new JRadioButton();
    		hydrogen.setBounds(100, 125, 150, 20);
    		hydrogen.setFont(new Font("Default", Font.PLAIN, 12));
    		hydrogen.setText("Hydrogen");
    		hydrogen.setForeground(black);
    		hydrogen.setHorizontalAlignment(JLabel.LEFT);
    		add(hydrogen);
    	
    		gallons = new JLabel();
    		gallons.setBounds(100, 175, 150, 20);
    		gallons.setFont(new Font("Default", Font.PLAIN, 12));
    		gallons.setText("Number of Gallons");
    		gallons.setForeground(black);
    		gallons.setHorizontalAlignment(JLabel.LEFT);
    		add(gallons);
    	
    		gallonsText = new JTextField(2);
    		gallonsText.setBounds(100, 200, 125, 20);
    		gallonsText.setFont(new Font("Default", Font.PLAIN, 12));
    		gallonsText.setForeground(black);
    		gallonsText.setHorizontalAlignment(JLabel.LEFT);
    		gallonsText.setEditable(true);
    		add(gallonsText);
    	
    		amount = new JLabel();
    		amount.setBounds(100, 225, 150, 20);
    		amount.setFont(new Font("Default", Font.PLAIN, 12));
    		amount.setText("Amount");
    		amount.setForeground(black);
    		amount.setHorizontalAlignment(JLabel.LEFT);
    		add(amount);
    	
    		amountText = new JTextField(2);
    		amountText.setBounds(100, 250, 150, 20);
    		amountText.setFont(new Font("Default", Font.PLAIN, 12));
    		amountText.setForeground(black);
    		amountText.setHorizontalAlignment(JLabel.LEFT);
    		amountText.setEditable(false);
    		add(amountText);
    	
    		enter = new JButton();
    		enter.setBounds(100, 300, 100, 20);
    		enter.setFont(new Font("Default", Font.PLAIN, 12));
    		enter.setText("Enter");
    		enter.setForeground(black);
    		enter.setBackground(white);
    		enter.addActionListener(this);
    		add(enter);
    	
    		clear = new JButton();
    		clear.setBounds(210, 300, 100, 20);
    		clear.setFont(new Font("Default", Font.PLAIN, 12));
    		clear.setText("Clear");
    		clear.setForeground(black);
    		clear.setBackground(white);
    		clear.addActionListener(this);
    		add(clear);
    	
    }
    
    	public void actionPerformed(ActionEvent event)
    	{
    		Object obj = event.getSource();
    		if(obj == enter)
    		{
    			getButton();
    		}
    		else	if(obj == clear)
    		{
    			clearAll();
    		}
    	}
    
    	public void getButton()
    	{
    		if(gasoline.isSelected())
    		{
    			priceOfFuel = GASOLINE_PRICE;			
    		}
    		
    		if(diesel.isSelected())
    		{
    			priceOfFuel = DIESEL_PRICE;
    		}
    		
    		if(hydrogen.isSelected())
    		{
    			priceOfFuel = HYDROGEN_PRICE;
    		}
    	}
    	
    	
    	public void getGallons()
    	{
    	try
    	{
    		numberOfGallons = Integer.parseInt(gallonsText.getText());
    		calculateAmount();
    	}	
    		catch(NumberFormatException exception)
    		{
    		      	JOptionPane.showMessageDialog(this,
    		       	"Please enter and integer!",
    		        "Number Format Error", JOptionPane.ERROR_MESSAGE);
    		        gallonsText.setText("");
    		        gallonsText.requestFocusInWindow();
    		}
    	}
    	
    	public void calculateAmount()
    		{
    			totalAmount = priceOfFuel * numberOfGallons;
    			displayAmountText();
    		}
    	
    	public void displayAmountText()
    	{
    		decimalFormat = new DecimalFormat("0.00");
    		amountText.setText("" + decimalFormat.format(amountText));
    	}
    		
    	public void clearAll()
    	{
    		gallonsText.setText("");
    		gallonsText.requestFocusInWindow();
    		amountText.setText("");
    	}
    }

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Which button do you mean, and where in your code above do you tell the button to do all this in the button's action listener's actionPerformed method?

  3. #3
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    There are 3 buttons to choose from, "gasoline" "diesel" and "hydrogen." You choose one and then type in the number of gallons, hit enter and then it multiplies the two and adds tax.
    My book is crap and doesnt tell us how to grab the value of the delcared variable for the button, my teacher gives us "sample code" to try and incorporate it into our own.
    Below is what Im trying to use so if "gasoline" were selected the variable 'priceOfGas' would equal the final value of GASOLINE_PRICE.
    Java Code:
    public void getButton()
    	{
    		if(gasoline.isSelected())
    		{
    			priceOfFuel = GASOLINE_PRICE;			
    		}
    		
    		if(diesel.isSelected())
    		{
    			priceOfFuel = DIESEL_PRICE;
    		}
    		
    		if(hydrogen.isSelected())
    		{
    			priceOfFuel = HYDROGEN_PRICE;
    		}
    	}

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    So your getting the price of fuel when the button is pressed and that's it. You're not doing anything else as the code is only doing what you tell it to. Nowhere do you tell it to get any textfield text, convert anything to numbers or multiply or add anything.

    If you want this method to get the results you'll have to put more code in this method. You've got other methods in the code above that does these things, and perhaps in this method above you should call some of those methods...

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by skuzzie View Post
    You choose one and then type in the number of gallons, hit enter and then it multiplies the two and adds tax.
    to display the result of gallons * fueltype i made two corrections:

    1. in the actionPerformed method just after getButton() add the call getGallons(), so that also the entered value is readed
    2. in the method displayAmountText() you have an error: instead of formatting the amountText which throw an exception you should format the totalAmount, so use amountText.setText(decimalFormat.format(totalAmoun t)); instead.

    i couldn't find the statement for calculating the taxes so the totalAmount shown is without taxes. other point: group your radiobuttons so that only one can be selected.
    Last edited by j2me64; 11-07-2010 at 10:49 PM.

  6. #6
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    So your getting the price of fuel when the button is pressed and that's it. You're not doing anything else as the code is only doing what you tell it to. Nowhere do you tell it to get any textfield text, convert anything to numbers or multiply or add anything.
    This is what I dont understand I guess. Thers no actual textfield thats holding the value for when the button is clicked. Its declared so im thinking that it would just hold the value once the buttons clicked??...then when I go to calculate it, it just brings it back when its called.

    Also, I should consolidate all of the methods into the first one then??? Shouldnt it still work though since the calculation method is still there, and being called at the end of the previous method?

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by skuzzie View Post
    This is what I dont understand I guess. Thers no actual textfield thats holding the value for when the button is clicked. Its declared so im thinking that it would just hold the value once the buttons clicked??...then when I go to calculate it, it just brings it back when its called.

    Also, I should consolidate all of the methods into the first one then??? Shouldnt it still work though since the calculation method is still there, and being called at the end of the previous method?
    No, don't consolidate anything. I've been trying to tell you that you've got methods that already extract the textfield text for you. All you have to do is call those methods in your getButton method. That's it.

  8. #8
    skuzzie is offline Member
    Join Date
    Oct 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok, sorry I had a brain fart. I just added "getGallons()" and it works. Thanks a bunch!

Similar Threads

  1. WHY is it not def? PLZ HELP IT IS VERY SIMPLE!!
    By just_java in forum New To Java
    Replies: 9
    Last Post: 02-07-2010, 09:01 AM
  2. Simple Help
    By Andy_M in forum New To Java
    Replies: 6
    Last Post: 01-09-2010, 09:10 PM
  3. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 02:55 AM
  4. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  5. simple GUI
    By dim_ath in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 03:00 PM

Posting Permissions

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