JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 3));
JFrame frame = new JFrame(); frame.setLayout(new BorderLayout());
JButton button = new JButton("Click me"); button.setLayout(new FlowLayout());This code creates a new JButton and sets its layout to FlowLayout. In all these examples, we are using the setLayout() method to set the layout manager for the JComponent. This method is part of the javax.swing package library.