JavaFX 中鼠标颜色更改效果

发布于 2024-10-10 11:37:11 字数 217 浏览 0 评论 0原文

我想更改此代码,以便当我单击球时,它会变成红色。有人可以帮助我吗?我是java新手: http://download.oracle.com/javafx/1.3/tutorials /ui/syntax/index.html

I want to change this code so when I click on the ball, it turns red. Can anyone help me? I'm new to java:
http://download.oracle.com/javafx/1.3/tutorials/ui/syntax/index.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2024-10-17 11:37:11

试试这个:

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)将Circle提取到变量(圆)中,以便以后使用。
2)向矩形添加鼠标单击事件,以翻转颜色。

Try this:

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

Two changes here:
1) Extract Circle in a variable (circle), so that it can be used later.
2) Add a mouse clicked event to the rectangle, to flip colors.

我的痛♀有谁懂 2024-10-17 11:37:11

你只需要把这里的“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矩形:矩形;就像var Circle: Circle;

you just have to change the word circle into rectangle right here

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
                }

and put in this sentence var rectangle : Rectangle; just like var circle: Circle;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文