我创建了自己的对象Stone来扩展Blocks类,从而扩展了Parent类。为了测试它是否有效,我制作了一个事件监听器方法来响应由我尚未使用的菜单项之一引起的事件。但是,当我单击该方法附带的菜单项时,没有显示任何图像。
我还尝试在url前面添加../../,以防它从较低级别搜索文件,在该文件中激活了运行代码并起作用,如果我只是将图像添加到我的fxml中文件中没有引用它起作用的对象。 但是,一旦我尝试将对象本身添加到锚定窗格中或将对象的图像(Stone.image)添加为Imageview的图像,它便不再起作用。
这是我的石头代码:
public class Stone extends Blocks{
private static final boolean VISIBLE = true;
private static final boolean TRANSPARENT = false; //A property of every block, that defines, if the character can walk through it.
private static Image image = new Image("file: ../../../../../.idea/assets/images/stone.png");
ImageView imageView = new ImageView(image);
}
这就是我尝试将其添加到我的An鱼鱼中的地方:
public void test(ActionEvent event) {
Stone stone = new Stone();
AnchorPane.getRightAnchor(stone);
Controller.drawplace.getChildren().add(stone);
}
也这样尝试过:
public class Stone extends Blocks{
private static final boolean VISIBLE = true;
private static final boolean TRANSPARENT = false; //A property of every block, that defines, if the character can walk through it.
private static Image image = new Image("file: ../../../../../.idea/assets/images/stone.png");
}
@FXML
protected ImageView stone;
@FXML
private void test(ActionEvent event) {
stone.setImage(Stone.image);
}
我的fxml文件:
<AnchorPane fx:id="drawplace" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="969.0" styleClass="bodymain" stylesheets="@gui.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<ImageView fx:id="stone" fitHeight="200.0" fitWidth="200.0">
</ImageView>
</children>