I'm currently using Java Swing and GridBagLayout to try and organize some buttons. I'm running into an issue where my "cookieClick" and "shopButton" buttons are taking up two spaces (gridx 0 & 1) when firstly, I set their grid x to 1, and set the weightx to 1.0. Any help would be great, thank you.
Swing Setup Class
package com.jetbrains;
import javax.swing.*;
import java.awt.*;
public class GUI {
final int screenX = 1280, screenY = 720;
JFrame mainGui = new JFrame();
JPanel gamePanel = new JPanel();
JPanel shopPanel = new JPanel();
JButton userName = new JButton("a"); // this is the area for the user's username they input in
JButton cookieCounter = new JButton("b"); // this is the counter for how many cookies the user currently has
JButton cookieClick = new JButton("c"); // this is the label for the cookie we click on
JButton shopButton = new JButton("d"); // this takes us to the shop, probably some if statement and set visible etc.
GridBagConstraints gameLayout = new GridBagConstraints();
public void guiConfig() {
mainGui.setSize(screenX, screenY);
mainGui.setTitle("journey");
mainGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainGui.setResizable(false);
mainGui.setBackground(Color.WHITE);
mainGui.add(gamePanel);
gamePanel.setSize(screenX, screenY);
gamePanel.setLayout(new GridBagLayout());
}
public void labelPositioning() {
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 0;
gameLayout.gridwidth = 2;
gameLayout.gridy = 0;
gameLayout.weightx = 2.0;
gameLayout.weighty = 1.0;
gamePanel.add(cookieCounter, gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 2;
gameLayout.gridwidth = 1;
gameLayout.gridy = 0;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(userName, gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 1;
gameLayout.gridwidth = 1;
gameLayout.gridy = 1;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(cookieClick, gameLayout);
gameLayout.fill = GridBagConstraints.HORIZONTAL;
gameLayout.gridx = 1;
gameLayout.gridwidth = 1;
gameLayout.gridy = 2;
gameLayout.weightx = 1.0;
gameLayout.weighty = 1.0;
gamePanel.add(shopButton, gameLayout);
}
public void frameVisibility() {
mainGui.setVisible(true);
}
}
Main Class
package com.jetbrains;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
GUI run = new GUI();
run.guiConfig();
run.labelPositioning();
run.frameVisibility();
}
}