首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当从4k监视器拖动到FullHD监视器时,应用程序会变黑

当从4k监视器拖动到FullHD监视器时,应用程序会变黑
EN

Stack Overflow用户
提问于 2017-02-21 12:13:05
回答 2查看 1.1K关注 0票数 1

我的笔记本有4k显示器。我的外部显示器是1920年。当我将我的Java Swing应用程序窗口从4k显示器拖到外部监视器上时,除了窗口框架之外,它会完全变黑。无论我是否在外部监视器上最大限度地使用它,都不重要。

下面是我从HelloWorldSwing获得的Oracle.com示例。它显示了完全相同的行为。

代码语言:javascript
复制
import javax.swing.*;        

public class HelloWorldSwing {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

看上去像是秋千上的窃听器。总之,我得找个解决办法/解决办法。有什么想法吗?

我正在运行jdk/jre 1.8.0 121。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-01 10:59:52

我找到了解决办法。基本上,我在我的主框架中添加了一个组件侦听器,并侦听移动事件。每当帧被移动时,我会检查它位于哪个显示器上。一旦框架被移动到另一个显示器上,我就将它设置为不可见,然后设置位于" new“显示上的新边界,将扩展状态设置为最大化,最后使框架再次可见。

在最后的几分钟里,我疯狂地把窗户从一个显示器拖到另一个显示器上,同时跳向joy。如果你也想在你的生活中有那么多的乐趣,下面是代码:

代码语言:javascript
复制
mainFrame.addComponentListener(new ComponentListener()
    {
        @Override
        public void componentMoved(ComponentEvent evt)
        {
            GraphicsConfiguration conf = mainFrame.getGraphicsConfiguration();
            GraphicsDevice curDisplay = conf.getDevice();
            GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

            GraphicsDevice[] allDisplays = env.getScreenDevices();
            for (int i = 0; i < allDisplays.length; i++)
            {
                if (allDisplays[i].equals(curDisplay))
                {
                    //the window has been dragged to another display
                    if (i != _currentDisplayIndex)
                    {
                        final Rectangle sb = conf.getBounds();

                        mainFrame.setVisible(false);
                        //it is important to set the bounds somewhere on the new display before setting the extended state to maximized
                        mainFrame.setBounds(sb.x, sb.y, 1000, 800);
                        mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
                        mainFrame.setVisible(true);

                        _currentDisplayIndex = i;
                    }
                }
            }

        }

        @Override
        public void componentShown(ComponentEvent evt)
        {
        }

        @Override
        public void componentResized(ComponentEvent evt)
        {
        }

        @Override
        public void componentHidden(ComponentEvent evt)
        {
        }
    });

伊皮!

旁注:我之前试着拍了一张黑窗的截图。然而,屏幕截图并不是黑色的,但通常只显示所有的swing组件。这让我相信,这不仅是一个摇摆问题,也是某种类型的窗口/显示驱动程序问题。按下"printscreen“不会触发java中的任何内容(我猜),但只会刷新显示。

票数 0
EN

Stack Overflow用户

发布于 2017-02-21 13:21:44

虽然我在任何多屏幕环境中都没有遇到这样的问题,但到目前为止,我尝试过Swing --我可能会帮助调试它。

尝试运行以下代码示例:

代码语言:javascript
复制
public class HelloWorldSwing
{
    private static void createAndShowGUI ( final GraphicsDevice device )
    {
        final GraphicsConfiguration conf = device.getDefaultConfiguration ();

        final JFrame frame = new JFrame ( "HelloWorldSwing", conf );
        frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );

        final JLabel label = new JLabel ( "Hello World", SwingConstants.CENTER );
        frame.getContentPane ().add ( label );

        final Rectangle sb = conf.getBounds ();
        frame.setBounds ( sb.x + sb.width / 2 - 100, sb.y + sb.height / 2 - 100, 200, 200 );

        frame.setVisible ( true );
    }

    public static void main ( final String[] args )
    {
        SwingUtilities.invokeLater ( new Runnable ()
        {
            @Override
            public void run ()
            {
                final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment ();
                final GraphicsDevice[] devices = env.getScreenDevices ();
                for ( final GraphicsDevice device : devices )
                {
                    if ( device.getType () == GraphicsDevice.TYPE_RASTER_SCREEN )
                    {
                        createAndShowGUI ( device );
                    }
                }
            }
        } );
    }
}

这基本上会在系统中可用的每个屏幕设备上放置一个框架。它还将为每个帧提供默认的屏幕图形配置。

检查是否有同样的问题,拖动框架到您的笔记本屏幕,以及是否有任何其他问题。

Edit1:也很重要,因为有一些相关的内部更改,所以知道您使用哪个JDK/JRE运行代码示例非常重要。如果您正在运行一些旧的JDK,那么在新的Mac版本上,这可能不是最好的主意。

Edit2:,我会尝试做一些更多的事情来了解更多:

  1. 在显示框架之前先设置Nimbus外观和感觉,然后尝试使用安装的自定义L&F拖动它们。
  2. 安装系统(本机)看上去和感觉像如本教程所示 -这可能会对Mac产生一些影响。
  3. 尝试运行Java示例并将其拖到显示器之间,看看是否存在相同的问题--您已经在使用JDK 8,因此它应该很容易完成。Java只是不久前推出的新UI框架。

总的来说,我可以说Swing对高分辨率显示的支持很差,所以即使一切运行正常,并且没有得到黑色屏幕,您也会看到其他问题,如UI模糊或低质量的文本渲染。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42367058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档