【问题标题】:Can not get full screen with full screen exclusive mode [duplicate]无法使用全屏独占模式获得全屏[重复]
【发布时间】:2015-12-09 19:54:30
【问题描述】:

我想将 JFrame 设为全屏并将显示模式更改为 1280*720 但 JFrame 不是全屏。

pic

这是我的代码

JFrame f = new JFrame("Test");
f.setUndecorated(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GraphicsDevice device = GraphicsEnvironment
        .getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (device.isFullScreenSupported()) {
    device.setFullScreenWindow(f);
    if (device.isDisplayChangeSupported()) {
        try {
            DisplayMode dm = new DisplayMode(1280, 720, 32, 60);
            device.setDisplayMode(dm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        } else {
        System.err.println("Change display mode not supported");
    }
} else {
    System.err.println("Full screen not supported");
}

【问题讨论】:

  • 适用于我,Windows 7,Java 8。看起来您使用的是 Windows 10?
  • @MadProgrammer 是的,我使用的是 Windows 10,我尝试在 Windows 8 上运行,但它也不起作用。
  • @NoppawitThairungroj 然后看看链接的“可能重复”,这似乎是合理的选择
  • @MadProgrammer 我可以更改显示模式而不会出现任何错误,但它仍然不是全屏,所以我认为我的问题没有重复?对不起,如果我错了,我是初学者。
  • 但是根据你的屏幕截图,它并没有改变屏幕模式

标签: java swing jframe fullscreen


【解决方案1】:

我怀疑,您的显卡和/或视频驱动程序和/或显示器不支持您尝试使用的DisplayMode

最好使用DisplayModes 之一,它由 GraphicsDevice#getDisplayModes,比如……

DisplayMode[] modes = device.getDisplayModes();
for (DisplayMode mode : modes) {
    System.out.println(mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate());
}

哪个,在我的机器上输出

640x480 32 @ 60
640x480 32 @ 75
720x480 32 @ 60
720x480 32 @ 75
720x576 32 @ 60
720x576 32 @ 75
800x600 32 @ 60
800x600 32 @ 75
1024x768 32 @ 60
1024x768 32 @ 75
1152x864 32 @ 75
1280x720 32 @ 60
1280x720 32 @ 75
1280x768 32 @ 60
1280x768 32 @ 75
1280x800 32 @ 60
1280x800 32 @ 75
1280x960 32 @ 60
1280x960 32 @ 75
1280x1024 32 @ 60
1280x1024 32 @ 75
1360x768 32 @ 60
1366x768 32 @ 60
1600x900 32 @ 60
1600x1024 32 @ 60
1600x1200 32 @ 60
1680x1050 32 @ 59
1680x1050 32 @ 60
1920x1080 32 @ 59
1920x1080 32 @ 60
1920x1200 32 @ 59
1920x1200 32 @ 60

如您所见,1280x720 32 @ 60 被列为可用模式之一,您的代码无需修改即可在我的机器上正常运行。

我确实尝试使用DisplayMode dm = new DisplayMode(1280, 720, DisplayMode.BIT_DEPTH_MULTI, DisplayMode.REFRESH_RATE_UNKNOWN);,但使用java.lang.IllegalArgumentException: Invalid display mode 失败

所以,然后我想,操蛋,我会挑选出“最”可能的匹配项,然后尝试直到找到一个为止...

try {
    List<DisplayMode> matchingModes = new ArrayList<>(25);

    DisplayMode[] modes = device.getDisplayModes();
    for (DisplayMode mode : modes) {
        if (mode.getWidth() == 1280 && mode.getHeight() == 720) {
            matchingModes.add(mode);
        }
    }

    if (!matchingModes.isEmpty()) {
    for (DisplayMode mode : matchingModes) {
        try {
            device.setDisplayMode(mode);
            System.out.println(mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate());
            break;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    } else {
        System.err.println("!! No matching modes available");
    }
} catch (Exception e) {
    e.printStackTrace();
}

最终使用1280x720, 32 @ 60。现在我还认为您可以按位深度和刷新率的顺序对列表进行排序,但我将把它留给您来决定和解决

这基本上是我的测试代码...

import java.awt.DisplayMode;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JavaApplication155 {

    public static void main(String[] args) {
        JFrame f = new JFrame("Test");
        f.setUndecorated(true);
        f.add(new TestPane());
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GraphicsDevice device = GraphicsEnvironment
                        .getLocalGraphicsEnvironment().getDefaultScreenDevice();
        if (device.isFullScreenSupported()) {
            device.setFullScreenWindow(f);
            if (device.isDisplayChangeSupported()) {
                try {
                    List<DisplayMode> matchingModes = new ArrayList<>(25);

                    DisplayMode[] modes = device.getDisplayModes();
                    for (DisplayMode mode : modes) {
                        if (mode.getWidth() == 1280 && mode.getHeight() == 720) {
                            matchingModes.add(mode);
                        }
                    }

                    if (!matchingModes.isEmpty()) {
                        for (DisplayMode mode : matchingModes) {
                            try {
                                device.setDisplayMode(mode);
                                System.out.println(mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate());
                                break;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        System.err.println("!! No matching modes available");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                System.err.println("Change display mode not supported");
            }
        } else {
            System.err.println("Full screen not supported");
        }
    }

    public static class TestPane extends JPanel {

        public TestPane() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    SwingUtilities.windowForComponent(TestPane.this).dispose();
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            String text = getWidth() + "x" + getHeight();
            FontMetrics fm = g.getFontMetrics();
            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = (getHeight() - fm.getHeight()) / 2;
            g.drawString(text, x, y + fm.getAscent());

            GraphicsDevice device = GraphicsEnvironment
                            .getLocalGraphicsEnvironment().getDefaultScreenDevice();

            DisplayMode mode = device.getDisplayMode();
            text = mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate();
            x = (getWidth() - fm.stringWidth(text)) / 2;
            y += fm.getHeight();
            g.drawString(text, x, y + fm.getAscent());
        }

    }

}

the Display Mode trail中所述

在为您的应用程序选择显示模式时,您可能希望保留首选显示模式列表,然后从可用显示模式列表中选择最佳模式。

在 Windows 10、Java 8 上测试

【讨论】:

    猜你喜欢
    • 2014-05-18
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多