在Java FX中切换场景时如何保持窗口大小? [英] How can I keep my window size while switching scenes in Java FX?

查看:210
本文介绍了在Java FX中切换场景时如何保持窗口大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图弄清楚当我改变场景时为什么窗口大小(我设置为全屏)正在改变,是否有一种方法(即命令)可以发出一次让Java FX知道不改变大小吗?

I've simply been trying to make sense of why the window size (which I set to full screen) is changing when I change scenes, is there a way (i.e. a command) which can be issued once to let Java FX know not to change the size?

import javafx.application.Application;
import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;

import javafx.scene.layout.HBox;

import javafx.scene.layout.StackPane;

import javafx.stage.Stage;

public class Main extends Application {

    Scene scene,scene_2;

    public static void main(String[] args) {

        launch(args);

    }

    @Override

    public void start(Stage Win_primary) throws Exception {

        Win_primary.setTitle("AN-0");



        Button start,exit_pro,back_to_start;

        start= new Button();

        start.setText("Start");

        Image exit_door = new Image("file:C:\\Users\\MyPc\\Pictures\\FX\\vector-icons_05-128 (2).png");



        exit_pro= new Button ();
        exit_pro.setGraphic(new ImageView(exit_door));





          start.setStyle(

                "-fx-background-radius: 100em; " +

                "-fx-min-width: 200px; " +

                "-fx-min-height: 200px; " +

                "-fx-max-width: 200px; " +

                "-fx-max-height: 170px;" +

                "-fx-font-size: 20px"

        );


           exit_pro.setMaxSize(50, 50);
           exit_pro.setOnAction(e -> Win_primary.close());

        StackPane layout = new StackPane();
        layout.setStyle("-fx-background-color: #FFFFFF ;");
        layout.getChildren().add(start);
        layout.setAlignment(exit_pro, Pos.BOTTOM_CENTER);
         layout.getChildren().add(exit_pro);


        start.setOnAction(e -> {
             Win_primary.setScene(scene_2);

            });
        scene = new Scene(layout, 300, 250);


        //scene 2

        StackPane layout2 = new StackPane();
        back_to_start = new Button("Cancel");
        layout2.setStyle("-fx-background-color: #FFFFFF ;");
        layout2.getChildren().add(back_to_start);
        layout.setAlignment(back_to_start, Pos.BOTTOM_CENTER);
        scene_2 = new Scene (layout2,300,250);   
        back_to_start.setOnAction(e -> Win_primary.setScene(scene));


        Win_primary.setScene(scene);
        Win_primary.setMinHeight(700);
        Win_primary.setMinWidth(700);
        Win_primary.setMaximized(true);

        Win_primary.setFullScreen(true);

        Win_primary.show();

    }

}


推荐答案

我喜欢这样做:

StackPane layout = new StackPane();
layout.getChildren().add(exit_pro);
layout.getChildren().add(back_to_start);
start.setOnAction(e -> {
 back_to_start.toFront();
 exit_pro.setVisible(false);
});
back_to_start.setOnAction(e -> {
 exit_pro.toFront();
 back_to_start.setVisible(false);
});

切换 StackPane 的孩子表现得像切换两个场景。

Switch StackPane's children to act like switch two scenes.

这篇关于在Java FX中切换场景时如何保持窗口大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆