簡體   English   中英

JavaFX更改鼠標按下時的圓圈顏色

[英]JavaFX change color of a circle on mousepress

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

您永遠不會將CirclePane添加到場景中。

因此,而不是new Scene(pane,200,200); 嘗試new Scene(circlePane,200,200);

暫無
暫無

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

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