Centering Issues

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2005
Posts: 180
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Centering Issues

 
0
  #1
Sep 15th, 2009
I'm toying around with Java trying to familiarize myself with the basic GUI stuff. I can't get my program or my text field inside of it to center on the screen.

For the Simple... file, I've tried using the
  1. theTextArea.setLocation(15, 30);
and
  1. //theTextArea.setAlignmentX(CENTER_ALIGNMENT);
  2. //theTextArea.setAlignmentY(CENTER_ALIGNMENT);
seperately. When I have either of them as active code and not comments, it throws an error and won't even run the program. What am I missing here?

  1. //mainForSimpleGUI.java
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5.  
  6. public class mainForSimpleGUI
  7. {
  8. public static void main(String[] args)
  9. {
  10. SimpleJavaGUI labelFrame = new SimpleJavaGUI(); //creates SimpleJavaGUI
  11. //labelFrame = new JLabel();
  12. labelFrame.setLocationRelativeTo(null);
  13. labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. labelFrame.setSize(300, 300);
  15. labelFrame.setVisible(true);
  16. }
  17.  
  18. }


  1. //SimpleJavaGUI.java
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6.  
  7. public class SimpleJavaGUI extends JFrame
  8. {
  9. private JTextArea theTextArea;
  10.  
  11. public SimpleJavaGUI()
  12. {
  13. super("mainForSimpleGUI");
  14. setLayout(new FlowLayout() );
  15.  
  16. theTextArea.setLocation(15, 30);
  17. //theTextArea.setAlignmentX(CENTER_ALIGNMENT);
  18. //theTextArea.setAlignmentY(CENTER_ALIGNMENT);
  19. theTextArea = new JTextArea(2, 20);
  20. theTextArea.setText(" Follow the white rabbit.");
  21. theTextArea.setToolTipText("This is a text area.");
  22. theTextArea.setEditable(false);
  23. theTextArea.setDisabledTextColor(Color.BLACK);
  24. theTextArea.setLineWrap(true);
  25. theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) );
  26. add(theTextArea);
  27. }
  28. }
Be what you is, don't be what you ain't.
Cause if you ain't what you is, you is what you ain't!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 108
Reputation: Majestics is an unknown quantity at this point 
Solved Threads: 7
Majestics Majestics is offline Offline
Junior Poster

Re: Centering Issues

 
0
  #2
Sep 15th, 2009
Use line 19 before 16 and 17. You are not initializing the theTextArea.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 180
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: Centering Issues

 
0
  #3
Sep 15th, 2009
Okay, I changed that. For whatever reason, it's not showing the text area at all now. Here's how it looks now (I commented out any location-oriented lines just to try and "dumb" it down and figure out why my text area vanished):

  1. //SimpleJavaGUI.java
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6.  
  7. public class SimpleJavaGUI extends JFrame
  8. {
  9. private JTextArea theTextArea;
  10.  
  11. public SimpleJavaGUI()
  12. {
  13. super("mainForSimpleGUI");
  14. setLayout(new FlowLayout() );
  15.  
  16. theTextArea = new JTextArea(2, 15);
  17. theTextArea.setLocation(50, 50);
  18. //theTextArea.setAlignmentX(CENTER_ALIGNMENT);
  19. //theTextArea.setAlignmentY(CENTER_ALIGNMENT);
  20. theTextArea.setText(" Follow the white rabbit.");
  21. theTextArea.setToolTipText("This is a text area.");
  22. theTextArea.setEditable(false);
  23. theTextArea.setDisabledTextColor(Color.BLACK);
  24. theTextArea.setLineWrap(true);
  25. theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) );
  26. add(theTextArea);
  27. }
  28. }
Be what you is, don't be what you ain't.
Cause if you ain't what you is, you is what you ain't!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 108
Reputation: Majestics is an unknown quantity at this point 
Solved Threads: 7
Majestics Majestics is offline Offline
Junior Poster

Re: Centering Issues

 
0
  #4
Sep 16th, 2009
  1.  
  2. import javax.swing.*;
  3.  
  4. import java.awt.*;
  5.  
  6. public class mainForSimpleGUI
  7. {
  8. public static void main(String[] args)
  9. {
  10. SimpleJavaGUI labelFrame = new SimpleJavaGUI();
  11. labelFrame = new SimpleJavaGUI();
  12. labelFrame.setLocationRelativeTo(null);
  13. labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. labelFrame.setSize(300, 300);
  15. labelFrame.setVisible(true);
  16. }
  17.  
  18. }
  19.  
  20. @SuppressWarnings("serial")
  21. class SimpleJavaGUI extends JFrame
  22. {
  23. public JTextArea theTextArea;
  24.  
  25. public SimpleJavaGUI()
  26. {
  27. super("mainForSimpleGUI");
  28. setLayout(new FlowLayout() );
  29. theTextArea = new JTextArea(2, 20);
  30. theTextArea.setLocation(15, 30);
  31. theTextArea.setAlignmentX(CENTER_ALIGNMENT);
  32. theTextArea.setAlignmentY(CENTER_ALIGNMENT);
  33.  
  34. theTextArea.setText(" Follow the white rabbit.");
  35. theTextArea.setToolTipText("This is a text area.");
  36. theTextArea.setEditable(false);
  37. theTextArea.setDisabledTextColor(Color.BLACK);
  38. theTextArea.setLineWrap(true);
  39. theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) );
  40. add(theTextArea);
  41. }
  42. }
I applied this and it is showing me the text area.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 180
Reputation: kahaj is an unknown quantity at this point 
Solved Threads: 0
kahaj kahaj is offline Offline
Junior Poster

Re: Centering Issues

 
0
  #5
Sep 16th, 2009
Okay, what options are available to center items within a JFrame? I thought for sure that the setAlignmentX/Y would work for it. I don't understand why they're not working.
Be what you is, don't be what you ain't.
Cause if you ain't what you is, you is what you ain't!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 233 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC