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

时间:2011-01-04 19:08:52

标签: events colors mouse javafx

我想更改此代码,所以当我点击球时,它会变为红色。谁能帮我?我是java的新手: http://download.oracle.com/javafx/1.3/tutorials/ui/syntax/index.html

2 个答案:

答案 0 :(得分:0)

试试这个:

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)将鼠标单击事件添加到矩形,以翻转颜色。

答案 1 :(得分:0)

你只需要在这里将圆圈改为矩形

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 rectangle:Rectangle;就像var circle: Circle;

一样