右键单击JavaFX for Minesweeper

时间:2016-07-22 18:04:51

标签: java javafx

我想用左键单击打开图块,然后用鼠标右键标记它们。我阅读并尝试了很多,但不知何故不能使这个工作。

private class Tile extends StackPane {
    private int x, y;
    private boolean hasBomb;
    private boolean isOpen = false;

    private Rectangle border = new Rectangle(TILE_SIZE - 2, TILE_SIZE - 2);
    private Text text = new Text();

    public Tile(int x, int y, boolean hasBomb) {
        this.x = x;
        this.y = y;
        this.hasBomb = hasBomb;

        border.setStroke(Color.BLACK);
        border.setFill(Color.GREY);
        text.setFont(Font.font(18));
        text.setText(hasBomb ? "X" : "");
        text.setVisible(false);

        getChildren().addAll(border, text);

        setTranslateX(x * TILE_SIZE);
        setTranslateY(y * TILE_SIZE);

        onMouseClicked: function(e:MouseEvent):Void {
            if (e.button == MouseButton.SECONDARY) {
                setOnMouseClicked(e -> open());
            }
        }
    }

请帮忙吗?

1 个答案:

答案 0 :(得分:1)

您的onMouseClicked处理程序出了问题。

有关lambda表达式的正确语法,请参阅per RFC 2616

正确的方法是

this.setOnMouseClicked(e -> {
    if (e.getButton() == MouseButton.SECONDARY) {
        open();
    }
});

此外,您的代码段中缺少一些声明:

  • open方法
  • TILE_SIZE字段