user577304
user577304

Reputation:

Both maximizing and preventing resizing on a JFrame?

I am making a game, so I want the JFrame to start maximized, but I also don't want users resizing the game, as it would screw up the graphics. I am wondering how to do both at the same time?

Right now, I can use JFrame.setExtendedState(JFrame.MAXIMIZED_BOTH) to maximize, but as soon as I do JFrame.setResizeable(false), I can't use setExtendedState() to maximize it anymore.

I realize I can just maximize it, store the width and height, then use that, but if possible I would prefer for the window to actually look like it is maximized. The title bar changes when it is maximized versus when it is just sized to fill the screen, if anyone knows what I am talking about.

Upvotes: 5

Views: 10231

Answers (2)

gersonZaragocin
gersonZaragocin

Reputation: 1202

If you are using JSR296 this is the way it works inside the singleton Application class:

    JFrame main = view.getFrame();
    main.setExtendedState(JFrame.MAXIMIZED_BOTH);
    main.pack();
    show(view);
    main.setResizable(false);

Without the pack() operation the window is not maximized. This was tested using Linux and Java 6.

Upvotes: 2

camickr
camickr

Reputation: 324197

This works for me:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
frame.setResizable(false);

Upvotes: 6

Related Questions