Program not repainting upon JFrame height maximizing

前端 未结 2 1006
长情又很酷
长情又很酷 2021-01-07 00:07

You can resize JFrame with mouse dragging in several ways:

  1. You can resize it\'s height (top/bottom edge)
  2. You can resize it\'s width (left/right edge)<
2条回答
  •  日久生厌
    2021-01-07 00:24

    It appears to be a bug in the Windows implementation of Java. (I am also using Windows 7 and JDK7.)

    When Windows decides to change the height of the window, a COMPONENT_MOVED event is received by Window, but no COMPONENT_RESIZED event is received.

    Inside the Windows class, the non-public method dispatchEventImpl() will respond to COMPONENT_RESIZED events by calling invalidate() and validate(), but it will ignore COMPONENT_MOVED events.

    Here is a brute-force method of making the window re-validate itself after such an event. This may occasionally make the window re-validate in other situations, but not very often since the Window class itself is doing re-validation after every COMPONENT_RESIZED event, and the reported bug only happens when the window is being actively resized by the user.

        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();
          }
        });
    

提交回复
热议问题