gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:58:12 49 4
gpt4 key购买 nike

我希望调整窗口大小以适应 ImageView,该 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. 使用尊重其所包含的节点首选大小的容器,例如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);
}
}

关于java - 调整窗口大小以适应 ImageView (JavaFX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60364205/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com