首先jdk的setExtendedState是有bug的,需要先重載JFrame的setExtendedState方法
/**
* Fix the bug "jframe undecorated cover taskbar when maximized". See:
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4737788
*
* @param state
*/
@Override
public void setExtendedState(int state) {
if ((state & java.awt.Frame.MAXIMIZED_BOTH) == java.awt.Frame.MAXIMIZED_BOTH) {
Rectangle bounds = getGraphicsConfiguration().getBounds();
Rectangle maxBounds = null;
// Check to see if this is the 'primary' monitor
// The primary monitor should have screen coordinates of (0,0)
if (bounds.x == 0 && bounds.y == 0) {
Insets screenInsets = getToolkit().getScreenInsets(getGraphicsConfiguration());
maxBounds = new Rectangle(screenInsets.left, screenInsets.top, bounds.width - screenInsets.right - screenInsets.left
, bounds.height - screenInsets.bottom - screenInsets.top);
} else {
// Not the primary monitor, reset the maximized bounds...
maxBounds = null;
}
super.setMaximizedBounds(maxBounds);
}
super.setExtendedState(state);
}
然后,最大化的時候就設置:
setExtendedState(Frame.MAXIMIZED_BOTH);
但是,最小化的時候,需要注意,設置成:
super.setExtendedState(Frame.ICONIFIED | getExtendedState());
否則,假如直接設置:super.setExtendedState(Frame.ICONIFIED);
還原的時候Sate在~Frame.ICONIFIED之后成了0,就變成了NORMAL態,這樣是不對的。
