Ang*_*gie 5 java swing fullscreen
我无法识别我的奇怪显示器的原始分辨率,因此我必须为它设置自定义分辨率.问题是java无法识别它,因为它不在Win7的"已批准"列表中,因此全屏模式会"卡住".Netbeans出乎意料地全屏,所以必须有办法解决这个问题.谁知道呢?
//编辑(2010年3月29日):看起来NetBeans假装全屏而不是实际进入全屏独占模式,因此实际上这可能无法解决.现在,我也在假装它.看起来像java应该将活动的 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);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
输出运行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)
Run Code Online (Sandbox Code Playgroud)
如果我在运行之前将分辨率设置为1024x768,则输出:
run:
Available resolutions:
Current resolution: 1024x768
Current in available: true
BUILD SUCCESSFUL (total time: 2 seconds)
Run Code Online (Sandbox Code Playgroud)
你说的卡住是什么意思?分辨率不好或者没有真正的全屏模式?对于大屏幕你可以尝试
Rectangle rect = GraphicsEnvironment.getLocalGraphicsEnvironment().
getMaximumWindowBounds();
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(rect.width, rect.height));
Run Code Online (Sandbox Code Playgroud)
对于全屏切换,您可以简单地从 netbeans 的主窗口复制源代码:-)