Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By gioeco

Thread: Area and Volume help

  1. #1
    gioeco is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

    Default 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();
    			}
    			}

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gioeco is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default 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.

  5. #5
    gioeco is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

    Default 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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Area and Volume help

    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    gioeco is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

    Default 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

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default 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.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Area and Volume help

    Quote Originally Posted by gioeco View Post
    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.
    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.
    DarrylBurke likes this.

  11. #11
    gioeco is offline Member
    Join Date
    May 2012
    Posts
    10
    Rep Power
    0

    Default Re: Area and Volume help

    All right thanks for the suggestion, Fubarable. Next time I will be much more specific and descriptive whenever I post something.
    Fubarable likes this.

Similar Threads

  1. Control volume
    By calippus in forum New To Java
    Replies: 0
    Last Post: 10-31-2011, 02:17 PM
  2. Volume of Intersection
    By afifi in forum Advanced Java
    Replies: 0
    Last Post: 01-07-2011, 10:42 AM
  3. javax sound set volume
    By Dennis in forum Advanced Java
    Replies: 0
    Last Post: 06-08-2010, 03:33 PM
  4. Volume of AudioClip
    By Fedor in forum New To Java
    Replies: 0
    Last Post: 04-25-2009, 04:16 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
  •