Open Jframe in maximized Java

Asked

Viewed 1,068 times

2

I’m having a problem, I’m creating a Jframe and I want it to open maximized. I’m using the following code:

frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setLocationRelativeTo(null);

The size of Jframe is maximized, but the maximize button is still available, so if I click it, it increases the screen a little bit more.

I want to open it really maximized with the size restore button available. Does anyone know what’s wrong with the code? Or how to do it? Thanks.

  • The frame object is of a Jframe class?

  • I took the test and it worked normally. You are doing this before or after setar setVisible?

  • It’s a Jframe yes. And I’m doing after the setVisible. I could not find a plausible explanation for not working, because in another class I use the same code and maximize right.

  • Try not to set the setExtendedState directly after setvisible.

  • That’s how I’m doing it. Right after.

  • Opa, I tried right after the setLocation and it worked!

Show 1 more comment

1 answer

1

The problem is that you are centering the frame before centering it. Center it first and then maximize:

frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);

setLocationRelativeTo

...

  • If the Component is null, or the Graphicsconfiguration Associated with this Component is null, the window is placed in the center of the screen. The center point can be obtained with the Graphicsenvironment.getCenterPoint method.

...

In free translation:

...

  • If the component is null, or the Graphicsconfiguration associated with this component is null, the window is placed in the center of the screen. The center of the point can be obtained by the Graphicsenvironment.getCenterPoint method.

...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.