簡體   English   中英

JavaFX將事件偵聽器附加到黑色的圓圈

[英]JavaFX attach event listener to circles with color black

我是Java的新手,但是我正在嘗試構建一個跳棋游戲。 我已經構建了一塊板,但是我為在鼠標懸停和單擊時添加某種事件偵聽器的最佳方式和位置感到困惑。

我使用了Groovy和JavaFX的組合,因為我喜歡語法和JavaFX,因此我喜歡Groovy,因為它似乎是Swing的更好選擇。

class Window extends Application {

    private int boardSize = 8
    private int squareSize = 60

    void start(Stage primaryStage) {
        primaryStage.setTitle("Draughts")
        GridPane checkerBoard = new GridPane()
        checkerBoard.setPadding(new Insets(10,10,10,10))

        configureBoardSpecs(checkerBoard)

        layoutBoard(checkerBoard)

        BorderPane root = new BorderPane(checkerBoard);
        primaryStage.setScene(new Scene(root, 500, 500))


        primaryStage.show()
    }

    private void layoutBoard(def checkerBoard) {
        def fill = Color.WHITE

        for (row in 0..boardSize-1) {
            for (col in 0..boardSize-1) {
                if ((row+col)%2) {
                    fill = Color.SADDLEBROWN
                } else {
                    fill = Color.PERU
                }
                checkerBoard.add(new Rectangle(squareSize, squareSize, fill), col, row)

                if (row % 2 != col % 2) {
                    if (row < 3) {
                        checkerBoard.add(new Circle(squareSize/2-4, Color.WHITE), col, row)
                    } else if (row > 4) {
                        checkerBoard.add(new Circle(squareSize/2-4, Color.BLACK), col, row)
                    }
                }
            }
        }
    }

    private void configureBoardSpecs(def board) {
        for (i in 0..boardSize-1) {
            RowConstraints rowConstraints = new RowConstraints()
            rowConstraints.setMinHeight(squareSize)
            rowConstraints.setPrefHeight(squareSize)
            rowConstraints.setMaxHeight(squareSize)
            rowConstraints.setValignment(VPos.CENTER)
            board.getRowConstraints().add(rowConstraints)

            ColumnConstraints colConstraints = new ColumnConstraints()
            colConstraints.setMinWidth(squareSize)
            colConstraints.setMaxWidth(squareSize)
            colConstraints.setPrefWidth(squareSize)
            colConstraints.setHalignment(HPos.CENTER)
            board.getColumnConstraints().add(colConstraints)
        }
    }

我在jQuery上做過很多這類的事情,我會使用選擇器來抓取任何黑色的圓圈,並使其具有色標,因此當鼠標指針懸停在圓圈上時,會在圓圈上添加邊框。 然后單擊,整個圓形或包圍的矩形的顏色將改變顏色。

有關解決此問題的最佳方法的任何有用建議。

非常感謝,

創建圓時,需要創建對圓的引用,以便可以添加偵聽器。 例如(注意:我使用的是Java,而不是Groovy,因此語法可能不匹配,但這將為您提供想法):

Circle circle = new Circle(squareSize/2-4, Color.WHITE) ;
circle.setOnMouseClicked(e -> {
    // handler code...
});
checkerBoard.add(circle, col, row) ;

我設法按照我的建議去做:

    children = checkerBoard.getChildren()
        for (child in children) {
            if (child instanceof Circle) {
                child.setOnMouseEntered(new EventHandler<MouseEvent>() {
                    @Override
                    void handle(MouseEvent event) {
                        child.setStroke(Color.ORANGE);
                    }
                })
            }
        }

但是@James_D的答案似乎更好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM