【问题标题】:Right click in JavaFX for Minesweeper右键单击 JavaFX for Minesweeper
【发布时间】:2016-07-22 18:04:50
【问题描述】:

我想用鼠标左键打开图块并用鼠标右键标记它们。我阅读并尝试了很多,但不知何故无法使其正常工作。

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());
            }
        }
    }

有人可以帮忙吗?

【问题讨论】:

标签: java javafx


【解决方案1】:

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

有关 lambda 表达式的正确语法,请参阅the Syntax section of the oracle tutorial

正确的做法是

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

此外,您的代码 sn-p 中缺少一些声明:

  • open 方法
  • TILE_SIZE 字段

【讨论】:

  • 两者都是后面声明的,这里就不放了。但是您的解决方案很简单而且非常合乎逻辑!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 2021-11-16
  • 2014-07-31
  • 2012-11-06
  • 1970-01-01
相关资源
最近更新 更多