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

theTextArea.setLocation(15, 30);

and

//theTextArea.setAlignmentX(CENTER_ALIGNMENT);
          //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?

//mainForSimpleGUI.java

import javax.swing.*;
import java.awt.*;

public class mainForSimpleGUI
{
    public static void main(String[] args)
    {
        SimpleJavaGUI labelFrame = new SimpleJavaGUI(); //creates SimpleJavaGUI
        //labelFrame = new JLabel();
        labelFrame.setLocationRelativeTo(null);
        labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        labelFrame.setSize(300, 300);
        labelFrame.setVisible(true);
    }

}
//SimpleJavaGUI.java

import java.awt.*;
import javax.swing.*;


public class SimpleJavaGUI extends JFrame
{
    private JTextArea theTextArea;

    public SimpleJavaGUI()
    {
        super("mainForSimpleGUI");
        setLayout(new FlowLayout() );

        theTextArea.setLocation(15, 30);
        //theTextArea.setAlignmentX(CENTER_ALIGNMENT);
        //theTextArea.setAlignmentY(CENTER_ALIGNMENT);
        theTextArea = new JTextArea(2, 20);
        theTextArea.setText(" Follow the white rabbit.");
        theTextArea.setToolTipText("This is a text area.");
        theTextArea.setEditable(false);
        theTextArea.setDisabledTextColor(Color.BLACK);
        theTextArea.setLineWrap(true);
        theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) );
        add(theTextArea);
    }
}

Use line 19 before 16 and 17. You are not initializing the theTextArea.

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

//SimpleJavaGUI.java

import java.awt.*;
import javax.swing.*;


public class SimpleJavaGUI extends JFrame
{
    private JTextArea theTextArea;

    public SimpleJavaGUI()
    {
        super("mainForSimpleGUI");
        setLayout(new FlowLayout() );

        theTextArea = new JTextArea(2, 15);
        theTextArea.setLocation(50, 50);
        //theTextArea.setAlignmentX(CENTER_ALIGNMENT);
        //theTextArea.setAlignmentY(CENTER_ALIGNMENT);
        theTextArea.setText(" Follow the white rabbit.");
        theTextArea.setToolTipText("This is a text area.");
        theTextArea.setEditable(false);
        theTextArea.setDisabledTextColor(Color.BLACK);
        theTextArea.setLineWrap(true);
        theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) );
        add(theTextArea);
    }
}
import javax.swing.*;

import java.awt.*;
   
  public class mainForSimpleGUI
  {
      public static void main(String[] args)
      {
          SimpleJavaGUI labelFrame = new SimpleJavaGUI(); 
          labelFrame = new SimpleJavaGUI();
          labelFrame.setLocationRelativeTo(null);
          labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          labelFrame.setSize(300, 300);
          labelFrame.setVisible(true);
      }
   
  }

    @SuppressWarnings("serial")
	class SimpleJavaGUI extends JFrame
  {
      public  JTextArea theTextArea;
   
      public SimpleJavaGUI()
      {
          super("mainForSimpleGUI");
          setLayout(new FlowLayout() );
          theTextArea = new JTextArea(2, 20);
          theTextArea.setLocation(15, 30);
          theTextArea.setAlignmentX(CENTER_ALIGNMENT);
          theTextArea.setAlignmentY(CENTER_ALIGNMENT);
          
          theTextArea.setText(" Follow the white rabbit.");
          theTextArea.setToolTipText("This is a text area.");
          theTextArea.setEditable(false);
          theTextArea.setDisabledTextColor(Color.BLACK);
          theTextArea.setLineWrap(true);
          theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) );
          add(theTextArea);
      }
  }

I applied this and it is showing me the text area.

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 a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.