2016-12-03 3 views
1

У меня есть панель прокрутки внутри нее анкерной панели. и внутри этой анкерной панели у меня есть ярлык. Мне нужно, чтобы этот ярлык всегда был видимым, когда я прокручиваю. Мне нужно двигаться так, чтобы пользователь всегда видел это так, как будто он не менялся. Может ли кто-нибудь помочь?Как сделать узел всегда видимым при прокрутке?

@FXML 
private ScrollPane s; 


public void initialize(URL location, ResourceBundle resources) {       
       AnchorPane p = new AnchorPane(); 
       VBox v = new VBox(); 
       p.getChildren().add(v); 
       s.setContent(p);} 
+0

Необходимо показать код, это поможет получить swers – Yanga

+0

Для этого должно быть динамическим и положение узла (Label)! –

+0

@BoHalim Как я могу исправить это – user6601127

ответ

2

Самый простой способ сделать это было бы не поместить Label внутри содержимого, но в StackPane, который содержит как ScrollPane и Label:

@Override 
public void start(Stage primaryStage) { 
    Region content = new Region(); 
    content.setPrefSize(2000, 2000); 
    content.setBackground(new Background(new BackgroundFill(
      new LinearGradient(0, 0, 1, 1, true, CycleMethod.NO_CYCLE, new Stop(0, Color.RED), new Stop(1, Color.BLUE)), 
      CornerRadii.EMPTY, 
      Insets.EMPTY))); 

    Label label = new Label("Hello World!"); 
    label.setTextFill(Color.WHITE); 
    StackPane.setAlignment(label, Pos.TOP_LEFT); 
    StackPane.setMargin(label, new Insets(10)); 

    ScrollPane scrollPane = new ScrollPane(content); 

    StackPane root = new StackPane(); 
    root.getChildren().addAll(scrollPane, label); 

    Scene scene = new Scene(root, 200, 200); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

Или можно альтернативно настроить положение используя следующие условия перевода:

@Override 
public void start(Stage primaryStage) { 
    Label label = new Label("Hello World!"); 
    label.setTextFill(Color.WHITE); 

    Pane content = new Pane(label); 
    content.setPrefSize(2000, 2000); 
    content.setBackground(new Background(new BackgroundFill(
      new LinearGradient(0, 0, 1, 1, true, CycleMethod.NO_CYCLE, new Stop(0, Color.RED), new Stop(1, Color.BLUE)), 
      CornerRadii.EMPTY, 
      Insets.EMPTY))); 

    ScrollPane scrollPane = new ScrollPane(content); 

    double targetX = 10; 
    double targetY = 10; 

    InvalidationListener listener = o -> { 
     Bounds viewportBounds = scrollPane.getViewportBounds(); 
     Bounds contentBounds = content.getBoundsInLocal(); 
     Bounds labelBounds = label.getBoundsInLocal(); 

     double factorX = Math.max(contentBounds.getWidth() - viewportBounds.getWidth(), 0); 
     double factorY = Math.max(contentBounds.getHeight() - viewportBounds.getHeight(), 0); 

     label.setTranslateX(targetX + scrollPane.getHvalue() * factorX - labelBounds.getMinX()); 
     label.setTranslateY(targetY + scrollPane.getVvalue() * factorY - labelBounds.getMinY()); 
    }; 

    scrollPane.viewportBoundsProperty().addListener(listener); 
    scrollPane.hvalueProperty().addListener(listener); 
    scrollPane.vvalueProperty().addListener(listener); 
    label.boundsInLocalProperty().addListener(listener); 

    Scene scene = new Scene(scrollPane, 200, 200); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 

    listener.invalidated(null); 
} 
Смежные вопросы