2014-12-02 71 views
0

方案:舞臺包含場景。場景包含一個StackPane。 StackPane和Scene的高度和寬度綁定在一起。 StackPane包含一個Shape,它是Rectangle和Shape的聯合。將Shape類綁定到StackPane高度

問題 - 我正在將Shape類綁定到StackPane的高度時遇到問題。如何在我的示例中綁定Shape類的特定部分或完整的Shape類?

要求 - 我有2個要求。

  1. 當我最大化階段,StackPane得到增加,因爲 高度和寬度綁定到場景但形狀不增加。 I 需要形狀的(smallShapebigRectangle)僅在高度方面增加 。
  2. 當我最大化階段時,StackPane應該增加,只有更大的矩形高度應該增加,但不應該增加其他的小矩形,即bigRectangle高度應該只增加。

下面是我的代碼片斷

package application; 

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.layout.StackPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 
import javafx.scene.shape.RectangleBuilder; 
import javafx.scene.shape.Shape; 
import javafx.stage.Stage; 

public class BindingShape extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     StackPane stackPane = new StackPane(); 
     stackPane.setPrefHeight(200); 
     stackPane.setPrefWidth(200); 
     stackPane.setStyle("-fx-background-color: BEIGE"); 
     Shape smallShape = RectangleBuilder.create() 
       .x(0) 
       .y(3) 
       .arcWidth(6) 
       .arcHeight(6) 
       .width(50) // allow overlap for union of tab and content rectangle 
       .height(50) 
       .build(); 

     Rectangle bigRectangle = RectangleBuilder.create() 
       .x(25) 
       .y(0) 
       .arcWidth(10) 
       .arcHeight(10) 
       .width(100) 
       .height(100) 
       .build(); 
     Shape unionShape = Shape.union(smallShape, bigRectangle); 
     unionShape.setFill(Color.rgb(0, 0, 0, .50)); 
     unionShape.setStroke(Color.BLUE); 

     Group shapeGroup = new Group(); 
     shapeGroup.getChildren().add(unionShape); 
     stackPane.getChildren().add(shapeGroup); 

     Group paneGroup = new Group(); 
     paneGroup.getChildren().add(stackPane); 

     Scene scene = new Scene(paneGroup, 400, 400,Color.LIGHTSKYBLUE); 
     stackPane.prefHeightProperty().bind(scene.heightProperty().divide(2)); 
     stackPane.prefWidthProperty().bind(scene.widthProperty().divide(2)); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 

請讓我知道解決的辦法。提前致謝。

+0

我用Java8(1.8.0_11)與FX2,在該版本中,RectangleBuilder已棄用且Shape.union()不存在......您使用的是哪個版本? – 2014-12-02 21:49:59

+0

我正在使用Java8(1.8.0_25)。是的,在這個版本RectangleBuilder是deprectaed,但我們仍然能夠起訴它。如果不是RectangleBuilder,我需要直接使用Rectangle。但在這種情況下,問題也會持續存在。請讓我知道,如果通過直接使用新的矩形()可以實現上述要求。 Shape.union對我來說確實存在。下面的鏈接有它。 https://docs.oracle.com/javafx/2/api/javafx/scene/shape/Shape.html – 2014-12-03 03:33:37

回答

0

由於工會不能在部分來調整,我將重建工會如果容器已調整就像:

public class BindStackPaneToScene extends Application { 
    Shape union; 
    Shape makeShape(double w, double h) { 
     Rectangle smallShape = new Rectangle(0, 3, 50, 50); 
     smallShape.setArcHeight(6); 
     smallShape.setArcWidth(6); 

     Rectangle bigRectangle = new Rectangle(25, 3, w/2, h/2); 
     bigRectangle.setArcHeight(10); 
     bigRectangle.setArcWidth(10); 

     Shape unionShape = Shape.union(smallShape, bigRectangle); 
     unionShape.setFill(Color.rgb(0, 0, 0, .50)); 
     unionShape.setStroke(Color.BLUE); 

     return unionShape; 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 

     StackPane stackPane = new StackPane(); 
     stackPane.setPrefHeight(200); 
     stackPane.setPrefWidth(200); 
     stackPane.setStyle("-fx-background-color: BEIGE"); 

     union = makeShape(200,200); 

     Group shapeGroup = new Group(); 
     shapeGroup.getChildren().add(union); 
     stackPane.getChildren().add(shapeGroup); 

     stackPane.heightProperty().addListener((p, o, n) -> { 
      if (union != null) shapeGroup.getChildren().remove(union); 
      union = makeShape(stackPane.getWidth(), n.doubleValue()); 
      shapeGroup.getChildren().add(union); 
     }); 

     Group paneGroup = new Group(); 
     paneGroup.getChildren().add(stackPane); 

     Scene scene = new Scene(paneGroup, 400, 400,Color.LIGHTSKYBLUE); 
     stackPane.prefHeightProperty().bind(scene.heightProperty().divide(1)); 
     stackPane.prefWidthProperty().bind(scene.widthProperty().divide(1)); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+0

非常感謝。它的工作,也是我能夠調整我的其他要求的代碼。 – 2014-12-03 18:45:48

相關問題