【问题标题】:JavaFX change color of a circle on mousepressJavaFX在鼠标按下时改变圆圈的颜色
【发布时间】:2015-10-26 07:48:44
【问题描述】:

我正在使用 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.

【问题讨论】:

    标签: java javafx colors action shapes


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,就这样。我知道这一定是我忽略的一些简单的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 2014-09-27
    相关资源
    最近更新 更多