【发布时间】:2018-01-30 09:21:49
【问题描述】:
我看到有人问过类似的问题,但是我的问题是我希望舞台在整个场景更改期间保持全屏模式,即不仅仅是在结尾添加stage.setFullScreen(true),这会导致暂时但从全屏退出非常明显。在改变场景之前隐藏舞台也无济于事,因为有明显的消失。这是我的代码:
主要:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.setFullScreen(true);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器:
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Controller {
@FXML
private AnchorPane pane;
@FXML
void doSomething(ActionEvent event) throws Exception {
Stage stage = (Stage) pane.getScene().getWindow();
Parent parent = FXMLLoader.load(getClass().getResource("sample2.fxml"));
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.setFullScreen(true);
stage.show();
}
}
示例:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-
Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
<children>
<Button layoutX="180.0" layoutY="188.0" mnemonicParsing="false"
onAction="#doSomething" prefHeight="25.0" prefWidth="241.0" text="Do Something"
/>
</children>
</AnchorPane>
样本2
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-
Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: blue;"
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
<children>
<Button layoutX="180.0" layoutY="188.0" mnemonicParsing="false"
onAction="#doSomething" prefHeight="25.0" prefWidth="241.0" text="Do Something"
/>
</children>
</AnchorPane>
如您所见,如果您运行代码并按下按钮,则会暂时退出全屏。除了使用相同的 fxml 文件之外,还有什么方法可以解决这个问题?
非常感谢。
【问题讨论】:
-
所以当您加载第二个屏幕时,如果您按下退出键,您不想关闭全屏?
-
不...我只是想要从第一个到第二个的平滑过渡。现在舞台退出全屏,然后再次进入。对不起,如果我不清楚。
-
但是完全不同的是,如果您知道如何在按下转义键时不关闭全屏,那也将非常有帮助:)
标签: java