程序不重新绘制JFrame高度最大化 [英] Program not repainting upon JFrame height maximizing

查看:95
本文介绍了程序不重新绘制JFrame高度最大化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以通过以下几种方式拖动鼠标来调整JFrame的大小:

You can resize JFrame with mouse dragging in several ways:


  1. 您可以调整其高度(上/下边缘)

  2. 你可以调整它的宽度(左/右边缘)

  3. 你可以调整两个角度(在角落里)

  4. 你通过将整个窗口拖动到显示器顶部边缘可以最大化它

  5. 您可以通过将其顶部/底部边缘拖动到显示器的顶部/底部边缘来最大化它的高度

  1. You can resize it's height (top/bottom edge)
  2. You can resize it's width (left/right edge)
  3. You can resize both (in corners)
  4. You can maximize it by dragging the whole window to the monitor top edge
  5. You can maximize it's height by dragging it's top/bottom edge to the top/bottom edge of the monitor

除了数字5之外,我的程序正在重新绘制每个动作。

My program is being repainted on every one of this actions except the number 5.

为什么那是吗?

这可能是我程序中的错误吗?它似乎并非如此。我不知道我会在哪里为那个特殊情况添加重绘请求......似乎JFrame本身应该在每次调整大小后调用它的contentPane上重绘,对吗?

Could that be bug in my program? It doesn't seem so. I don't know where would I put repaint request for that particular case... It seems that JFrame itself should call repaint on it's contentPane after each resize, right?

它在JFrame类中闻起来像Java本身的一个bug。

It smells like a bug in Java itself, in JFrame class.

SSCCE:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

class Gui
{
    Gui()
    {
        JFrame masterWindow = new JFrame("My very own SSCCE");

        masterWindow.setSize(1100, 100);
        masterWindow.setLocationRelativeTo(null);
        masterWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        masterWindow.setVisible(true);
    }
}

class Main
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Gui();
            }
        });
    }
}

抓住JFrame的上边缘并将其拖动到屏幕的上边缘和释放。底部延伸部分应为黑色。如果你然后最大化它,它将是灰色的,应该是。

Grab the top edge of the JFrame and drag it to the top edge of the screen and release. Bottom extended part should be pitch black. If you then maximize it, it will all be grey, as it should.

我仍然说它是Java或Windows bug。

I still say it's Java or Windows bug.

推荐答案

这似乎是Windows的Windows实现中的一个错误。 (我也在使用Windows 7和JDK7。)

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

当Windows决定更改窗口高度时, COMPONENT_MOVED Window收到事件,但未收到 COMPONENT_RESIZED 事件。

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.

在Windows类中,非公共方法 dispatchEventImpl()将响应 COMPONENT_RESIZED 事件通过调用 invalidate() validate(),但它将忽略 COMPONENT_MOVED events。

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.

这是一种强制方法,可让窗口在此类事件后重新验证。这可能偶尔会使窗口在其他情况下重新验证​​,但不常见,因为Window类本身在每个 COMPONENT_RESIZED 事件后都进行了重新验证,并且仅报告了错误当用户主动调整窗口大小时会发生这种情况。

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

这篇关于程序不重新绘制JFrame高度最大化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆