gpt4 book ai didi

java - 全屏独占模式无法全屏

转载 作者:行者123 更新时间:2023-12-01 10:45:58 25 4
gpt4 key购买 nike

我想将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");
}

最佳答案

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

最好使用以下列出的 DisplayMode 之一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 中所述

When choosing a display mode for your application, you may want to keep a list of preferred display modes, then choose the best one from the list of available display modes.

在 Windows 10、Java 8 上测试

关于java - 全屏独占模式无法全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171381/

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