在JavaFX中设置舞台和场景的高度和宽度

19
  • 我开发了一个JavaFX应用程序。
  • 我的应用程序有两个场景和一个舞台。
  • 在应用程序中,这两个场景的高度和宽度都是相同的或者是常数。
  • 根据我的研究,场景的高度和宽度保持不变,并在构造函数中指定,但场景会自动调整到舞台的高度和宽度。
  • 如果我使用与场景常量高度和宽度不同的舞台高度和宽度来启动应用程序,则场景会自动调整到舞台的大小。
  • 但是,当我在运行时应用第二个场景时,场景不会根据舞台的高度和宽度进行调整。场景的高度和宽度保持不变。

  • 是否有任何解决方案?


舞台需要刷新,但怎么刷新? - Anvay
3个回答

12
根据我理解上述问题,我认为该阶段已足够好,可以根据侦听器获取的新请求设置首选高度和宽度,并应用于窗口大小。但是它有一些限制,如果您最大化或最小化javaFX屏幕并尝试导航到其他屏幕,则其他屏幕将具有相同的窗口大小,但场景内容将扭曲为其默认高度和宽度,例如在javafx中登录和主页场景(所有场景均使用fxml创建)。 Login.fxml由其控制器初始化。正如您所提到的场景在构造函数中初始化,因此它必须是相关fxml的控制器(目前FXML与控制器紧密耦合)。您将在构造函数本身中设置场景大小(高度和宽度)。
1.) login.fxml的LoginController
 import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;

    import java.io.IOException;

    class LoginController  {

        private Stage stage;
        private Scene scene;
        private Parent parent;
        @FXML  
        private Button gotoHomeButton;        

        public LoginController()  throws Exception {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/login.fxml"));
            fxmlLoader.setController(this);
            try {
                parent = (Parent) fxmlLoader.load();
                // set height and width here for this login scene
                scene = new Scene(parent, 1000, 800);
            } catch (IOException ex) {
                System.out.println("Error displaying login window");
                throw new RuntimeException(ex);
            }
        }

        // create a launcher method for this. Here I am going to take like below--
        public void launchLoginScene(Stage stage) {
           this.stage = stage;
            stage.setScene(scene);
            stage.setResizable(true);

            stage.widthProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
                    setCurrentWidthToStage(number2); 
                }
            });

            stage.heightProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
                    setCurrentHeightToStage(number2);
                }
            });

            //Don't forget to add below code in every controller
            stage.hide();
            stage.show();

        }

         @FXML
        public void authenticateUser(ActionEvent actionEvent) { 

        // write your logic to authenticate user


         // 
         new HomeController().displayHomeScreen(stage);

        } 

        private void setCurrentWidthToStage(Number number2) {
            stage.setWidth((double) number2);
        }

        private void setCurrentHeightToStage(Number number2) {
            stage.setHeight((double) number2);
        }
    }

2.) HomeController同理 --

public class HomeController {

    private Parent parent;
    private Stage stage;
    private Scene scene;


    public HomeController (){
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/home.fxml"));
        fxmlLoader.setController(this);
        try {
             parent = (Parent) fxmlLoader.load();
                // set height and width here for this home scene
                scene = new Scene(parent, 1000, 800);
        } catch (IOException e) {
         // manage the exception
        }
    }

    public void displayHomeScreen(Stage stage){
        this.stage = stage;
        stage.setScene(scene); 

        // Must write
        stage.hide()
        stage.show();
    }
}

3.) 主类

import javafx.application.Application;

import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        new LoginController().launchLoginScene(primaryStage);
    }


    public static void main(String[] args) {
        launch(args);
    }
}

尝试在每个控制器中将 Stage.hide() 放在 Stage.show() 之前。我希望这能对您有所帮助。


很棒的回答!在JavaFX中,通过监听来更新大小是一件奇怪的事情...大小(场景具有高优先级)imho。似乎必须呈现自动模式。但代码运行良好。谢谢。 - Sergey Orlov

9
由于舞台会根据场景自适应大小,除非有明确的不同指示......这是因为。
以下是一个解决方案:
stage.setScene(scene2);
stage.setHeight(1000);
stage.setWidth(1000);

以下是一个示例应用程序:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Test extends Application {

    @Override
    public void start(final Stage stage) throws Exception {

        AnchorPane anchor1 = new AnchorPane();
        final Scene scene1 = new Scene(anchor1, 250, 250);
        Button boton1 = new Button();
        anchor1.getChildren().add(boton1);

        AnchorPane anchor2 = new AnchorPane();
        final Scene scene2 = new Scene(anchor2, 500, 500);
        Button boton2 = new Button();
        anchor2.getChildren().add(boton2);

        boton2.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                // TODO Auto-generated method stub
                stage.setScene(scene1);
                stage.setHeight(1000);
                stage.setWidth(1000);
            }
        });

        boton1.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                // TODO Auto-generated method stub
                stage.setScene(scene2);
                stage.setHeight(1000);
                stage.setWidth(1000);
            }
        });
        stage.setScene(scene1);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

你发布的样例是正确的,对于将高度和宽度应用到舞台的概念也是正确的。但是,如果你将背景图像应用到场景,并在两个按钮的点击事件中更改舞台的大小(尝试不同的大小),那么你会发现实际问题。第二次时,场景将无法与舞台相匹配......所以我需要一种刷新舞台的方法。 - Anvay
stage.sizeToScene() 是正确的概念,但我希望场景能够与舞台相适应。 - Anvay

2

我知道这是一个老话题,但是在Java7u75中仍然存在这个(bug?)。

我遇到了同样的问题...上面shambhu提供的解决方案确实可行,但会导致一个明显的“窗口切换”效果:

stage.hide();
stage.show();

我发现以下代码可以解决问题(使舞台“刷新”),但没有任何可见效果:
final boolean resizable = stage.isResizable();
stage.setResizable(!resizable);
stage.setResizable(resizable);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接