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);
});
}
我尝试着一个&#39; hover&#39;影响。我有不同的矩形,我希望当我进入矩形时,它会变成灰色的笔触。当光标离开矩形时,笔划应该再次消失。
当我离开矩形时,为什么我的笔划始终是灰色的并且没有变化。
如何禁用中风?如果我退出矩形,我实际上并不想将颜色更改为蓝色,但我希望它不再存在。
编辑:其余的
@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();
}
答案 0 :(得分:1)
当我离开矩形时,为什么我的笔划始终是灰色的并且没有变化。
没有剩下的代码就很难说。
如何禁用中风?
将笔画设置为null
或Color.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); }
}