Javafx 2 JavaFX全屏-根据屏幕大小调整元素大小
有没有办法让全屏(如果可能的话,也可以调整大小)而不是重新安排一切(实际上它做的是重新安排元素,比如调整大小,但整个屏幕),从而形成一个真正的全屏模式?(就像通常改变屏幕分辨率的游戏一样),因此按钮和文本会随着屏幕/窗口的大小而增长Javafx 2 JavaFX全屏-根据屏幕大小调整元素大小,javafx-2,fullscreen,Javafx 2,Fullscreen,有没有办法让全屏(如果可能的话,也可以调整大小)而不是重新安排一切(实际上它做的是重新安排元素,比如调整大小,但整个屏幕),从而形成一个真正的全屏模式?(就像通常改变屏幕分辨率的游戏一样),因此按钮和文本会随着屏幕/窗口的大小而增长 另外,我如何删除消息以及单击“esc”键退出全屏模式时的效果 编辑:使用此方法可调整大小 @Override public void start(Stage stage) throws Exception{ final int initWidth = 720
另外,我如何删除消息以及单击“esc”键退出全屏模式时的效果 编辑:使用此方法可调整大小
@Override public void start(Stage stage) throws Exception{
final int initWidth = 720; //initial width
final int initHeight = 1080; //initial height
final Pane root = new Pane(); //necessary evil
Pane controller = new CtrlMainMenu(); //initial view
controller.setPrefWidth(initWidth); //if not initialized
controller.setPrefHeight(initHeight); //if not initialized
root.getChildren().add(controller); //necessary evil
Scale scale = new Scale(1, 1, 0, 0);
scale.xProperty().bind(root.widthProperty().divide(initWidth)); //must match with the one in the controller
scale.yProperty().bind(root.heightProperty().divide(initHeight)); //must match with the one in the controller
root.getTransforms().add(scale);
final Scene scene = new Scene(root, initWidth, initHeight);
stage.setScene(scene);
stage.setResizable(true);
stage.show();
//add listener for the use of scene.setRoot()
scene.rootProperty().addListener(new ChangeListener<Parent>(){
@Override public void changed(ObservableValue<? extends Parent> arg0, Parent oldValue, Parent newValue){
scene.rootProperty().removeListener(this);
scene.setRoot(root);
((Region)newValue).setPrefWidth(initWidth); //make sure is a Region!
((Region)newValue).setPrefHeight(initHeight); //make sure is a Region!
root.getChildren().clear();
root.getChildren().add(newValue);
scene.rootProperty().addListener(this);
}
});
}
@Override public void start(Stage)引发异常{
final int initWidth=720;//初始宽度
final int initHeight=1080;//初始高度
最终窗格根=新窗格();//必要的错误
窗格控制器=新建CtrlMainMenu();//初始视图
controller.setPrefWidth(initWidth);//如果未初始化
controller.setPrefHeight(initHeight);//如果未初始化
root.getChildren().add(控制器);//必要的错误
比例比例=新比例(1,1,0,0);
scale.xProperty().bind(root.widthProperty().divide(initWidth));//必须与控制器中的匹配
scale.yProperty().bind(root.heightProperty().divide(initHeight));//必须与控制器中的匹配
root.getTransforms().add(scale);
最终场景场景=新场景(根、初始宽度、初始高度);
舞台场景;
stage.setresizeable(true);
stage.show();
//添加侦听器以使用scene.setRoot()
scene.rootProperty().addListener(新的ChangeListener()){
@覆盖已更改的公共void(observeValue有几种方法可以调整UI的大小
按字体大小缩放
通过在场景样式表的.root
中设置-fx font size
,可以缩放所有控件
例如,如果将以下样式表应用于场景,则所有控件的大小都将加倍(因为默认字体大小为13px)
.根{
-fx字体大小:26px;
}
以上内容适用于缩放控件,这对于完全基于控件的对象很好,但对于基于图形和形状的对象则不太好
按变换缩放
将以(0,0)为轴的变换应用于场景的根节点
Scale scale = new Scale(scaleFactor, scaleFactor);
scale.setPivotX(0);
scale.setPivotY(0);
scene.getRoot().getTransforms().setAll(scale);
为了扩展我开发的一个包含图形和各种形状的游戏,我使用了字母装箱技术,将游戏窗口的大小调整为恒定的纵横比(类似于在16:9屏幕上观看4:3电视节目时看到的字母装箱)
下面代码中的SceneSizeChangeListener侦听场景大小的更改,并根据可用场景大小缩放场景内容
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import org.jewelsea.games.supersnake.layout.LayoutController;
import java.io.IOException;
import java.util.ResourceBundle;
/* Main JavaFX application class */
public class SuperSnake extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(final Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(
getClass().getResource("layout/layout.fxml"),
ResourceBundle.getBundle("org.jewelsea.games.supersnake.layout.text")
);
Pane root = (Pane) loader.load();
GameManager.instance().setLayoutController(loader.<LayoutController>getController());
Scene scene = new Scene(new Group(root));
stage.setScene(scene);
stage.show();
GameManager.instance().showMenu();
letterbox(scene, root);
stage.setFullScreen(true);
}
private void letterbox(final Scene scene, final Pane contentPane) {
final double initWidth = scene.getWidth();
final double initHeight = scene.getHeight();
final double ratio = initWidth / initHeight;
SceneSizeChangeListener sizeListener = new SceneSizeChangeListener(scene, ratio, initHeight, initWidth, contentPane);
scene.widthProperty().addListener(sizeListener);
scene.heightProperty().addListener(sizeListener);
}
private static class SceneSizeChangeListener implements ChangeListener<Number> {
private final Scene scene;
private final double ratio;
private final double initHeight;
private final double initWidth;
private final Pane contentPane;
public SceneSizeChangeListener(Scene scene, double ratio, double initHeight, double initWidth, Pane contentPane) {
this.scene = scene;
this.ratio = ratio;
this.initHeight = initHeight;
this.initWidth = initWidth;
this.contentPane = contentPane;
}
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
final double newWidth = scene.getWidth();
final double newHeight = scene.getHeight();
double scaleFactor =
newWidth / newHeight > ratio
? newHeight / initHeight
: newWidth / initWidth;
if (scaleFactor >= 1) {
Scale scale = new Scale(scaleFactor, scaleFactor);
scale.setPivotX(0);
scale.setPivotY(0);
scene.getRoot().getTransforms().setAll(scale);
contentPane.setPrefWidth (newWidth / scaleFactor);
contentPane.setPrefHeight(newHeight / scaleFactor);
} else {
contentPane.setPrefWidth (Math.max(initWidth, newWidth));
contentPane.setPrefHeight(Math.max(initHeight, newHeight));
}
}
}
}
导入javafx.application.application;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.fxml.fxmloader;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.layout.Pane;
导入javafx.scene.transform.Scale;
导入javafx.stage.stage;
导入org.jewelsea.games.supersnake.layout.LayoutController;
导入java.io.IOException;
导入java.util.ResourceBundle;
/*主JavaFX应用程序类*/
公共类SuperSnake扩展了应用程序{
公共静态void main(字符串[]args){launch(args);}
@覆盖公共无效开始(最终阶段)引发IOException{
FXMLLoader=新的FXMLLoader(
getClass().getResource(“layout/layout.fxml”),
getBundle(“org.jewelsea.games.supersnake.layout.text”)
);
窗格根=(窗格)加载程序。加载();
GameManager.instance().setLayoutController(loader.getController());
场景=新场景(新组(根));
舞台场景;
stage.show();
GameManager.instance().showMenu();
信箱(场景,根);
stage.setFullScreen(真);
}
专用空白信箱(最终场景场景,最终窗格内容窗格){
final double initWidth=scene.getWidth();
final double initHeight=scene.getHeight();
最终倍率=初始宽度/初始高度;
SceneSizeChangeListener sizeListener=新SceneSizeChangeListener(场景、比率、初始高度、初始宽度、内容窗格);
scene.widthProperty().addListener(sizeListener);
scene.heightProperty().addListener(sizeListener);
}
私有静态类SceneSizeChangeListener实现ChangeListener{
私人最终场景;
私人最终双倍比率;
私人最终双倍身高;
私人最终双宽度;
私有最终窗格内容窗格;
公共场景更改侦听器(场景场景、双比率、双初始高度、双初始宽度、窗格内容窗格){
这个场景=场景;
这个比率=比率;
this.initHeight=initHeight;
this.initWidth=initWidth;
this.contentPane=contentPane;
}
@凌驾
public void changed(ObservalEvalue“另外,我如何删除消息以及单击“esc”键退出全屏模式时的效果?”
使用此代码:
stage.setFullScreenExitHint("");
它会将字符串消息“按Esc退出全屏模式”更改为空字符串,这样它就不会显示。您可以将其复制到JavaFXApplication中
Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize();
double width = resolution.getWidth();
double height = resolution.getHeight();
double w = width/1280; // your window width
double h = height/720; // your window height
Scale scale = new Scale(w, h, 0, 0);
root.getTransforms().add(scale);
非常感谢,尝试了一点代码,似乎很有效,尽管我没想到使用全屏会那么麻烦和困难…稍微修改了侦听器的代码,但更改根的宽度/高度不起作用,因此当场景比原始场景变大时,根元素会超出它,但当它变小时r、 没有涵盖整个场景,可以帮我解决这个问题吗?代码在以下链接上:(场景我以以下方式初始化场景s=新场景(新控制器()),其中控制器扩展了一个窗格(可以随我更改窗格而变化);抱歉,您的解决方案与我的有点不同,我无法真正为您调试它。我确实准备了一个案例,以防可能出现这种情况