【发布时间】:2018-09-07 21:46:51
【问题描述】:
我正在尝试构建 MineSweeper 的副本,为此我正在尝试将一些预制图像(在 Photoshop 中制作的 100x100 像素)设置到图像视图中,然后单击隐藏它(以显示下面的数字)。没有太多复杂性——只是图像变得可见和不可见,我发现了很多问题和困难。
这可能是由于完全缺乏对 Javafx 的一般知识,但我什至按照教程进行操作,我仍然无法实现此功能。我将附上我的 Main.Java 代码、我的 sample.fxml 代码(虽然它不再被调用),然后是我试图在单击时隐藏的图像。
我(过去几天)对此进行了大量研究,但没有找到任何可以解决我问题的方法。
Main.java:
package sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
ImageView button;
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("MineSweeper XP");
button = new ImageView();
button.setOnMouseClicked(new EventHandler<MouseEvent>)(){
public void handle(MouseEvent event){
}
}
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout,1000, 1000);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="100.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fx:id="button" fitHeight="150.0" fitWidth="100.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../textures/Button_Custom.png" />
</image>
</ImageView>
</children>
</AnchorPane>
The "Button" that will be used for all the MineSweeper keys
目前我的唯一目标是创建一个任意大小的窗口,我可以在其中将按钮放在任何地方,然后当用户单击该按钮时它就会消失。
【问题讨论】:
-
您是否尝试将点击的
ImageView的visible属性设置为false?如果你有,为什么这对你不起作用? -
您发布的代码毫无意义。你有
FXML,没有Controller。带有Main的代码不会加载FXML或做任何事情。它只有一个空按钮处理程序。 -
我之前的代码称为 .fxml 函数,但我删除了该部分以尝试创建按钮处理程序
-
不要使用 ImageView 作为类似按钮的控件 - 而是使用带有 ImageView 作为图形的 Button 并在操作时更改它
-
@kleopatra 我个人认为使用 ImageView 作为按钮式控件没有任何问题。样式确实是唯一的区别,如果您想要的只是单击时消失的图像,则无需使用按钮...