2016-07-22 97 views
0

我想用鼠标左键点击并用鼠标右键点击它们。我阅读并尝试了很多,但不知何故无法得到这个工作。右键点击JavaFX for Minesweeper

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

可以anyonw请帮忙吗?

+0

定义“不工作”。你有什么看起来像一个语法错误。它当然不是Java。 – markspace

+0

对不起,是的,有几个语法错误。我以此为例:http://stackoverflow.com/questions/1515547/right-click-in-javafx –

+0

@KendelVentonda:这是JavaFX **脚本**,不再支持。 – fabian

回答

1

您的onMouseClicked处理程序出错了。

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

正确的方式做这将是

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

此外,还有一些声明在你的代码片段丢失:

  • open方法
  • TILE_SIZE
+0

两者都是后来宣布的,没有放在这里。但你的解决方案很简单,非常合乎逻辑!万分感谢! –