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);
}
}