gpt4 book ai didi

java - Java 无法识别自定义监视器分辨率

转载 作者:搜寻专家 更新时间:2023-10-31 20:24:24 24 4
gpt4 key购买 nike

Windows 无法识别我奇怪的显示器的原始分辨率,因此我必须为其设置自定义分辨率。问题是 java 无法识别它,因为它不在 Win7 的“已批准”列表中,因此全屏模式会“卡住”。 Netbeans 无法全屏显示,所以必须有办法解决这个问题。有人知道吗?

//编辑(2010 年 3 月 29 日):看起来 NetBeans 是伪造全屏而不是真正进入全屏独占模式,所以这实际上可能无法解决。现在,我也在伪装它。不过,似乎 java 应该将 active DisplayMode 识别为有效。

这个例子重现了这个问题:


package resolutionexample;

import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();

DisplayMode currentDM = gd.getDisplayMode();

boolean currentInAvailable = false;

System.out.println("Available resolutions:");
for ( DisplayMode availDM : gd.getDisplayModes() ){
//System.out.println(availDM.getWidth() + "x" + availDM.getHeight());
if ( availDM.equals(currentDM) ){
currentInAvailable = true;
}
}

System.out.println("Current resolution: " + currentDM.getWidth() + "x" + currentDM.getHeight() );

System.out.println("Current in available: " + currentInAvailable);


JFrame frame = new JFrame("Resolution Bug Example");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

if ( !gd.isFullScreenSupported() ){System.exit(0);}

gd.setFullScreenWindow(frame);

gd.setFullScreenWindow(null);
}
});
}
}

运行 1680x1050 的输出(显示器不稳定的原始分辨率):


run:
Available resolutions:
Current resolution: 1680x1050
Current in available: false
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid display mode
at sun.awt.Win32GraphicsDevice.setDisplayMode(Win32GraphicsDevice.java:393)
at sun.awt.Win32GraphicsDevice.setFullScreenWindow(Win32GraphicsDevice.java:329)
at resolutionexample.Main$1.run(Main.java:43)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 2 seconds)

如果我在运行前将分辨率设置为 1024x768,则输出:


run:
Available resolutions:
Current resolution: 1024x768
Current in available: true
BUILD SUCCESSFUL (total time: 2 seconds)

最佳答案

卡住是什么意思?不是一个好的分辨率或没有真正的全屏模式?对于大屏幕,您可以尝试

Rectangle rect = GraphicsEnvironment.getLocalGraphicsEnvironment().
getMaximumWindowBounds();
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(rect.width, rect.height));

对于全屏切换,您可以简单地复制 sources from netbeans' MainWindow :-)

关于java - Java 无法识别自定义监视器分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2533188/

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