0

I am trying to align my JButton and JTextArea to the bottom middle of my code, side by side. I followed a few tutorials and came to this point. When I execute my program, the text area and the button are still both aligned at the top. Help!

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class Main extends JFrame implements ActionListener{

JPanel panel = new JPanel();
JButton button = new JButton("Confirm!");
JTextArea text = new JTextArea(1, 20);
public Main() {
    super("Battleship!");
    setLayout(new FlowLayout());
    button.addActionListener(this);
    setSize(600, 500);
    setResizable(true);
    button.setLayout(new FlowLayout(FlowLayout.CENTER));
    text.setLayout(new FlowLayout(FlowLayout.CENTER));
    panel.add(text, BorderLayout.SOUTH);
    panel.add(button, BorderLayout.SOUTH);
    add(panel);

    setVisible(true);
}
public static void main(String[] args) {
    new Main();
}
@Override
public void actionPerformed(ActionEvent e) {
    button.setText(text.getText());
}

}
4
  • 2
    BorderLayout can only manage a single component at any of the 5 available positions. A better solution might be to use a more flexible layout manager, like GridBagLayout Commented Dec 8, 2017 at 0:23
  • Another great option is the JavaFX library. Not trait a fix to your problem but I love the JavaFX library. Commented Dec 8, 2017 at 0:47
  • @JacobB. "but I love the JavaFX library" That's no excuse for recommending a clearly off-topic API. Commented Dec 8, 2017 at 3:34
  • @AndrewThompson "clearly off-topic" is opinionated. I think that offering another possible GUI API isn't off topic for a question about GUI, especially if it's only in the comments section. Commented Dec 8, 2017 at 4:39

2 Answers 2

1

See the notes in the code comments.

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

public class MainLayout extends JFrame {

    // A panel defaults to flow layout. Use the default
    JPanel panel = new JPanel();
    JButton button = new JButton("Confirm!");
    JTextArea text = new JTextArea(1, 20);

    public MainLayout() {
        super("Battleship!");
        //setLayout(new FlowLayout()); // use the default border layout
        setSize(600, 500); // should be packed after components added.
        //setResizable(true); // this is the default
        /* Don't set layouts on things like buttons or text areas
        This is only useful for containers to which we add other
        components, and it is rarely, if ever, useful to add components
        to these types of GUI elements. */
        //button.setLayout(new FlowLayout(FlowLayout.CENTER));
        // text.setLayout(new FlowLayout(FlowLayout.CENTER));

        /* No need for layout constraints when adding these to 
        a flow layout */
        //panel.add(text, BorderLayout.SOUTH);
        panel.add(text);
        //panel.add(button, BorderLayout.SOUTH);
        panel.add(button);

        // NOW we need the constraint!
        add(panel, BorderLayout.PAGE_END);

        setVisible(true);
    }

    public static void main(String[] args) {
        /* Swing/AWT GUIs should be started on the EDT.
        Left as an exercise for the reader */
        new MainLayout();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

There is no need of setting any flow layout to button or text for placing in center because panel has by default "FlowLayout center" which will place the components in center And frame has by default "BorderLayout"
To place the button and textarea at the bottom you only need to add both of them in panel(which you already done) and then add panel to frame with parameter as BorderLayout.SOUTH Seee the modified code below it will work as per your need

 import java.awt.BorderLayout;
 import java.awt.FlowLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.*;
 public class Main extends JFrame implements ActionListener
 {
      JPanel panel = new JPanel();
      JButton button = new JButton("Confirm!");
      JTextArea text = new JTextArea(1, 20);
      public Main()
      {
          super("Battleship!");
          button.addActionListener(this);
          setSize(600, 500);
          setResizable(true);
          panel.add(text);
          panel.add(button);
          add(panel, BorderLayout.SOUTH);
          setVisible(true);
      } 
      public static void main(String[] args)
      {
          new Main();
      }
      @Override public void actionPerformed(ActionEvent e)
      {
           button.setText(text.getText());
      }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.