2014-12-02 65 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

相关问题