Java 重新连接显示输出时,如何刷新GraphicsEnvironment.getLocalGraphicsEnvironment()结构?
问题: Java/Swing应用程序,在每个连接的显示器上显示单独的Java 重新连接显示输出时,如何刷新GraphicsEnvironment.getLocalGraphicsEnvironment()结构?,java,swing,refresh,monitor,Java,Swing,Refresh,Monitor,问题: Java/Swing应用程序,在每个连接的显示器上显示单独的JFrames。 我从GraphicsEnvironment.getLocalGraphicsEnvironment() 结构无效,因为应用程序启动且数据未动态更新 连接新显示器或断开现有显示器时。有没有什么 应用程序运行期间图形环境的“强制刷新”或“更新”过程 由GraphicsEnvironment.getLocalGraphicsEnvironment()返回的GraphicsEnvironment已在内部缓存,不再创建
JFrame
s。
我从GraphicsEnvironment.getLocalGraphicsEnvironment()
结构无效,因为应用程序启动且数据未动态更新
连接新显示器或断开现有显示器时。有没有什么
应用程序运行期间图形环境的“强制刷新”或“更新”过程 由
GraphicsEnvironment.getLocalGraphicsEnvironment()
返回的GraphicsEnvironment
已在内部缓存,不再创建
如果您认为配置已更改,您可以自己实例化它。GraphicsEnvironment
实现的类名存储在的“java.awt.graphicsenv”
系统属性中:
String className = System.getProperty("java.awt.graphicsenv");
GraphicsEnvironment ge = (GraphicsEnvironment) Class.forName(className)
.newInstance();
但是必须小心,因为即使您看到新的配置,使用GraphicsEnvironment.getLocalGraphicsEnvironment()
的标准库的类也不会知道这一点 @icza
谢谢你指点我。
出于测试目的,我根据您的建议编写了一个简单的代码:
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
public class GuiScreens {
public static void main(String[] args) {
while(true){
String className = System.getProperty("java.awt.graphicsenv");
try {
GraphicsEnvironment ge = (GraphicsEnvironment) Class.forName(className)
.newInstance();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
System.out.println("Device " + j + ": " + gd);
}
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
System.out.println("--------------------------------");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
导入java.awt.GraphicsConfiguration;
导入java.awt.GraphicsDevice;
导入java.awt.GraphicsEnvironment;
导入java.awt.Rectangle;
公共类GUI屏幕{
公共静态void main(字符串[]args){
while(true){
字符串className=System.getProperty(“java.awt.graphicsenv”);
试一试{
GraphicsEnvironment ge=(GraphicsEnvironment)Class.forName(className)
.newInstance();
GraphicsDevice[]gs=ge.getScreenDevices();
对于(int j=0;j
不幸的是,这不起作用。我错过了什么