Reading in a string, converting to int in an applet

I have to calculate the percentage of fat in the total calories, so na

Page 1 of 1

1 Replies - 1175 Views - Last Post: 22 October 2009 - 08:51 PM Rate Topic: -----

#1 MaryMooCow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-October 09

Reading in a string, converting to int in an applet

Posted 22 October 2009 - 08:07 PM

//And of course, if you see anything else wrong here, please inform me, thanks again!
in case the code doesn't show up right, use this url:
My Java Code
import java.awt.*;
import java.awt.event.*;
import java.swing.*;
import java.swing.event.*;

public class Calories extends JFrame implements ActionListener
{
	private JTextArea caloriesJT, fatJT, percentJT;
	private JLabel calorieJL, fatJL, percent JL, introJL;
	private JButton calculateJB;
	private Container c;

	public Calories()
	{
		super("Calories from Fat");
		c=getContentPane();
		c.setBackground(Color.grey);
		c.setLayout(new BorderLayout(5,5));

		JLabel introJL=("This will help you to determine the amount of\n
						 calories that come from fat in a food.\n
						 Please enter the total calories followed by the\n
						 total grams of fat and press 'Calculate' when done.");
		JLabel calorieJL=("Total amount of calories: ");
		JLabel fatJL=("Total grams of fat: ");
		JLabel percentJL=("Percentage of Calories from Fat: ");

		c.add(introJL, BorderLayout.NORTH);
		introJL.setFont(newFont("Courier", Font.BOLD, 18);

		c.add(calorieJL, BorderLayout.WEST);
		calorieJL.setFont(newFont("Courier", Font.BOLD, 18);
		c.add(calorieJT, BorderLayout.WEST);

		c.add(fatJL, BorderLayout.WEST);
		fatJL.setFont(newFont("Courier", Font.BOLD, 18);
		c.add(fatJT, BorderLayout.WEST);

		c.add(percentJL, BorderLayout.EAST);
		percentJL.setFont(newFont("Courier", Font.BOLD, 18);
		c.add(percentJT, BorderLayout.EAST);

		JButton calculateJB=("Calculate");
		calculateJB.setFont(newFont("Courier", Font.BOLD, 18);
		c.add(calculateJB, BorderLayout.SOUTH);
		calculate.addActionListener(this);

		setSize(350, 350);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	private void displayPercent()
	{
		int calorie, fat, percent;
		percent.setEditable(false);
		percent.setText("");
		//read in calorie and fat
		percent=(int)((fat*9)/calorie)*100;
		percentJT.append(percent+"%");
		repaint();
	}

	public void actionPerformed(ActionEvent event)
	{
		if(event.getActionCommand().equals("Calculate"))displayPercent();
	}

	public static void main(String[] args)
	{
		Calories alc= new Calories();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Reading in a string, converting to int in an applet

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading in a string, converting to int in an applet

Posted 22 October 2009 - 08:51 PM

Multiple points:

	private JLabel calorieJL, fatJL, percent JL, introJL;
	private JButton calculateJB;



OK your components are defined as instance variables... so all methods in the class can see them

But here you redefined them... and the way to create a JLabel is not correct
		JLabel calorieJL=("Total amount of calories: ");
		JLabel fatJL=("Total grams of fat: ");
		JLabel percentJL=("Percentage of Calories from Fat: ");
		JButton calculateJB=("Calculate");



should be

		calorieJL= new JLabel("Total amount of calories: ");
		fatJL= new JLabel("Total grams of fat: ");
		percentJL= new JLabel("Percentage of Calories from Fat: ");
		calculateJB= new JButton("Calculate");


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1