Events JavaFX中鼠标效果的颜色更改

Events JavaFX中鼠标效果的颜色更改,events,colors,mouse,javafx,Events,Colors,Mouse,Javafx,我想更改此代码,以便当我单击球时,它变为红色。有人能帮我吗?我是java新手: 试试这个: import javafx.scene.Scene; import javafx.scene.shape.Rectangle; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.scene.input.MouseEvent; var ci

我想更改此代码,以便当我单击球时,它变为红色。有人能帮我吗?我是java新手: 试试这个:

import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.input.MouseEvent;


var circle: Circle;
Stage {
    title: "Declaring Is Easy!"
    scene: Scene {
        width: 300
        height: 250
        content: [
            circle = Circle {
                centerX: 150  centerY: 120 radius: 80
                fill: Color.MAROON
                stroke: Color.INDIANRED
                strokeWidth: 10.0

            }, //Circle
            Rectangle {
                x: 25, y: 80 width: 250, height: 80
                arcWidth: 20 arcHeight: 20
                fill: Color.web("#6699ff")
                stroke: Color.web("#003399")
            strokeWidth: 5.0
                onMouseClicked:function(e: MouseEvent)
                {
                 if(circle.fill == Color.MAROON)
                    circle.fill = Color.GREEN
                 else
                    circle.fill = Color.MAROON
                }

            } //Rectangle
        ] //Content
    } //Scene
} //Stage
这里有两个变化:
1) 提取变量(圆)中的圆,以便以后使用。

2) 将鼠标单击事件添加到矩形,以翻转颜色

您只需在此处将单词circle更改为矩形即可

if(**circle**.fill == Color.MAROON)
                    **circle**.fill = Color.GREEN
                 else
                    **circle**.fill = Color.MAROON
                }

if(**rectangle**.fill == Color.MAROON)
                    **rectangle**.fill = Color.GREEN
                 else
                    **rectangle**.fill = Color.MAROON
                }
把这句话放在矩形里:矩形;就像
var循环:循环