I'm trying to center a JButton in a JPanel using the default FlowLayout. Here is my code:
import javax.swing.*;
import java.awt.*;
public class CoinFlip {
public static void main(String[] args) {
JFrame frame = new JFrame("Coin Flip");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton coinFlipButton = new JButton("Flip Coin");
coinFlipButton.setSize(100, 100);
JPanel coinFlipPanel = new JPanel();
coinFlipPanel.setSize(500, 100);
coinFlipPanel.setBackground(Color.GRAY);
coinFlipPanel.add(coinFlipButton);
JTextArea coinFlipResults = new JTextArea();
coinFlipResults.setSize(500, 200);
frame.getContentPane().add(coinFlipPanel);
frame.getContentPane().add(coinFlipResults);
frame.setVisible(true);
}
}
How can I center the JButton (coinFlipButton) in the JPanel (coinFlipPanel)? I've tried using FlowLayout.CENTER but that makes the button increase in size so that it takes up all the space.