Results 1 to 9 of 9
Thread: JTextField question
- 05-01-2012, 04:08 AM #1
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
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(); } }
-
Re: JTextField question
Which JTextField is that? What are you doing to try to move it over?
- 05-01-2012, 04:21 AM #3
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
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.
- 05-01-2012, 04:23 AM #4
Re: JTextField question
Chose a suitable layout manager. Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-01-2012, 03:41 PM #5
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
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?
- 05-01-2012, 05:16 PM #6
Re: JTextField question
I said, use a suitable layout manager. It's evident that GridLayout isn't serving your purpose.
GridLayout doesn't do that. Read the API and the tutorial.I am trying to get the TextField smaller and move it to the left,
dbLast edited by DarrylBurke; 05-01-2012 at 05:27 PM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-01-2012, 05:20 PM #7
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
Re: JTextField question
Thanks for your help mate. It is evident that I need more experience with Java.
.gif)
Thanks again for clearing things up.
- 05-01-2012, 05:29 PM #8
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-01-2012, 08:10 PM #9
Member
- Join Date
- May 2012
- Posts
- 10
- Rep Power
- 0
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
-
JTextField Question.
By dougie1809 in forum AWT / SwingReplies: 6Last Post: 04-01-2012, 05:53 AM -
JTextfield question
By xyknight in forum New To JavaReplies: 3Last Post: 04-08-2011, 04:25 PM -
JTextField Question
By Rocketz in forum New To JavaReplies: 1Last Post: 03-09-2011, 12:38 AM -
JTextField Question
By Kyle227 in forum New To JavaReplies: 5Last Post: 05-09-2010, 07:44 AM -
JTextField question
By Chasingxsuns in forum New To JavaReplies: 5Last Post: 07-14-2009, 02:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks