Question

I am making a calculator. I used a GridBagLayout to arrange the buttons, and so far it looks like this:

normal calculator look

I am trying to find the exact size of the grid so I can set the size of the JFrame to match it. In the GridBagLayout API, I found three methods: maximumLayoutSize, minimumLayoutSize, and preferredLayoutSize. I tried all three and set the JFrame to each size to see how they worked:

When I looked at the dimensions returned by maximumLayoutSize, they were huge (millions and billions).

When I used the dimensions returned by minimumLayoutSize, I got width=151, height=150, which looks like this:

enter image description here

When I used the dimensions returned by preferredLayoutSize, I got width=220, height=180, which looks like this:

enter image description here

Instead, I want the dimensions of the GridBagLayout when it looks normal like it is in the first picture. How can I get these dimensions?

Was it helpful?

Solution

Call, JFrame#pack, it will pack the frame based on the needs of the frame decorations and the preferred size of the frames content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top