【问题标题】:Swing Paint Issues On Secondary Monitor?辅助显示器上的摆动油漆问题?
【发布时间】:2018-01-31 23:09:47
【问题描述】:

我最近遇到了一个我无法解释的问题。希望有人知道我可以做些什么来解决我的问题。我有一台运行 Windows 7 64 位的双显示器机器。两台显示器的型号和设置相同。主显示器在右侧。我正在使用 JRE 1.8.0_131

我看到的问题是,最近我发现我不再能够在我的第二台显示器上运行我的 Java Swing 应用程序。这之前工作得非常好,因为我可以在两个显示器之间移动我的应用程序,调整大小和切换到/从最大化没有问题。现在,每当我的应用程序在我的第二台显示器上时,我所看到的只是一个框架,它显示了框架后面的桌面背景部分

下面是一个为每个监视器创建一个框架的小示例程序。我的主显示器上的框架看起来不错。就像我提到的那样,我的辅助显示器上的那个搞砸了。简单地将坏帧移动到我的主显示器并不能修复它,但如果我最小化然后恢复它看起来很好(即,如果我强制帧在主显示器上重新绘制它看起来很好)。同样,将好帧移动到辅助监视器并强制重绘会导致坏帧

我的 JRE 已经有几个月没有更新了,所以这似乎不是问题。作为实验,我尝试使用 JDK 1.7.0_17,但结果相同。我的机器昨天收到了 Windows 更新,这可能是我注意到这个问题的巧合。我不是说这是问题,只是注意我拥有的信息

无论如何,任何想法或建议将不胜感激。这个问题很烦人,因为双显示器支持有助于让我的应用程序和 Eclipse 同时可见,而且我的应用程序有多个帧,我在每个显示器上放置一个帧

SwingUtilities.invokeLater(new Runnable()
{
    @Override
    public void run()
    {
        for (final GraphicsDevice device : GraphicsEnvironment
            .getLocalGraphicsEnvironment().getScreenDevices())
        {
            final DisplayMode displayMode = device.getDisplayMode();

            LOGGER.debug(
                "testSwing: device: {}, ID string: {}, isFullScreenSupported: {}, "
                    + "displayModeWidth: {}, displayModeHeight: {}, "
                    + "displayModeBitDepth: {}, displayModeRefreshRate: {}", device,
                device.getIDstring(), device.isFullScreenSupported(),
                displayMode.getWidth(), displayMode.getHeight(), displayMode.getBitDepth(),
                displayMode.getRefreshRate());

            if (GraphicsDevice.TYPE_RASTER_SCREEN == device.getType())
            {
                final GraphicsConfiguration configuration =
                    device.getDefaultConfiguration();

                final JFrame frame = new JFrame("Test Frame", configuration);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(
                    new JLabel(device.getIDstring(), SwingConstants.CENTER));

                final Rectangle screenBounds = configuration.getBounds();

                frame.setBounds((screenBounds.x + (screenBounds.width / 2)) - 100,
                    (screenBounds.y + (screenBounds.height / 2)) - 100, 200, 200);

                frame.setVisible(true);
            }
        }
    }
});

注意:LOGGER 只是一个 SLF4J 记录器,我添加它只是为了查看是否可以收集到任何有用的信息。这个可以在运行的时候去掉,因为它对上面描述的问题没有影响

在我的机器上运行它的输出:

testSwing: 设备: Win32GraphicsDevice[screen=0], ID 字符串: \Display0, isFullScreenSupported: true, displayModeWidth: 1920, displayModeHeight: 1200, displayModeBitDepth: 32, displayModeRefreshRate: 59

testSwing: 设备: Win32GraphicsDevice[screen=1], ID 字符串: \Display1, isFullScreenSupported: true, displayModeWidth: 1920, displayModeHeight: 1200, displayModeBitDepth: 32, displayModeRefreshRate: 59

【问题讨论】:

  • 无法复制。你能说明这个SwingUtilites.invokeLater() 是如何被调用的吗?我将它添加到 main() 方法中。我相信这是一个环境问题。也许 Windows 更新并不是那么巧合。
  • 可能与最近的 Windows 更新有关:answers.microsoft.com/en-us/windows/forum/windows_7-update/…
  • @a_horse_with_no_name 感谢您提供信息。我刚刚验证了其他程序(例如 Windows Media Player)表现出相同的行为 :( 我将不得不尝试解决方法

标签: java swing


【解决方案1】:

发生这种情况是因为移动您的应用程序的内容在移动到另一个监视器时没有更新,并且仍在尝试从初始监视器获取数据。

试试这个链接。它关于在多个监视器上显示 JFrame。希望这会有所帮助。

how to display JFrame in full screen display mode at a multi-monitor environment?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    相关资源
    最近更新 更多