调整窗口大小以适应ImageView(JavaFX)

时间:2020-02-23 16:13:23

标签: java user-interface javafx fxml

我想调整窗口大小以适合当按下按钮时更改大小的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>

1 个答案:

答案 0 :(得分:1)

  1. 使用尊重其包含的Image的首选大小的容器,例如BorderPane
  2. 在更改图像时(按问题中的描述单击按钮后),调用方法sizeToScene来调整应用程序窗口的大小。

这是上述内容的极小的实现。
注意:在以下代码的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); } } 构造函数中,将 url 替换为图像的实际URL。

class ParentClass
{
    public function getNewFoo()
    {
        return new Foo();
    }
}
相关问题