I just started using Java Swing and GridBagLayout, and I can't figure out how to get the button to take up all of the space I'm trying to give it (the whole window in this case). I apologize if my code looks bad, I only do Java as a hobby and I don't have formal training.
Here is the code from Main.java:
gui = new Gui();
gui.textButton("Hello World!", 1.0, 1.0, 0, 0, 1, 1);
Here is the code from Gui.java:
public class Gui extends JFrame {
private static GridBagLayout gbl = new GridBagLayout();
public JPanel panel = new JPanel();
public Gui() {
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Conlang Translator");
this.setIconImage(new ImageIcon("images\\icon.png").getImage());
this.setLayout(gbl);
this.add(panel);
this.setVisible(true);
}
public JButton textButton(String text, double weightx, double weighty, int gridx, int gridy, int gridwidth, int gridheight) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = weightx;
gbc.weighty = weighty;
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.fill = GridBagConstraints.BOTH;
JButton button = new JButton(text);
panel.add(button, gbc);
panel.revalidate();
panel.repaint();
return button;
}
}
The immediate solution would be to replace
this.setLayout(gbl)withpanel.setLayout(gbl), for example...As already stated, you could get the same effect by using a
BorderLayout, but I'm assuming that you have reasons for wanting to use aGridBagLayout