I'm trying to position 5 buttons on a JFrame. Four buttons should be in first row and the fifth one should be in the second row. But here all of the buttons appear in one row and half of the fifth button is out of the panel. By the way if I use frame.pack(); my frame become smaller than what I expected. I don't want to change the size of frame. I want to locate the fifth one in the second row.
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;
public class test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 529, 300);
frame.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
JPanel panel = new JPanel();
frame.getContentPane().add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JButton btnNewButton_4 = new JButton("New button");
panel.add(btnNewButton_4);
JButton btnNewButton_3 = new JButton("New button");
panel.add(btnNewButton_3);
JButton btnNewButton = new JButton("New button");
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button");
panel.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button");
panel.add(btnNewButton_2);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}