首先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态,这样是不对的。