Results 1 to 11 of 11
Thread: Area and Volume help
- 05-10-2012, 08:09 PM #1
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Area and Volume help
All right, first of all thanks in advance for your help.
You have to press enter in the text field in order for the program to run.
When you run the program and enter side length, of either cube or square the program won't calculate the area or volume. The program will only calculate the rest until you enter the radius of the square. This is my problem right here. I want the program to be able to calculate the cube or square without having to enter a number below the circle text field.
If I didn't make any sense in my post please let me know so I can describe my problem in a different way.
Java Code:import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Area extends JFrame implements ActionListener { JPanel row1 = new JPanel(); JLabel circleLabel = new JLabel ("Circle"); JLabel cubeLabel = new JLabel ("Cube"); JLabel squareLabel = new JLabel ("Square"); JPanel row2 = new JPanel(); JLabel radiusLabel = new JLabel ("radius"); JLabel cubelengthLabel = new JLabel ("side length"); JLabel squarelengthLabel = new JLabel ("side length"); JTextField radiusText = new JTextField (); JTextField cubesidelenghtText = new JTextField (); JTextField squaresidelengthText = new JTextField (); JPanel row3 = new JPanel(); JLabel circleareaLabel = new JLabel ("area"); JLabel volumeLabel = new JLabel ("volume"); JLabel squareareLabel = new JLabel ("area"); JTextField circle = new JTextField(10); JTextField cube = new JTextField(10); JTextField square = new JTextField(10); JPanel row4 = new JPanel(); JLabel volumesphere = new JLabel ("volume"); JTextField sphere = new JTextField (10); JLabel spaceholder = new JLabel(""); JLabel spaceholder1 = new JLabel(""); JLabel spaceholder2 = new JLabel(""); JLabel spaceholder3 = new JLabel(""); public Area (){ super("Area and Volume Calculator"); setSize(450,150); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GridLayout layout = new GridLayout(4, 3, 0, 10); setLayout(layout); FlowLayout flow1 = new FlowLayout(FlowLayout.LEFT, 110, 1); row1.setLayout(flow1); row1.add(circleLabel); row1.add(cubeLabel); row1.add(squareLabel); add(row1); GridLayout layout2 = new GridLayout(1, 6, 10, 1); row2.setLayout(layout2); row2.add(radiusLabel); row2.add(radiusText); radiusText.addActionListener(this); row2.add(cubelengthLabel); row2.add(cubesidelenghtText); cubesidelenghtText.addActionListener(this); row2.add(squarelengthLabel); row2.add(squaresidelengthText); squaresidelengthText.addActionListener(this); add(row2); GridLayout layout3 = new GridLayout(1, 2, 10, 600); row3.setLayout(layout3); row3.add(circleareaLabel); row3.add(circle); row3.add(volumeLabel); row3.add(cube); row3.add(squareareLabel); row3.add(square); add(row3); GridLayout layout4 = new GridLayout(1, 2, 10, 10); row4.setLayout(layout4); row4.add(volumesphere); row4.add(sphere); row4.add(spaceholder); row4.add(spaceholder1); row4.add(spaceholder2); row4.add(spaceholder3); add(row4); setVisible(true); } public void actionPerformed (ActionEvent e) { double pi = 3.14; double etc = 1.33; double circles = Integer.parseInt(radiusText.getText()) * Integer.parseInt(radiusText.getText()) * pi; circle.setText(circles + " ^2"); double spheres = Integer.parseInt(radiusText.getText()) * Integer.parseInt(radiusText.getText()) * Integer.parseInt(radiusText.getText())* etc * pi; sphere.setText(spheres + " ^3"); int cubes = Integer.parseInt(cubesidelenghtText.getText()) * Integer.parseInt(cubesidelenghtText.getText()); cube.setText(cubes + " ^2"); int squares = Integer.parseInt(squaresidelengthText.getText()) * Integer.parseInt(squaresidelengthText.getText()) * Integer.parseInt(squaresidelengthText.getText()); square.setText(squares + " ^3"); } public static void main (String [] arg){ Area sc = new Area(); } }
- 05-10-2012, 08:19 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Area and Volume help
You have one single ActionListener that does all the work when whatever component firest an ActionEvent. You should have different ActionListeners that all do part of the job, i.e. when you press the area button for a square the ActionListener linked to that text field should calculate the number squared.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-10-2012, 09:06 PM #3
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Re: Area and Volume help
I have been trying to do what you said, but it seems that I don't know how. I already went and did a little research to do what you are talking about, but it seems that I have no idea how to do it.
I don't know if this is what you are talking about having different Listeners? I separated each code inside the ActionEvent. Still didn't solve the problem, instead it made it worse. Again, I have no idea how to add "different ActionListeners that all do part of the job"
Thanks again for your help.
I am a bit of a newbie to JAVA and online resources sometimes don't help me.
Java Code:public void actionPerformed (ActionEvent e) { double pi = 3.14; double etc = 1.33; double circles = Integer.parseInt(radiusText.getText()) * Integer.parseInt(radiusText.getText()) * pi; circle.setText(circles + " ^2"); } public void actionPerformed1 (ActionEvent e) { double pi = 3.14; double etc = 1.33; double spheres = Integer.parseInt(radiusText.getText()) * Integer.parseInt(radiusText.getText()) * Integer.parseInt(radiusText.getText())* etc * pi; sphere.setText(spheres + " ^3"); } public void actionPerformed2 (ActionEvent e) { int squares = Integer.parseInt(squaresidelengthText.getText()) * Integer.parseInt(squaresidelengthText.getText()); square.setText(squares + " ^2"); } public void actionPerformed3 (ActionEvent e) { int cubes = Integer.parseInt(cubesidelenghtText.getText()) * Integer.parseInt(cubesidelenghtText.getText()) * Integer.parseInt(cubesidelenghtText.getText()); cube.setText(cubes + " ^3"); }Last edited by gioeco; 05-10-2012 at 09:09 PM.
- 05-10-2012, 09:21 PM #4
Re: Area and Volume help
Create new classes with each implementing the ActionListener interface for each listener you want. They can be inner classes inside of the Area class. Then create an instance of each class and add it as a listener to the button that goes with it.
If you don't understand my response, don't ignore it, ask a question.
- 05-12-2012, 10:48 PM #5
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Re: Area and Volume help
Thanks Norm for helping me because now I understand what I need to do.
One question though, how do I do inner classes? What is the code for this? Is it: Class Area { }?
thanks for helping
-
- 05-12-2012, 11:08 PM #7
Re: Area and Volume help
Take a look at the tutorial:
Inner Class Example (The Java™ Tutorials > Learning the Java Language > Classes and Objects)If you don't understand my response, don't ignore it, ask a question.
- 05-13-2012, 02:11 AM #8
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Re: Area and Volume help
I had a good laugh with the first link on google
I had already googled inner classes and I read the examples from Java tutorials before I posted, but I was still having trouble after I read the tutorials.
I guess I am just retarded. lol
- 05-13-2012, 02:14 AM #9
Re: Area and Volume help
Take the tutorial examples and play with them.
If you don't understand my response, don't ignore it, ask a question.
-
Re: Area and Volume help
In that situation, rather than ask a very general question such as " how do I do inner classes" or ask for the code, tell us *exactly* what in the tutorial confuses you, or show your code that doesn't work and ask questions about it. If you do this, we'll be able to give you much more helpful advice.
- 05-13-2012, 04:02 AM #11
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Control volume
By calippus in forum New To JavaReplies: 0Last Post: 10-31-2011, 02:17 PM -
Volume of Intersection
By afifi in forum Advanced JavaReplies: 0Last Post: 01-07-2011, 10:42 AM -
javax sound set volume
By Dennis in forum Advanced JavaReplies: 0Last Post: 06-08-2010, 03:33 PM -
Volume of AudioClip
By Fedor in forum New To JavaReplies: 0Last Post: 04-25-2009, 04:16 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks