2016-05-04 29 views
0

特定のイベントを使用して、HBoxに小さな四角形を配置します。ウィンドウのサイズが変更されていないときの配置は完璧ですが、たとえば小さな画面から全画面に移動するときは、配置が間違っています(もちろん、配置時にXの値が得られるためです)。その特定の瞬間におけるHBoxの幅)。HBoxのJavaノードの動的位置

質問:

がどのように私はこれらの位置を動的にすることができますので、私は、ウィンドウのサイズを変更するとき、彼らは割合に滞在?

画像: Normal view Fullscreen

コード:

@FXML HBox tagLine; // initializes the HBox 

... 

public void addTag(String sort) { 
    Rectangle rect = new Rectangle(20, tagLine.getHeight()); 
    double pos = timeSlider.getValue()/100 * tagLine.getWidth(); // retrieves position at the moment of being called 
    rect.setTranslateX(pos); 
    rect.setOnMouseEntered(new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent event) { 
      showNotification("Gemarkeerde gebeurtenis: " + sort); 
     } 
    }); 
    rect.setOnMouseExited(new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent event) { 
      notificationHide.play(); 
     } 
    }); 

    tagLine.getChildren().add(rect); 
} 
+0

どのようにしてHBoxの中に矩形を配置するのですか?マージン?ノードをその左側に置く? 'translateX'? – fabian

+0

コードを表示する必要があります。質問を編集し、矩形の翻訳に現在使用しているロジックを追加するだけです。 – ItachiUchiha

答えて

1

責任サイズと形状を翻訳しながら、あなたは、あなたがする必要があるということである考慮に入れる必要があるいくつかのこと:

  • 応じ

翻訳はノードの幅に依存している場合は、

  • その中心から形状を翻訳し、その特定のノードのとに加えられた変更に耳を傾け、変換プロパティに変更を加えます上記の両方の点は、実装には欠落しているようです。 HBoxのwidthプロパティを聞くことは決してありません。 posの計算もRectangleの中心を考慮していません。

    ここでは、HBoxのサイズにかかわらず、Rectangleを中央に維持しようとする例を示します。

    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.layout.HBox; 
    import javafx.scene.paint.Color; 
    import javafx.scene.shape.Rectangle; 
    import javafx.stage.Stage; 
    
    public class Main extends Application { 
    
        private static final int SIDE = 40; 
        private static final double DEFAULT_WIDTH = 200; 
        private static final double DEFAULT_POSITION = 100; 
    
        @Override 
        public void start(Stage primaryStage) { 
    
         Rectangle rectangle = new Rectangle(SIDE, SIDE); 
         HBox root = new HBox(rectangle); 
         root.setPrefWidth(DEFAULT_WIDTH); 
    
         rectangle.translateXProperty().bind(root.widthProperty().multiply(DEFAULT_POSITION/DEFAULT_WIDTH).subtract(SIDE/2)); 
    
         Scene scene = new Scene(root); 
         primaryStage.setScene(scene); 
         primaryStage.show(); 
        } 
    
        public static void main(String[] args) { 
         launch(args); 
        } 
    } 
    

    enter image description here

  • 関連する問題