首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法以全屏独占模式获得全屏

无法以全屏独占模式获得全屏
EN

Stack Overflow用户
提问于 2015-12-09 05:30:03
回答 1查看 978关注 0票数 0

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

图片

这是我的密码

代码语言:javascript
复制
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");
}
EN

回答 1

Stack Overflow用户

发布于 2015-12-09 11:54:30

我怀疑,您的显卡和/或视频驱动程序和/或监视器无法支持您试图使用的DisplayMode

例如,最好使用由DisplayMode列出的GraphicsDevice#getDisplayModes之一.

代码语言:javascript
复制
DisplayMode[] modes = device.getDisplayModes();
for (DisplayMode mode : modes) {
    System.out.println(mode.getWidth() + "x" + mode.getHeight() + " " + mode.getBitDepth() + " @ " + mode.getRefreshRate());
}

在我的机器输出上

代码语言:javascript
复制
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失败了

所以,然后我想,该死的,我会挑出“最有可能的”火柴,然后试一试,直到有人坚持.

代码语言:javascript
复制
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。现在我还以为你可以按点位深度和刷新率对列表进行排序,但我将由你来决定和计算。

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

代码语言:javascript
复制
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());
        }

    }

}

正如显示模式轨迹中提到的

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

在Windows 10,Java 8上进行测试

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34171381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档