在 imageview JavaFx 上显示图像
Display Image on imageview JavaFx
我有一个 .fxml 文件,里面有一些空的矩形
<AnchorPane id="AnchorPane" prefHeight="700.0" prefWidth="650.0" styleClass="fondo2" stylesheets="@tablerolote.css" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="juegoloto.CartonController">
<children>
<GridPane gridLinesVisible="true" layoutX="11.0" layoutY="44.0" prefHeight="614.0" prefWidth="474.0" styleClass="fondo">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView id="1" fitHeight="127.0" fitWidth="93.0" pickOnBounds="true" preserveRatio="true" />
<ImageView id="2" fitHeight="127.0" fitWidth="96.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" />
<ImageView id="3" fitHeight="126.0" fitWidth="96.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" />
<ImageView id="4" fitHeight="127.0" fitWidth="97.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" />
</children>
</GridPane>
</children>
</AnchorPane>
它是使用 netbeans(拖放界面)自动创建的,我如何从 .java 文件
发送图像以在 ImageView id="2" /ImageView id="3"
等中显示
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Carton.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
我也有一个 controller.java 文件,但我不知道如何处理它
public class CartonController implements Initializable {
@FXML
private Label label;
@FXML
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
您可以定义一个方法来检索 ImageView
:
public ImageView findById(AnchorPane root, String id) {
for (Node child : root.getChildren()) {
if (child instanceof ImageView && child.getId().equals(id)) {
return (ImageView)child;
}
}
return null;
}
此方法将遍历根 AnchorPane
的所有直接子节点并检查 ImageView
节点的 ID。如果没有找到匹配的节点,则返回 null。从那里您可以在返回的 ImageView
.
上使用 setImage
方法
您应该在控制器中执行此操作 class。 FXML reference 中完整描述了如何使用控制器,但简而言之:
在 ImageView
上放置一个 fx:id
(您可以在 SceneBuilder 中执行此操作):
<AnchorPane id="AnchorPane" prefHeight="700.0" prefWidth="650.0" styleClass="fondo2" stylesheets="@tablerolote.css" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="juegoloto.CartonController">
<children>
<GridPane gridLinesVisible="true" layoutX="11.0" layoutY="44.0" prefHeight="614.0" prefWidth="474.0" styleClass="fondo">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView id="1" fx:id="imageView1" fitHeight="127.0" fitWidth="93.0" pickOnBounds="true" preserveRatio="true" />
<ImageView id="2" fx:id="imageView2" fitHeight="127.0" fitWidth="96.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" />
<ImageView id="3" fx:id="imageView3" fitHeight="126.0" fitWidth="96.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" />
<ImageView id="4" fx:id="imageView4" fitHeight="127.0" fitWidth="97.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" />
</children>
</GridPane>
</children>
</AnchorPane>
然后将它们注入到您的控制器中,您可以根据需要访问它们:
public class CartonController implements Initializable {
@FXML
private ImageView imageView1 ;
@FXML
private ImageView imageView2 ;
@FXML
private ImageView imageView3 ;
@FXML
private ImageView imageView4 ;
@Override
public void initialize(URL url, ResourceBundle rb) {
imageView1.setImage(new Image(...));
// etc...
}
}
我有一个 .fxml 文件,里面有一些空的矩形
<AnchorPane id="AnchorPane" prefHeight="700.0" prefWidth="650.0" styleClass="fondo2" stylesheets="@tablerolote.css" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="juegoloto.CartonController">
<children>
<GridPane gridLinesVisible="true" layoutX="11.0" layoutY="44.0" prefHeight="614.0" prefWidth="474.0" styleClass="fondo">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView id="1" fitHeight="127.0" fitWidth="93.0" pickOnBounds="true" preserveRatio="true" />
<ImageView id="2" fitHeight="127.0" fitWidth="96.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" />
<ImageView id="3" fitHeight="126.0" fitWidth="96.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" />
<ImageView id="4" fitHeight="127.0" fitWidth="97.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" />
</children>
</GridPane>
</children>
</AnchorPane>
它是使用 netbeans(拖放界面)自动创建的,我如何从 .java 文件
发送图像以在ImageView id="2" /ImageView id="3"
等中显示
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Carton.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
我也有一个 controller.java 文件,但我不知道如何处理它
public class CartonController implements Initializable {
@FXML
private Label label;
@FXML
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
您可以定义一个方法来检索 ImageView
:
public ImageView findById(AnchorPane root, String id) {
for (Node child : root.getChildren()) {
if (child instanceof ImageView && child.getId().equals(id)) {
return (ImageView)child;
}
}
return null;
}
此方法将遍历根 AnchorPane
的所有直接子节点并检查 ImageView
节点的 ID。如果没有找到匹配的节点,则返回 null。从那里您可以在返回的 ImageView
.
setImage
方法
您应该在控制器中执行此操作 class。 FXML reference 中完整描述了如何使用控制器,但简而言之:
在 ImageView
上放置一个 fx:id
(您可以在 SceneBuilder 中执行此操作):
<AnchorPane id="AnchorPane" prefHeight="700.0" prefWidth="650.0" styleClass="fondo2" stylesheets="@tablerolote.css" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="juegoloto.CartonController">
<children>
<GridPane gridLinesVisible="true" layoutX="11.0" layoutY="44.0" prefHeight="614.0" prefWidth="474.0" styleClass="fondo">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView id="1" fx:id="imageView1" fitHeight="127.0" fitWidth="93.0" pickOnBounds="true" preserveRatio="true" />
<ImageView id="2" fx:id="imageView2" fitHeight="127.0" fitWidth="96.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" />
<ImageView id="3" fx:id="imageView3" fitHeight="126.0" fitWidth="96.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="2" />
<ImageView id="4" fx:id="imageView4" fitHeight="127.0" fitWidth="97.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" />
</children>
</GridPane>
</children>
</AnchorPane>
然后将它们注入到您的控制器中,您可以根据需要访问它们:
public class CartonController implements Initializable {
@FXML
private ImageView imageView1 ;
@FXML
private ImageView imageView2 ;
@FXML
private ImageView imageView3 ;
@FXML
private ImageView imageView4 ;
@Override
public void initialize(URL url, ResourceBundle rb) {
imageView1.setImage(new Image(...));
// etc...
}
}