0

I'm new to using swing and jframe components in java, and I've been working on making a console for a game. I'm trying to align the elements of the console like so:

_______________________________
|    _____________________    |
|    |                   |    |
|    |       Output      |    |
|    |___________________|    |
|                             |
|           _______           |
|          |_Input_|          |
|_____________________________|

My current program, shown below, displays the input and output boxes next to each other, rather than above and below each other.

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

public class GUI{

    public static void main(String[] args) {
        SwingUtilities.invokeLater(GUI::startUp); 
    }

    private static void startUp() {
        JFrame frame = new JFrame("AoA");
        frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1020,760);
        frame.setBackground(Color.LIGHT_GRAY);
        frame.setResizable(false);
        frame.setLayout(new FlowLayout());

        JTextArea jta = new JTextArea(30,60);
        jta.setEditable(false);
        jta.setBackground(Color.WHITE);
        frame.add(new JScrollPane(jta));

        JTextField jta2 = new JTextField();
        jta2.setPreferredSize(new Dimension(200,70));
        jta2.setHorizontalAlignment((int) JTextField.CENTER_ALIGNMENT);
        frame.add(new JScrollPane(jta2));
        frame.setVisible(true);

        jta.append("Test");

    }
}

How could I set the alignment of these components so that they match the intended result?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tyler B
  • 27
  • 3
  • 3
    You're not really adding the BoxLayout to the JFrame but rather to its contentPane, and this should be noted in the layout's constructor: `new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)` – Hovercraft Full Of Eels Feb 01 '20 at 22:33
  • 3
    First you set the layout to a BoxLayout. Then a few statements later you change it to a FlowLayout. Read the Swing tutorial on [How to Use a BoxLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html) for a working example demonstrating how to use it properly. – camickr Feb 01 '20 at 22:33
  • 1
    You should also avoid using `setPreferredSize`. In the case of `JTextField`, you can specify the number of visible columns as a guide – MadProgrammer Feb 01 '20 at 22:34
  • Agree with @camickr. How does this `frame.setLayout(new FlowLayout());` make sense? – Hovercraft Full Of Eels Feb 01 '20 at 22:34
  • Oh, I completely forgot about that flowLayout. The elements are arranged correctly now, but their sizes are wrong. They both take up as much of the screen as they can. – Tyler B Feb 01 '20 at 22:36
  • 2
    *but their sizes are wrong.* - read the tutorial! BoxLayout will grow components up to their maximum size if extra space is available. Instead of setting the frame size use the `pack()` method after you add the components to the frame. Or another approach would be do keep the default BorderLayout. Add your text area to a JScrollPane and add the scroll pane to the CENTER of the BorderLayout. Then create a JPanel and add the JTextField to the panel. Then add the panel to the BorderLayout.PAGE_END of the frame. – camickr Feb 01 '20 at 22:40
  • @camickr Ok I'll try that. Thank you! – Tyler B Feb 01 '20 at 22:41

0 Answers0