0

我有个问题。如果我将效果设置为弹出矩形,该效果将一次又一次地应用。所以它会很厚......我该怎么办?感谢帮助!

@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();        
}
4

1 回答 1

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();
}
于 2013-06-20T10:05:56.490 回答