简体   繁体   English

创建一个 Mouselistner 到 Javafx 矩形

[英]Creating a Mouselistner to Javafx rectangle

I want to create a mouselistner on my javafx rectangle.我想在我的 javafx 矩形上创建一个 mouselistner。

the idea is that the rectangle has to change color when i press it?这个想法是当我按下矩形时,它必须改变颜色?

Does anyone know how to add a listner to shapes in Javafx?有谁知道如何在 Javafx 中向形状添加侦听器?

so far ive tried this:到目前为止我试过这个:

    final Rectangle rect = new Rectangle();

        rect.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                // TODO Auto-generated method stub

            }
        });

However i get an error saying that但是我收到一个错误说

the method setOnMouseClicked(new EventHandler(){}) is undefined for the type Rectangle方法 setOnMouseClicked(new EventHandler(){}) 未定义为 Rectangle 类型

Abit more information:升技更多信息:

The only options i have for rect are these:我对 rect 的唯一选择是:

rect.add()
rect.contains()
rect.grow();
rect.hashcode()
rect.intersection();

and a few others of no importance.和其他一些不重要的。

The import i am using are the following:我正在使用的导入如下:

import com.sun.glass.events.MouseEvent;
import com.sun.javafx.geom.Rectangle;
import com.sun.javafx.geom.Shape;

Your code looks correct and matches any examples I can find.您的代码看起来正确并与我能找到的任何示例相匹配。 To demonstrate this I have thrown together a quick example:为了证明这一点,我拼凑了一个简单的例子:

public class JavaFXApplication extends Application {

    Rectangle rect = new Rectangle(100,100);

    @Override
    public void start(Stage primaryStage) {
        rect.setFill(Color.BLUE);

        rect.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t) {
                rect.setFill(Color.RED);
            }
        });


        StackPane root = new StackPane();
        root.getChildren().add(rect);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

When the rectangle is clicked, the colour changes from blue to red.单击矩形时,颜色从蓝色变为红色。

This might be a long shot but make sure you are referencing the Rectangle type from the JavaFX library and not the AWT Rectangle ie make sure your import is:这可能是一个长期的尝试,但请确保您引用的是JavaFX库中的Rectangle类型而不是AWT Rectangle 即确保您的导入是:

import javafx.scene.shape.Rectangle;

and not并不是

import java.awt.Rectangle;

Update更新

As per my original comment it looks as though you are referencing the wrong import for the Rectangle type.根据我的原始评论,您似乎引用了错误的Rectangle类型导入。 I don't recognise the import com.sun.javafx.geom.Rectangle , is this from an older version of JavaFX?我不认识 import com.sun.javafx.geom.Rectangle ,这是来自旧版本的 JavaFX 吗?

You are also referencing the incorrect MouseEvent type.您还引用了错误的MouseEvent类型。

Change:改变:

import com.sun.glass.events.MouseEvent;

To:到:

import javafx.scene.input.MouseEvent;

I understand this answer is quite old -- but as an update (Java 13, JavaFX 13):我知道这个答案很旧 - 但作为更新(Java 13,JavaFX 13):

import javafx.scene.shape.Rectangle;
// ...
public void createRectangle() {

    Rectangle rect = new Rectanlge(200, 200);

    // On mouse click, code within here will be run.
    rect.setOnMouseClicked(mouseEvent -> {

        // Read from another property to toggle
        rect.setFill(Color.RED);

    });

    // Add to scene in start()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM