gpt4 book ai didi

java - 如果监视器关闭,AWT/Swing 会取消绘画操作吗?

转载 作者:搜寻专家 更新时间:2023-10-30 21:34:35 26 4
gpt4 key购买 nike

我遇到了一个 Swing 问题,该问题仅在计算机显示器关闭时出现,但我的 Swing 应用程序继续在后台运行。似乎每当显示器关闭时,Swing/AWT 都会取消所有绘制操作,从而导致 GUI 中出现许多显示问题,一旦显示器重新打开,这些问题就会立即出现。

例如,当我使用自定义 JNI 函数关闭监视器并随后打开一个简单的消息对话框时,当监视器重新打开时消息对话框是空白的:

Blank message dialog

但它在下一次重绘后正确绘制:

Correctly painted message dialog

这是 Swing 的预期行为吗?有没有办法指示 Swing 继续在屏幕上绘图,即使显示器已关闭?

编辑:这是一个 SSCCE:

package test;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class App {
public static void main(String[] args) throws Throwable {
System.out.println("***** Please turn off the monitor in the next 70 seconds *****");
Thread.sleep(1000L * 70);

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(null, "Test");
}
});
}
}

我使用的是 64 位 Windows 7 家庭高级版 SP1 和 64 位 Java 1.6.0_24。

编辑 2:这是我体验“取消绘画操作”效果的另一个程序:

package test;

import static com.mycompany.Util.turnOffMonitors;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class DialogTest extends JDialog {
private final JLabel label;

public DialogTest() {
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

label = new JLabel("Test", JLabel.CENTER);
label.setOpaque(true);

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(BorderLayout.CENTER, label);

this.setPreferredSize(new Dimension(200, 110));
pack();
setLocationRelativeTo(null);
setVisible(true);

Thread t = new Thread() {
@Override
public void run() {
turnOffMonitors();
try {
Thread.sleep(3000L);
} catch (InterruptedException ex) { }

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
label.setBackground(Color.YELLOW);
}
});
}
};
t.start();
}

public static void main(String[] args) throws Throwable {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DialogTest();
}
});
}
}

在显示器关闭之前,我看到:

Screenshot of the dialog of DialogTest before the monitor shuts off

在显示器关闭的情况下,标签背景颜色在背景中变为黄色。然后我移动鼠标重新打开显示器。该对话框在视觉上没有变化。只有在我强制重绘(例如通过 ALT-TABbing)之后,我才会看到黄色:

Screenshot of the dialog of DialogTest where the main label has a yellow background

编辑 3: 向 Oracle 报告为 Bug ID 7049597 .

最佳答案

I then started the program and stopped moving the mouse/typing. After one minute, the screen turned off. I waited another 20 seconds to move the mouse. The monitor turned back on and I saw a blank message dialog.

使用您的示例,我在我的(非 Windows)平台上看不到它。您可以尝试下面的示例,它应该在唤醒时的 WINDOW_ACTIVATED 和 sleep 时的 WINDOW_DEACTIVATED 之间交替。如果是这样,您可以在 windowActivated() 中扩展 JDialogrepaint()

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

/** @see http://stackoverflow.com/questions/6163606 */
public class DialogEventTest extends JDialog {

public DialogEventTest() {
this.setLayout(new GridLayout(0, 1));
this.add(new JLabel("Dialog event test.", JLabel.CENTER));
this.add(new JButton(new AbstractAction("Close") {

@Override
public void actionPerformed(ActionEvent e) {
DialogEventTest.this.setVisible(false);
DialogEventTest.this.dispatchEvent(new WindowEvent(
DialogEventTest.this, WindowEvent.WINDOW_CLOSING));
}
}));
}

private static class WindowHandler extends WindowAdapter {

@Override
public void windowActivated(WindowEvent e) {
System.out.println(e);
}

@Override
public void windowDeactivated(WindowEvent e) {
System.out.println(e);
}
}

private void display() {
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.addWindowListener(new WindowHandler());
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new DialogEventTest().display();
}
});
}
}

关于java - 如果监视器关闭,AWT/Swing 会取消绘画操作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6163606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com