以下代码的编写方式与我正在阅读的Java书籍完全相同

package main;

 import java.awt.Color;
 import java.awt.DisplayMode;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;

 import javax.swing.JFrame;

 public class FullScreenTest extends JFrame {
public static void main(String[] args) {
    DisplayMode displayMode;
    if (args.length == 3) {
        displayMode = new DisplayMode(Integer.parseInt(args[0]),
                Integer.parseInt(args[1]),        Integer.parseInt(args[2]),
                DisplayMode.REFRESH_RATE_UNKNOWN);

    } else {
        displayMode = new DisplayMode(800, 600, 16,
                DisplayMode.REFRESH_RATE_UNKNOWN);
    }
    FullScreenTest test = new FullScreenTest();
    test.run(displayMode);
}

private static final long DEMO_TIME = 1000;

public void run(DisplayMode displayMode) {
    setBackground(Color.blue);
    setForeground(Color.white);
    setFont(new Font("Dialog", Font.PLAIN, 24));

    SimpleScreenManager screen = new SimpleScreenManager();
    try {
        screen.setFullScreen(displayMode, this);
        try {
            Thread.sleep(DEMO_TIME);
        } catch (Exception e) {
        }

    } finally {
        screen.restoreScreen();
    }
}

public void paint(Graphics g) {

    g.drawString("Hello World!", 20, 50);
}
 }


运行此命令时,我会黑屏5秒钟,仅此而已。

但是,当我更改以下行时:

displayMode = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);


对此:

 displayMode = new DisplayMode(600, 800, 16, DisplayMode.REFRESH_RATE_UNKNOWN);


它完美地工作。

为什么会这样呢?我不太明白

谢谢,

-史蒂夫



 package main;

 import java.awt.DisplayMode;
 import java.awt.GraphicsDevice;
 import java.awt.GraphicsEnvironment;
 import java.awt.Window;

 import javax.swing.JFrame;

 public class SimpleScreenManager {

private GraphicsDevice device;

public SimpleScreenManager() {
    GraphicsEnvironment environment = GraphicsEnvironment
            .getLocalGraphicsEnvironment();
    device = environment.getDefaultScreenDevice();
}

public void setFullScreen(DisplayMode displayMode, JFrame window) {
    window.setUndecorated(true);
    window.setResizable(false);

    device.setFullScreenWindow(window);

    if (displayMode != null && device.isDisplayChangeSupported()) {

        try {
            device.setDisplayMode(displayMode);
        } catch (Exception e) {

        }

    }
}

public Window getFullScreenWindow() {
    return device.getFullScreenWindow();
}

public void restoreScreen() {
    Window window = device.getFullScreenWindow();
    if (window != null) {
        window.dispose();
    }
    device.setFullScreenWindow(null);
}
 }

最佳答案

DisplayMode与Java“全屏独占模式”功能有关,在此处介绍:


http://docs.oracle.com/javase/tutorial/extra/fullscreen/displaymode.html
http://docs.oracle.com/javase/7/docs/api/java/awt/DisplayMode.html


我鼓励您在这里尝试一些示例程序:


http://docs.oracle.com/javase/tutorial/extra/fullscreen/example.html


底线是:

1)此API不一定支持所有操作系统或显示设备

2)如果设备本身不支持任意DisplayMode设置,则不一定支持所有组合

08-04 09:57