我试图将 ImageView 放入 AnchorPane 中,ImageView 是从数据库获取的,所以没有 CSS,我尝试将 ImageView 的宽度和高度与 AnchorPane 的宽度和高度绑定,但我得到以下结果:
这是我的 fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane style="-fx-background-color: grey;" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label fx:id="nombrePlato" alignment="CENTER" contentDisplay="CENTER" maxHeight="50.0" prefHeight="50.0" text="Nombre del Plato" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font size="30.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane fx:id="anchorPane" layoutY="300.0" styleClass="anchorPane" stylesheets="@../../../../core/resources/css/MainCSS.css" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="50.0">
<children>
<ImageView fx:id="imagenPlato" fitHeight="100.0" fitWidth="100.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
</children>
</AnchorPane>
Run Code Online (Sandbox Code Playgroud)
这是我的方法:
public void rellenar(Label nombrePlato, ImageView imagenPlato, Plato plato, AnchorPane anchorPane) throws SQLException {
nombrePlato.setText(plato.getNombre());
Blob blob = plato.getRutaImagen();
int blobLength = (int) blob.length();
byte[] blobAsBytes = blob.getBytes(1, blobLength);
Image convertToJavaFXImage = convertToJavaFXImage(blobAsBytes, 1024, 768);
imagenPlato.setImage(convertToJavaFXImage);
imagenPlato.fitWidthProperty().bind(anchorPane.widthProperty());
imagenPlato.fitHeightProperty().bind(anchorPane.heightProperty());
}
Run Code Online (Sandbox Code Playgroud)
知道如何使其适合锚定窗格并使其具有响应能力吗?
作为 ImageView 的替代方案,您可以在区域(例如 AnchorPane)上使用CSS-fx-background-image属性。CSS-fx-background-size选项将为您提供各种尺寸选择,例如 cover 或 contains:
包含
将图像缩放到最大尺寸,同时保留其固有的纵横比(如果有),使其宽度和高度都能适合背景定位区域。
覆盖
将图像缩放到最小尺寸,同时保留其固有的纵横比(如果有),使其宽度和高度都可以完全覆盖背景定位区域。