调整大小以容纳ImageView(JavaFX)的窗口

问题描述 投票:0回答:1

我正在尝试调整窗口大小以适合当按下按钮时会更改大小的ImageView。 ImageView包含在AnchorPane中。我似乎找不到任何资源来解释如何执行此操作,也无法在SceneBuilder中找到任何内容。谢谢!

<AnchorPane prefHeight="400" prefWidth="600" style="-fx-background-color: #282828;" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
    <HBox maxHeight="-Infinity" maxWidth="-Infinity" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
        <children>
            <VBox prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: #535353;">
                <children>
                    <Pane prefHeight="50.0" prefWidth="100.0" />
                    <Button fx:id="topButton" mnemonicParsing="false" onAction="#handleTopButton" prefHeight="25.0" prefWidth="100.0" text="Top" />
                    <Button fx:id="sideButton" mnemonicParsing="false" onAction="#handleSideButton" prefHeight="25.0" prefWidth="100.0" text="Side" />
                    <Button fx:id="frontButton" mnemonicParsing="false" onAction="#handleFrontButton" prefHeight="25.0" prefWidth="100.0" text="Front" />
                    <Pane prefHeight="50.0" prefWidth="100.0" />
                    <Label fx:id="scaleLabel" prefHeight="17.0" prefWidth="103.0" text="Scale" textAlignment="CENTER" textFill="WHITE" />
                    <Slider fx:id="scale" max="3.0" min="1.0" />
                    <Label fx:id="sliceLabel" text="Slice" textAlignment="CENTER" textFill="WHITE" />
                    <Slider fx:id="slice" disable="true" />
                    <Pane prefHeight="50.0" prefWidth="100.0" />
                    <Button fx:id="mipButton" disable="true" mnemonicParsing="false" onAction="#handleMipButton" prefHeight="25.0" prefWidth="100.0" text="MIP" />
                    <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Histogram" />
                    <Button fx:id="exitButton" mnemonicParsing="false" onAction="#handleExitButton" prefHeight="25.0" prefWidth="100.0" text="Exit" />
                </children>
            </VBox>
            <ImageView fx:id="imageView"/>
        </children>
    </HBox>
</children>

java user-interface javafx fxml
1个回答
0
投票
  1. [使用符合其所含Node首选大小的容器,例如BorderPane
  2. 更改图像时(在按您所描述的问题单击按钮之后),调用方法sizeToScene来调整应用程序窗口的大小。

这里是上述内容的最小实现。注意:在以下代码的Image构造函数中,将url替换为图像的实际URL。

import javafx.application.Application;
import javafx.geometry.Pos;
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.stage.Stage;

public class ImgVwTst extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane root = new BorderPane();
        Image img = new Image("url");
        ImageView imgVw = new ImageView(img);
        root.setCenter(imgVw);
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        Button nextImageButton = new Button("next");
        nextImageButton.setOnAction(e -> {Image img2 = new Image("url");
                                          imgVw.setImage(img2);
                                          primaryStage.sizeToScene();});
        hBox.getChildren().add(nextImageButton);
        root.setBottom(hBox);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.