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());
}
}
BorderLayoutcan only manage a single component at any of the 5 available positions. A better solution might be to use a more flexible layout manager, likeGridBagLayout