开发者

Finding the restore bounds of a JFrame when it is in a maximized state

When a JFrame is in a maximized state, the getBounds method returns bounds that match its current state. What I need is the "restore bounds" (that is, the size/location of what the window would be set to if a user clicked on "Restore Down" on a Windows platform) without actually setting it to a non-maximized state.

The reason I ask is because I wish to save the bounds as a preference and then when the user reopens the application, set the window to the saved bounds (and then maximize it if that is what the last state was).

I considered adding a listener for when a window is maximized but it seems to me that the liste开发者_如何学运维ner is notified after its maximized and I'll need to be notified right before it is maximized.


I store the extendedState, window location and frame size in the user preferences in my application. Adding a ComponentListener is the way to go. You will have to listen to the componentMoved, componentResized, and componentShown methods. When the value returned from frame.getExtendedState() == JFrame.NORMAL, store the Rectangle from frame.getBounds() as your non-maximized value. If getExtendedState() returns JFrame.MAXIMIZED_BOTH, then don't store the current bounds, but do store the fact that it is extended. As long as you only store the value from the last JFrame.NORMAL extended state, you will be good.


I considered adding a listener for when a window is maximized but it seems to me that the listener is notified after its maximized and I'll need to be notified right before it is maximized.

That should work. Add a listener to handle component resized. When the event is fired, before updating the saved bounds, you check to make sure that the window is not in its maximized state.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜