setOnMouseExited 不适用于笔划?

setOnMouseExited doesn't work with strokes?

public void frame(Rectangle rechteck) {
        rechteck.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent e) {
                rechteck.setStroke(Color.DARKGREY);
            }
        });
        rechteck.setOnMouseExited((MouseEvent e) -> {
            rechteck.setStroke(Color.BLUE);
        });
    }

我试着有一个'hover'的效果。我有不同的矩形,我希望当我进入矩形时它会出现灰色笔划。当光标离开矩形时,笔画应该会再次消失。

  1. 为什么我的笔划总是灰色的,离开矩形后没有变化。

  2. 如何禁用笔画?如果我退出矩形,我实际上并不想将颜色更改为蓝色,但我希望它不再存在。

编辑:其余

@Override
    public void start(Stage primaryStage) {

        StackPane root = new StackPane();

        // Die Linie in der Mitte.
        Rectangle line = new Rectangle(0, 0, 450, 1);
        root.getChildren().add(line);

        // Das abgerundete Rechteck.
        Rectangle roundedRect = new Rectangle(0, 0, 300, 80);
        roundedRect.setStroke(Color.BLACK);
        roundedRect.setArcWidth(40);
        roundedRect.setArcHeight(40);
        roundedRect.setFill(Color.BISQUE);
        root.getChildren().add(roundedRect);

        // Die Farblinien.
        Rectangle firstLine = new Rectangle(100, 0, 25, 80);
        firstLine.setFill(Color.BROWN);
        frame(firstLine);
        Rectangle secondLine = new Rectangle(0, 0, 25, 80);
        secondLine.setFill(Color.BLACK);
        frame(secondLine);
        Rectangle thirdLine = new Rectangle(0, 0, 25, 80);
        thirdLine.setFill(Color.RED);
        frame(thirdLine);
        Rectangle fourthLine = new Rectangle(0, 0, 25, 80);
        fourthLine.setFill(Color.GOLD);
        frame(fourthLine);
        HBox lines = new HBox();
        lines.setAlignment(Pos.CENTER);
        lines.setPadding(new Insets(30));
        HBox.setMargin(firstLine, new Insets(0, 30, 0, 0));
        HBox.setMargin(secondLine, new Insets(0, 30, 0, 0));
        HBox.setMargin(thirdLine, new Insets(0, 30, 0, 0));
        HBox.setMargin(fourthLine, new Insets(0, 0, 0, 50));
        lines.getChildren().addAll(firstLine, secondLine, thirdLine, fourthLine);

        double widerstand = (ERSTERRING + ZWEITERRING) * DRITTERRING;

        char groesse = ' ';
        if (widerstand >= 1000 && widerstand < 10000) {
            widerstand = widerstand / 1000;
            groesse = 'k';
        } else if (widerstand >= 1000000) {
            widerstand = widerstand / 1000000;
            groesse = 'M';
        }

        VBox vbox = new VBox();
        Text werte = new Text(90, 210, "R = " + widerstand + groesse + "Ω mit " + VIERTERRING + " % Toleranz");
        VBox.setMargin(werte, new Insets(200, 0, 0, 70));
        vbox.getChildren().add(werte);
        root.getChildren().add(vbox);
        root.getChildren().add(lines);

        Scene scene = new Scene(root, 450, 300);
        primaryStage.setTitle("Widerstandsrechner");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

Why is my stroke always grey and doesnt change when I leave the rectangle.

如果没有您的其余代码,很难说。

How can I disable the stroke?

设置笔画为nullColor.TRANSPARENT


这是一个示例,它似乎可以完成您想要的。第一张图片没有鼠标悬停在(最初不可见的)矩形上。第二张图片是鼠标悬停在矩形上。

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class RectHover extends Application {
    @Override public void start(Stage stage) {
        Rectangle rectangle = new Rectangle(
                100, 100, Color.TRANSPARENT
        );

        rectangle.strokeProperty().bind(
                Bindings.when(rectangle.hoverProperty())
                        .then(Color.DARKGREY)
                        .otherwise((Color) null)
        );

        StackPane layout = new StackPane(rectangle);
        layout.setPadding(new Insets(10));
        stage.setScene(new Scene(layout));
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}