Georodin
Georodin

Reputation: 341

Position a JButton properly within a GridBagLayout | Java 8

I want to position a JButton "Start" in the lower center of my GUI. Sadly the GridBagConstraints that I set seem to have no effect. I'm working with jdk1.8.0_191 and the Eclipse IDE.

EDIT: I switched my snippet to a "minimal reproducible example". Here the snippet of my code:

package gui.main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test{
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame(); 
        frame.setSize(646, 509);
        frame.setResizable(false);
        frame.setTitle("Adventure Time");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
            JPanel panel = new JPanel();
            panel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            
            gbc.gridx = 1;
            gbc.gridy = 5;
            
                JButton button = new JButton("Start");  
                button.setBackground(Color.black);      
                button.setForeground(Color.white);  
                button.setPreferredSize(new Dimension(110, 60));
                button.setMinimumSize(new Dimension(110, 60));
                button.setVisible(true);
                
            panel.add(button, gbc);
            panel.setVisible(true);
        
        frame.add(panel);
        frame.setVisible(true);
        
    }
}

Here is a picture where I approximately want the "Start" Button instead.

enter image description here

Upvotes: 0

Views: 168

Answers (2)

Georodin
Georodin

Reputation: 341

In my amateurish opinion:

I ended up using different insets for my small application. It is more clean looking in my opinion.

   //margin 400 top, rest 3
   gbc.insets = new Insets(400,3,3,3);
   gbc.gridy = 0;
   getWrapperPanel().add(getBtnStart02(), gbc);
    
   //margin 3 at all sides for all following gui objects
   gbc.insets = new Insets(3,3,3,3);
   gbc.gridy = 1;
   getWrapperPanel().add(getBtnLoad(), gbc);

Works like a charm.

Upvotes: 1

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51485

I created a simple, runnable example of a GUI with a start button on the lower fourth of the JPanel. Here's the example GUI.

Start button GUI

Use Swing components. Don't extend Swing components, or any other Java class, unless you intend to override one of the methods in the class.

I used a BorderLayout for the JPanel. I used a calculated empty border to shift the start button down from the center.

I set the size of the JPanel in this example. I'm assuming you'll set the size of the game panel based on the size of your image.

Here's the code.

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class StartButtonExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new StartButtonExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createMainPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        Dimension panelSize = new Dimension(400, 400);
        panel.setPreferredSize(panelSize);

        JButton button = new JButton("Start");
        Dimension buttonSize = button.getPreferredSize();

        int height = panelSize.height - buttonSize.height;
        int width = panelSize.width - buttonSize.width;
        int top = height * 3 / 4;
        int left = (width - buttonSize.width) / 2;
        int bottom = height / 4;
        int right = left;
        panel.setBorder(BorderFactory.createEmptyBorder(
                top, left, bottom, right));

        panel.add(button, BorderLayout.CENTER);

        return panel;
    }

}

Upvotes: 2

Related Questions