简体   繁体   English

加载图像时BorderPane JavaFX会自动调整大小

[英]BorderPane JavaFX resize automatically when loading an image

This code: 这段代码:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));

        primaryStage.setTitle("Load an Image");
        primaryStage.setScene(new Scene(root));

        BorderPane mainBorderPane = (BorderPane) primaryStage.getScene().lookup("#mainBorderPane");
        mainBorderPane.setPadding(new Insets(8));

        HBox controls = new HBox();
        Button loadBtn = new Button("Load Image");
        Button sobelEdgeDetectionBtn  = new Button("Sobel Edge Detection");
        loadBtn.setMaxWidth(Double.MAX_VALUE);
        sobelEdgeDetectionBtn.setMaxWidth(Double.MAX_VALUE);

        setBrowseFileAction(loadBtn);

        controls.setSpacing(8);
        controls.getChildren().addAll(loadBtn,sobelEdgeDetectionBtn);
        mainBorderPane.setBottom(controls);

        primaryStage.sizeToScene();
        primaryStage.show();
    }

    private void setBrowseFileAction(Button loadBtn) {
        loadBtn.setOnAction((event) -> {
            Object source = event.getSource();
            Scene scene = ((Node) source).getScene();
            Stage stageOfEvent = (Stage) ((Node) source).getScene().getWindow();
            BorderPane mainBorderPane = (BorderPane) scene.lookup("#mainBorderPane");
            ImageView imageView = new ImageView();

            FileChooser fileChooser = new FileChooser();
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("JPG","*.jpg"),
                    new FileChooser.ExtensionFilter("JPEG","*.jpeg")
            );
            fileChooser.setTitle("Choose file...");
            fileChooser.setInitialDirectory(
                    new File(System.getProperty("user.home"))
            );
            File file = fileChooser.showOpenDialog(stageOfEvent);

            if(file != null) {
                Image image = new Image("file:"+file.getAbsolutePath());
                imageView.setImage(image);
                mainBorderPane.setCenter(imageView);
                stageOfEvent.sizeToScene();

            }
        });
    }

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

main.fxml file: main.fxml文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>

<BorderPane id="mainBorderPane" />

..not doing what I want. ..没有做我想做的。

I want to load an image and then resize the window and center it on the screen regardless of the image size. 我想加载图像,然后调整窗口大小并在屏幕上居中放置,而不管图像大小如何。

Spent too much time on it. 花了太多时间。 Any easy quick solutions ? 任何简单快速的解决方案? I could do it by adding loads of code which I am sure the poor documentation for JavaFX already has a method that does what I want. 我可以通过添加大量的代码来做到这一点,我确信JavaFX的较差文档已经有了满足我需要的方法。

At the end of the if-statement in setBrowseFileAction add: 在setBrowseFileAction的if语句末尾添加:

            stageOfEvent.setWidth(image.getWidth()+40);
            stageOfEvent.setHeight(image.getHeight()+40);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM