我有问题。如果我将效果设置为弹出矩形,效果将反复应用。所以它会很厚...... 我该怎么办? 谢谢你的帮助!
@Override
public void start(final Stage stage) {
stage.setTitle("PopupXmpl");
BorderPane root = new BorderPane();
final Popup pop = new Popup();
Circle circle = new Circle(400, 300, 200, Color.WHITESMOKE);
circle.setStroke(Color.BLACK);
circle.setOnMouseEntered(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent t) {
Rectangle rectangle = new Rectangle(40, 15, Color.WHITE);
rectangle.setStroke(Color.DARKGREY);
rectangle.setArcHeight(4);
rectangle.setArcWidth(6);
pop.setHeight(100);
pop.setWidth(100);
pop.setX(t.getScreenX());
pop.setY(t.getScreenY()-50);
rectangle.setEffect(new DropShadow());
pop.getContent().add(rectangle);
pop.show(stage);
}
});
circle.setOnMouseExited(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent t) {
pop.hide();
}
});
root.getChildren().add(circle);
stage.setScene(new Scene(root, 800, 600));
stage.show();
}
答案 0 :(得分:1)
您在每个MouseEntered
事件中反复将矩形添加到弹出窗口的内容中。只添加一次,并在此MouseEntered
事件中仅更改弹出窗口的事件相关属性:
@Override
public void start(final Stage stage) {
stage.setTitle("PopupXmpl");
BorderPane root = new BorderPane();
Rectangle rectangle = new Rectangle(40, 15, Color.WHITE);
rectangle.setStroke(Color.DARKGREY);
rectangle.setArcHeight(4);
rectangle.setArcWidth(6);
rectangle.setEffect(new DropShadow());
final Popup pop = new Popup();
pop.getContent().add(rectangle);
pop.setHeight(100);
pop.setWidth(100);
Circle circle = new Circle(400, 300, 200, Color.WHITESMOKE);
circle.setStroke(Color.BLACK);
circle.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
pop.setX(t.getScreenX());
pop.setY(t.getScreenY() - 50);
pop.show(stage);
}
});
circle.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
pop.hide();
}
});
root.getChildren().add(circle);
stage.setScene(new Scene(root, 800, 600));
stage.show();
}