943,647 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 462
  • Java RSS
Sep 15th, 2009
0

Centering Issues

Expand Post »
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
Java Syntax (Toggle Plain Text)
  1. theTextArea.setLocation(15, 30);
and
Java Syntax (Toggle Plain Text)
  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?

Java Syntax (Toggle Plain Text)
  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. }


Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
kahaj is offline Offline
193 posts
since Sep 2005
Sep 15th, 2009
0

Re: Centering Issues

Use line 19 before 16 and 17. You are not initializing the theTextArea.
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Sep 15th, 2009
0

Re: Centering Issues

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):

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
kahaj is offline Offline
193 posts
since Sep 2005
Sep 16th, 2009
0

Re: Centering Issues

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 188
Solved Threads: 44
Posting Pro
Majestics is offline Offline
554 posts
since Jul 2007
Sep 16th, 2009
0

Re: Centering Issues

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
kahaj is offline Offline
193 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Best Approach ?
Next Thread in Java Forum Timeline: Something's missing...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC