Reputation: 1
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());
}
}
Upvotes: 0
Views: 3985
Reputation: 11
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());
}
}
Upvotes: 0
Reputation: 168835
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();
}
}
Upvotes: 1