【问题标题】:How to maintain full screen when changing scenes javafxjavafx切换场景时如何保持全屏
【发布时间】: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


【解决方案1】:

我没有清楚地理解你在说什么。我认为您需要一个大小等于计算机屏幕分辨率的舞台并在两个屏幕之间切换。如果是,那么以下文件将帮助您这样做。如果您想从第二个场景返回到第一个场景,请保留另一个按钮来加载第一个场景或保留一个静态变量并通过增加变量在场景之间切换并根据奇数或偶数显示场景。就像变量的奇数值显示第一个场景,偶数值显示第二个场景。

其他过程是获取舞台的孩子。这给了你一个 ObservableList。现在清除列表,然后将相应的场景添加到列表中并显示舞台。

主类:

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();

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 2020-03-23
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多