Skip to content
Advertisement

GridBagLayout strange behaviour

I wrote this simple class to test GridBagLayout.

My scope was to put some buttons in diagonal into panel, using grid bag layout and setting gridx and gridy, but I have a strange behaviour. If I put gridwidth = 2 on button "2", button "3" will be drawn under button labeled "2".

My class is a simple demo, but I cant figure out what is wrong with it Guess Why?

JavaScript

Advertisement

Answer

I created the following unusual GUI

Button GUI

Here’s what I did.

  1. You only need one instance of GridBagConstraints.

  2. I made all of the gridwidth values 1.

  3. I set gbc.fill to GridBagConstraints.NONE.

Here’s the code I ran.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement