我想将JFrame全屏显示并将显示模式更改为1280 * 720,但JFrame不是全屏。
这是我的代码
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");
}
答案 0 :(得分:1)
我的怀疑是,您的显卡和/或视频驱动程序和/或显示器无法支持您尝试使用的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());
}
}
}
中所述
为应用程序选择显示模式时,您可能需要保留首选显示模式列表,然后从可用显示模式列表中选择最佳显示模式。
在Windows 10,Java 8上测试