JavaFX改变鼠标按下圆圈的颜色

JavaFX change color of a circle on mousepress

我正在使用 JavaFX 为学校开发一个简单的应用程序。我们应该制作一个白色圆圈,当按下鼠标按钮时该圆圈变为黑色,然后在松开鼠标按钮时变回白色。对于我的一生,我找不到我哪里出错了。该代码在 Eclipse 中没有 warnings/errors 时编译得很好,并给了我一个空白 window。我敢肯定这是很简单的事情,我盯着它看了这么久而想念它。任何帮助深表感谢。这是代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.layout.StackPane;


public class CircleColor extends Application {
    private CirclePane circlePane = new CirclePane();

    @Override // Override the start method in the Application class.
    public void start(Stage primaryStage) {     
        Pane pane = new Pane();

        // Handle mouse click actions.
        circlePane.setOnMousePressed(e -> { 
            circlePane.paintBlack();
        });

        // Handle mouse release actions.
        circlePane.setOnMouseReleased(e -> {
            circlePane.paintWhite();
        });

        // Create a scene & place it on the stage.
        Scene scene = new Scene(pane, 200, 200);
        primaryStage.setTitle("CircleColor"); // Set the stage title.
        primaryStage.setScene(scene); // Place the scene on the stage.
        primaryStage.show(); // Display the stage.

        circlePane.requestFocus();


    } // Close the start method.


    // Main method only needed for IDEs with limited JavaFX support
    public static void main(String[] args) {
        launch(args);

    } // Close the main method.

} // Close CircleColor class


class CirclePane extends StackPane {
    private Circle circle = new Circle(50);

    public CirclePane() {
        getChildren().add(circle);
        circle.setStroke(Color.BLACK);
        circle.setFill(Color.WHITE);
    } // Close CirclePane constructor.

    public void paintBlack() {
        circle.setFill(Color.BLACK);
    } // Close the paintBlack method.

    public void paintWhite() {
        circle.setFill(Color.WHITE);
    } // Close the paintWhite method.

} // Close the CirclePane class.

您永远不会将 CirclePane 添加到场景中。

所以不要 new Scene(pane,200,200); 试试 new Scene(circlePane,200,200);