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

    Post JTextField question

    I am having trouble moving the bottom text field to the left side.
    Could someone please show me a possible solution for this?






    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();
    		JTextField cube = new JTextField();
    		JTextField square = new JTextField();
    		
    		JPanel row4 = new JPanel();
    		JLabel volumesphere = new JLabel ("volume");
    		JTextField sphere =  new JTextField (5);		
    		
    
    		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, 700);
    			
    			row4.setLayout(layout4);
    			row4.add(volumesphere);
    			
    			
    			row4.add(sphere);
    			
    			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
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Default Re: JTextField question

    It is the "sphere" JTextField it is in line 91.

    That is the problem, I don't know what to do to try to move it over.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: JTextField question

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: JTextField question

    If you notice in my code I already have a layout manager, I have GridLayout. I have all the other stuff layed out but the last text field is not how I want it. I am trying to get the TextField smaller and move it to the left, right beside the JLabel.
    Could someone run this code on their compiler and see what I am talking about?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: JTextField question

    I said, use a suitable layout manager. It's evident that GridLayout isn't serving your purpose.

    I am trying to get the TextField smaller and move it to the left,
    GridLayout doesn't do that. Read the API and the tutorial.

    db
    Last edited by DarrylBurke; 05-01-2012 at 05:27 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: JTextField question

    Thanks for your help mate. It is evident that I need more experience with Java.

    Thanks again for clearing things up.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: JTextField question

    The API is the Application Programming Interface aka javadoc.
    Java Platform SE 6
    Java Platform SE 7

    There's no need to send a PM to ask a technical question.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: JTextField question

    I found another possible solution for my problem.



    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();
    		JTextField cube = new JTextField();
    		JTextField square = new JTextField();
    		
    		JPanel row4 = new JPanel();
    		JLabel volumesphere = new JLabel ("volume");
    		JTextField sphere =  new JTextField (5);
    		
    		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, 700);
    			
    			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();
    			}
    			}

Similar Threads

  1. JTextField Question.
    By dougie1809 in forum AWT / Swing
    Replies: 6
    Last Post: 04-01-2012, 05:53 AM
  2. JTextfield question
    By xyknight in forum New To Java
    Replies: 3
    Last Post: 04-08-2011, 04:25 PM
  3. JTextField Question
    By Rocketz in forum New To Java
    Replies: 1
    Last Post: 03-09-2011, 12:38 AM
  4. JTextField Question
    By Kyle227 in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 07:44 AM
  5. JTextField question
    By Chasingxsuns in forum New To Java
    Replies: 5
    Last Post: 07-14-2009, 02:39 AM

Posting Permissions

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