使javafx组件响应,并在调整大小时设置其最大大小

时间:2014-09-17 10:30:21

标签: java responsive-design javafx fxml

我成功地使组件响应,然后在拉伸窗口时变得越来越小。但问题是每当窗口调整大小时,组件也会调整大小并且看起来很糟糕。特此我发布了我的代码。这些行实际上将组件缩放到窗口大小:

contentRootRegion.scaleXProperty().bind(scene.widthProperty().divide(origW));
contentRootRegion.scaleYProperty().bind(scene.heightProperty().divide(origH));

有没有办法让javafx组件响应并保存其保存的口粮或最大尺寸?谢谢!

public class ResizingButtons extends Application {
    Controller controller;
    Scene scene;
    Stage stage;

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        controller = (Controller) replaceSceneContent(
                "Test.fxml");
    }

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

    public Initializable replaceSceneContent(String fxml) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = this.getClass().getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(this.getClass().getResource(fxml));

        Pane contentRootRegion;
        try {
            contentRootRegion = (Pane) loader.load();
        } finally {
            in.close();
        }
        double origW = 452;
        double origH = 526;
        if (contentRootRegion.getPrefWidth() == Pane.USE_COMPUTED_SIZE)
            contentRootRegion.setPrefWidth(origW);
        else
            origW = contentRootRegion.getPrefWidth();
        if (contentRootRegion.getPrefHeight() == Pane.USE_COMPUTED_SIZE)
            contentRootRegion.setPrefHeight(origH);
        else
            origH = contentRootRegion.getPrefHeight();
        // Wrap the resizable content in a non-resizable container (Group)
        Group group = new Group(contentRootRegion);
        // Place the Group in a StackPane, which will keep it centered
        StackPane rootPane = new StackPane();
        rootPane.getChildren().add(group);

        // Scene scene = new Scene(page);

        final Scene scene = new Scene(rootPane, 300, 700);

        // Bind the scene's width and height to the scaling parameters on the group
        //These two line of codes make the change its height and 
        // width when the frame is resized 
        contentRootRegion.scaleXProperty().bind(scene.widthProperty().divide(origW));
        contentRootRegion.scaleYProperty().bind(scene.heightProperty().divide(origH));
        stage.setScene(scene);
        stage.centerOnScreen();
        stage.show();
        scene.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable,
                                Number oldValue, Number newValue) {
                try {
                    System.out.println("Scene Width Property is " + scene.getWidth());
                    System.out.println(controller);
                    controller.resizeComponents();
                } catch (Exception e) {
                    System.out.println("Error in width listener of scene : " + e.getMessage());
                }
            }
        });
        return (Initializable) loader.getController();
    }
}

0 个答案:

没有答案