提问人:Marek Dvorský 提问时间:11/15/2023 更新时间:11/15/2023 访问量:53
Javafx 在我的 BorderPane 中设置 ImageView 时出现问题
Javafx problem setting ImageView inside my BorderPane
问:
我有什么:
我有一个 BorderPane,我的 BorderPane 由 2 个部分组成,顶部和中心。在顶部,我有要设置的 ImageView,在中心部分,我有一个窗格,其中有一个按钮,此按钮应更改 ImageView 中的图像。整个 BorderPane 是一个使用 BorderPaneController 控制的 FXML 文件,但中心部分(窗格)是另一个使用 CenterPaneController 控制的 FXML 文件。
我想做什么: 图像视图在我单击按钮后显示我的新图像
我的问题是什么:
当我单击该按钮时,图像路径已成功从 CenterPaneController 传输到 BorderPaneController,并且 ImageView 也已成功设置,因为它包含图像路径,但图像视图仍为空
这是 BorderPane 中心部分(窗格)的 FXML 文件,我在其中有按钮:
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ProfilPouzivatelaController">
<children>
<Button layoutX="434.0" layoutY="227.0" mnemonicParsing="false" onAction="#insertImagePath" onMouseClicked="#vlozFotku" text="Zmeniť profilovú fotku" />
</children>
</Pane>
这是 BorderPane 的 FXML 文件,其中我的 ImageView 位于顶部:
<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="700.0" prefWidth="1400.0" stylesheets="hlavnePouzivatelskeRozhranie.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="PouzivatelskeRozhranieController">
<top>
<HBox fx:id="topHBox" prefHeight="57.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<HBox fx:id="innerHbox" alignment="CENTER_LEFT" nodeOrientation="RIGHT_TO_LEFT" prefHeight="54.0" prefWidth="148.0">
<children>
<ImageView fx:id="image" fitHeight="50.0" fitWidth="60.0" pickOnBounds="true" preserveRatio="true" />
</children>
</children>
</HBox>
</top>
我的BorderPaneController的代码:
public class BorderPaneController {
@FXML
private ImageView image;
public void setImage(String imagePath) {
this.image.setImage(new Image(imagePath));
}
}
这是我的CenterPaneController的代码:
public class CenterPaneController {
private BorderPaneController borderPane;
@FXML
public void insertImagePath(ActionEvent e) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("borderPane.fxml"));
Parent root = loader.load();
borderPane = loader.getController();
borderPane.setImage("Obrazky//default.png");
}
答: 暂无答案
评论
"Obrazky//default.png"
insertImagePath()
Property<Image>