它似乎是Windows的Java实现中的错误。(我也在使用Windows 7和JDK7。)
当Windows决定更改窗口的高度时
COMPONENT_MOVED,Window会收到一个事件,但不会
COMPONENT_RESIZED收到任何事件。
在Windows类内部,非公共方法
dispatchEventImpl()将
COMPONENT_RESIZED通过调用
invalidate()和来响应事件
validate(),但它将忽略
COMPONENT_MOVED事件。
这是使窗口在发生此类事件后重新验证自身的蛮力方法。这可能在其他情况下偶尔使窗口重新验证,但不是很常见,因为Window类本身在每个
COMPONENT_RESIZED事件之后都在进行重新验证,并且所报告的错误仅在用户正在主动调整窗口大小时发生。
masterWindow.addComponentListener(new ComponentAdapter() { private int oldWidth = 0; private int oldHeight = 0; @Override public void componentResized(ComponentEvent e) { oldWidth = masterWindow.getWidth(); oldHeight = masterWindow.getHeight(); } @Override public void componentMoved(ComponentEvent e) { if (masterWindow.getWidth() != oldWidth || masterWindow.getHeight() != oldHeight) { masterWindow.invalidate(); masterWindow.validate(); } oldWidth = masterWindow.getWidth(); oldHeight = masterWindow.getHeight(); } });


