从4k显示器拖至FullHD显示器时,应用程序变黑 [英] Application turns black when dragged from a 4k monitor to a FullHD monitor
问题描述
我的笔记本电脑具有4k显示屏.我的外接显示器是1920.当我将Java Swing应用程序窗口从4k显示器拖到外接显示器上时,除了窗口框架外,它会变成全黑.是否在外部显示器上最大化它都没关系.
My notebook has a 4k display. My external monitor is 1920. When I drag my Java Swing app window from the 4k display onto the external monitor it gets completely black except for the window frame. It doesn't matter whether I maximize it on the external monitor or not.
这是我从Oracle.com获得的HelloWorldSwing
示例.它显示出完全相同的行为.
Here is the HelloWorldSwing
example I got from Oracle.com. It shows the exact same behaviour.
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();
}
});
}
}
它看起来像是Swing中的错误.无论如何,我必须找到解决方案/解决方法.有什么想法吗?
It looks like a bug in Swing. Anyway, I have to find a solution/work-around. Any ideas?
我正在运行jdk/jre 1.8.0 121.
I'm running jdk/jre 1.8.0 121.
推荐答案
我找到了解决方案.基本上,我在主框架中添加了组件侦听器,并在移动事件上侦听.每当移动框架时,我都会检查框架位于哪个显示器上.将框架移到另一个显示器后,我将其设置为不可见,然后在新"显示器上设置新的边界,将扩展状态设置为最大化,最后使该框架再次可见.
I found a solution. Basically I added a component listener to my main frame and listen on move events. Whenever the frame is moved I check on which display it is located. As soon as the frame has been moved to another display I set it invisible, then set new bounds located on the "new" display, set the extended state to maximized and finally make the frame visible again.
在最后几分钟,我疯狂地将窗户从一个显示器拖到另一个显示器上,同时还为之开心.如果您还希望生活中有那么多乐趣,请使用以下代码:
For the last minutes I wildly dragged windows from one display to another all while jumping for joy. If you also want that much fun in your life, here is the code:
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)
{
}
});
Yippie!
旁注:我曾尝试对黑色窗口进行截图.但是,屏幕截图不是黑色的,而是通常显示所有摆动组件.这使我相信,这不仅是摇摆问题,还是某种Windows/显示驱动程序问题.按下"printscreen"不会触发Java中的任何操作(我想),而只会刷新显示.
Side note: I tried to take a screenshot of the black window earlier. However, the screenshot isn't black, but just normally shows all swing components. That makes me believe, that it is not only a swing issue, but also some sort of a windows/display driver issue. Pressing "printscreen" doesn't trigger anything in java (I guess), but only refreshes the display.
这篇关于从4k显示器拖至FullHD显示器时,应用程序变黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!