JavaFX在mousepress上改变圆圈的颜色

时间:2015-10-26 07:48:45

标签: java javafx colors action shapes

我正在使用JavaFX为学校开发一个简单的应用程序。我们应该制作一个白色圆圈,当按下鼠标按钮时会变为黑色,然后在释放时变回白色。对于我的生活,我无法找到我在这里出错的地方。代码编译良好,Eclipse中没有警告/错误,并给我一个空白的白色窗口。我确信这是一件很简单的事情,我长时间盯着它看错了。任何帮助深表感谢。这是代码:

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.

1 个答案:

答案 0 :(得分:2)

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

而不是new Scene(pane,200,200);尝试new Scene(circlePane,200,200);