有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在更改场景时保持全屏javafx

我看到有人问过一个类似的问题,但我的问题是,我希望舞台在整个场景切换过程中保持全屏模式,即不只是在结尾添加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文件,还有什么方法可以解决这个问题吗

非常感谢


共 (1) 个答案

  1. # 1 楼答案

    我不清楚你在说什么。我认为你需要一个大小相当于电脑屏幕分辨率的舞台,并在两个屏幕之间切换。如果是,则以下文件将帮助您执行此操作。如果要从第二个场景返回第一个场景,请保留另一个按钮以加载第一个场景,或保留一个静态变量,并通过增加变量在场景之间切换,并基于奇数或偶数显示场景。比如对于变量的奇数值显示第一个场景,对于偶数值显示第二个场景

    另一个过程是让孩子们上台。这给了你一个观察者。现在清除列表,然后将各自的场景添加到列表中,并显示舞台

    主类:

    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.geometry.Rectangle2D;
    import javafx.scene.Group;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Screen;
    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");
            Scene scene = new Scene(root, 500, 200);
            primaryStage.setScene(scene);
            Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
            //set Stage boundaries to visible bounds of the main screen
            primaryStage.setX(primaryScreenBounds.getMinX());
            primaryStage.setY(primaryScreenBounds.getMinY());
            primaryStage.setWidth(primaryScreenBounds.getWidth());
            primaryStage.setHeight(primaryScreenBounds.getHeight());
            primaryStage.show();
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    控制器类:

    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.geometry.Rectangle2D;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Screen;
    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, 500, 200);
            stage.setScene(scene);
            Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
            //set Stage boundaries to visible bounds of the main screen
            stage.setX(primaryScreenBounds.getMinX());
            stage.setY(primaryScreenBounds.getMinY());
            stage.setWidth(primaryScreenBounds.getWidth());
            stage.setHeight(primaryScreenBounds.getHeight());
            stage.show();
    
        }
    }