【问题标题】:Resizing window to accommodate an ImageView (JavaFX)调整窗口大小以适应 ImageView (JavaFX)
【发布时间】:2020-06-07 09:24:13
【问题描述】:

我希望调整窗口大小以适应按下按钮时改变大小的 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>

【问题讨论】:

  • 图片大于用户屏幕怎么办?
  • @Slaw 我想在这种情况下,预期的效果是窗口不能像现在那样容纳图像视图
  • 我看不到您为ImageView 设置图像的位置。可以给我看看吗?
  • @Abra ImageView 在我发布的 fxml 中有一个 fx:id,当按下按钮时,我在这个 imageView 上使用 setImage(someWritableImage)。
  • 您或许可以使用Window#sizeToScene()。可能需要额外的工作来确保窗口不会超出屏幕的大小。

标签: java user-interface javafx fxml


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

这是上面的一个非常小的实现。
注意:将 url 替换为以下代码中 Image 构造函数中图像的实际 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);
    }
}

【讨论】:

    猜你喜欢
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    相关资源
    最近更新 更多