To properly center a fixed-size JPanel inside a JFrame in Java, you can use a combination of layout managers and setBounds method. Here are 6 examples along with step-by-step explanations of the code:

Example 1: Using BorderLayout

             java
import javax.swing.*;

public class CenteredPanelExample1 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Centered JPanel Example 1");
        JPanel panel = new JPanel();
        panel.setLayout(null);

        panel.setBounds(50, 50, 200, 200);

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

             

Explanation:

1. We create a JFrame and a JPanel.

2. We set the layout of the panel to null so we can use absolute positioning.

3. We set the bounds of the panel to be centered at (50, 50) with a fixed size of 200×200.

4. We add the panel to the frame and set the size of the frame to 300×300.

5. We use `frame.setLocationRelativeTo(null)` to center the frame on the screen.

6. Finally, we set the default close operation and make the frame visible.

Output: JFrame with a JPanel centered at (50, 50).

Example 2: Using GridBagLayout

             java
import javax.swing.*;
import java.awt.*;

public class CenteredPanelExample2 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Centered JPanel Example 2");
        JPanel panel = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;

        panel.setPreferredSize(new Dimension(200, 200));

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

             

Explanation:

1. We create a JFrame and a JPanel with GridBagLayout.

2. We create a `GridBagConstraints` object to handle the layout constraints.

3. We set the preferred size of the panel to be 200×200.

4. We add the panel to the frame and set the size of the frame to 300×300.

5. We use `frame.setLocationRelativeTo(null)` to center the frame on the screen.

6. Finally, we set the default close operation and make the frame visible.

Output: JFrame with a JPanel centered with GridBagLayout.

Example 3: Using BoxLayout

             java
import javax.swing.*;

public class CenteredPanelExample3 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Centered JPanel Example 3");
        JPanel panel = new JPanel();
        
        panel.add(Box.createHorizontalGlue());
        panel.add(new JLabel("Centered Panel"));
        panel.add(Box.createHorizontalGlue());

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

             

Explanation:

1. We create a JFrame and a JPanel.

2. We use BoxLayout to horizontally align components in the panel.

3. We add horizontal glue before and after the JLabel to center it horizontally.

4. We add the panel to the frame and set the size of the frame to 300×300.

5. We use `frame.setLocationRelativeTo(null)` to center the frame on the screen.

6. Finally, we set the default close operation and make the frame visible.

Output: JFrame with a centered panel using BoxLayout.

Example 4: Using GroupLayout

             java
import javax.swing.*;

public class CenteredPanelExample4 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Centered JPanel Example 4");
        JPanel panel = new JPanel();
        
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                    .addComponent(new JLabel("Centered Panel"))
                )
        );

        layout.setVerticalGroup(
            layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                    .addComponent(new JLabel("Centered Panel"))
                )
        );

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

             

Explanation:

1. We create a JFrame and a JPanel.

2. We create a GroupLayout for the panel and set it as the layout manager.

3. We define the horizontal and vertical group with alignment set to CENTER to center the JLabel.

4. We add the panel to the frame and set the size of the frame to 300×300.

5. We use `frame.setLocationRelativeTo(null)` to center the frame on the screen.

6. Finally, we set the default close operation and make the frame visible.

Output: JFrame with a centered panel using GroupLayout.

Example 5: Using FlowLayout

             java
import javax.swing.*;
import java.awt.*;

public class CenteredPanelExample5 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Centered JPanel Example 5");
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        panel.setPreferredSize(new Dimension(200, 200));

        frame.add(panel);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

             

Explanation:

1. We create a JFrame and a JPanel with FlowLayout and CENTER alignment.

2. We set the preferred size of the panel to be 200×200.

3. We add the panel to the frame and set the size of the frame to 300×300.

4. We use `frame.setLocationRelativeTo(null)` to center the frame on the screen.

5. Finally, we set the default close operation and make the frame visible.

Output: JFrame with a centered panel using FlowLayout.

Example 6: Using BorderLayout + Empty Borders

             java
import javax.swing.*;
import java.awt.*;

public class CenteredPanelExample6 {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Centered JPanel Example 6");
        JPanel panel = new JPanel();
        
        panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));

        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

             

Explanation:

1. We create a JFrame and a JPanel.

2. We add an empty border with specific insets to the panel, effectively centering it within the BorderLayout.CENTER of the frame.

3. We add the panel with BorderLayout.CENTER to the frame and set the size of the frame to 300×300.

4. We use `frame.setLocationRelativeTo(null)` to center the frame on the screen.

5. Finally, we set the default close operation and make the frame visible.

Output: JFrame with a centered panel using BorderLayout and empty borders.

These examples demonstrate different ways to center a fixed-size JPanel inside a JFrame using various layout managers and techniques.

Was this article helpful?
YesNo

Similar Posts