gpt4 book ai didi

java - 如何在 Java 中将框架设置为无边框全屏、窗口全屏和全屏?

转载 作者:行者123 更新时间:2023-12-01 17:42:41 25 4
gpt4 key购买 nike

我使用 GraphicsDevice 全屏显示我的 Java 窗口,但我不知道如何在应用程序运行时更改这些设置。

public void act(java.awt.Frame window) {
if (state == SCREEN.FULLSCREEN) {
if (ev.isFullScreenSupported()) {
isFullScreen = true;
window.dispose();
ev.setFullScreenWindow(window);
window.setVisible(true);
}
}
if (state == SCREEN.WINDOWED) {
isFullScreen = false;
window.dispose();
ev.setFullScreenWindow(null);
window.setUndecorated(false);
window.setVisible(true);
}
if (state == SCREEN.BORDERLESS) {
isFullScreen = true;
window.dispose();
ev.setFullScreenWindow(null);
window.setUndecorated(true);
window.setVisible(true);
}
}

当我无限地运行此代码时,代码只是一遍又一遍地创建一个新窗口,但我想让屏幕根据 state 的值而变化。

最佳答案

虽然我必须改变周围的参数,但它相当简单。对于字段,您需要:

    public enum ScreenState {
fullscreen, borderless, windowed, none
};
public static ScreenState current;
private static GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
private static GraphicsDevice ev = env.getDefaultScreenDevice();

对于该方法,您可以执行以下操作:

public void changeWindow(ScreenState applied, JFrame frame) {

if (applied == ScreenState.fullscreen && current != ScreenState.fullscreen) {
if (ev.isFullScreenSupported()) {
ev.setFullScreenWindow(frame);
}
current = ScreenState.fullscreen;
}
if (applied == ScreenState.borderless && current != ScreenState.borderless) {
frame.setUndecorated(true);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
current = ScreenState.borderless;
}
if (applied == ScreenState.windowed && current != ScreenState.windowed) {
frame.setUndecorated(false);
// you can choose to make the screen fit or not
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
current = ScreenState.windowed;
}
}

感谢您花时间帮助我!

关于java - 如何在 Java 中将框架设置为无边框全屏、窗口全屏和全屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60923760/

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