subscene中的组合框在javaFX中不起作用

时间:2014-05-09 18:29:18

标签: java combobox javafx

在下面的示例中,我尝试在子场景中添加一个组合框。但它没有正常工作。如果我从组合框中选择一个值,它就没有在组合框中设置,并且组合框值没有正确显示。

                    package javaFxExamples;

                    import javafx.application.Application;
                    import javafx.beans.binding.When;
                    import javafx.beans.property.BooleanProperty;
                    import javafx.beans.property.DoubleProperty;
                    import javafx.beans.property.SimpleBooleanProperty;
                    import javafx.beans.property.SimpleDoubleProperty;
                    import javafx.geometry.Insets;
                    import javafx.scene.*;
                    import javafx.scene.control.CheckBox;
                    import javafx.scene.control.ComboBox;
                    import javafx.scene.control.Label;
                    import javafx.scene.control.Slider;
                    import javafx.scene.image.Image;
                    import javafx.scene.layout.HBox;
                    import javafx.scene.paint.Color;
                    import javafx.scene.paint.PhongMaterial;
                    import javafx.scene.shape.DrawMode;
                    import javafx.scene.shape.MeshView;
                    import javafx.scene.shape.TriangleMesh;
                    import javafx.scene.transform.Rotate;
                    import javafx.stage.Stage;


                      public class MeshCubeSubScene extends Application {
                      double anchorX, anchorY;
                      private static final float EDGE_LENGTH = 380;
                      private double anchorAngleX = 0;
                      private double anchorAngleY = 0;

                      private final DoubleProperty angleX = new SimpleDoubleProperty(25);
                      private final DoubleProperty angleY = new SimpleDoubleProperty(40);

                      PerspectiveCamera camera = new PerspectiveCamera(false);


                      private Slider zSlider;
                      private final DoubleProperty zPos = new SimpleDoubleProperty(-200);
                      ComboBox comboBox;

                      private CheckBox meshCheckBox;


                      private final BooleanProperty showWireframe = new SimpleBooleanProperty(false);


                      static TriangleMesh createMesh(float w, float h, float d) {


                        if (w * h * d == 0) {
                          return null;
                        }


                        float hw = w / 2f;
                        float hh = h / 2f;
                        float hd = d / 2f;


                        float x0 = 0f;
                        float x1 = 1f / 4f;
                        float x2 = 2f / 4f;
                        float x3 = 3f / 4f;
                        float x4 = 1f;
                        float y0 = 0f;
                        float y1 = 1f / 3f;
                        float y2 = 2f / 3f;
                        float y3 = 1f;


                        TriangleMesh mesh = new TriangleMesh();
                        mesh.getPoints().addAll(
                            hw, hh, hd,   //point A
                            hw, hh, -hd,  //point B
                            hw, -hh, hd,  //point C
                            hw, -hh, -hd, //point D
                            -hw, hh, hd,  //point E
                            -hw, hh, -hd, //point F
                            -hw, -hh, hd, //point G
                            -hw, -hh, -hd //point H
                        );
                        mesh.getTexCoords().addAll(
                          x1, y0,
                          x2, y0,
                          x0, y1,
                          x1, y1,
                          x2, y1,
                          x3, y1,
                          x4, y1,
                          x0, y2,
                          x1, y2,
                          x2, y2,
                          x3, y2,
                          x4, y2,
                          x1, y3,
                          x2, y3
                        );
                        mesh.getFaces().addAll(
                            0, 10, 2, 5, 1, 9,   //triangle A-C-B
                            2, 5, 3, 4, 1, 9,    //triangle C-D-B
                            4, 7, 5, 8, 6, 2,    //triangle E-F-G
                            6, 2, 5, 8, 7, 3,    //triangle G-F-H
                            0, 13, 1, 9, 4, 12,  //triangle A-B-E
                            4, 12, 1, 9, 5, 8,   //triangle E-B-F
                            2, 1, 6, 0, 3, 4,    //triangle C-G-D
                            3, 4, 6, 0, 7, 3,    //triangle D-G-H
                            0, 10, 4, 11, 2, 5,  //triangle A-E-C
                            2, 5, 4, 11, 6, 6,   //triangle C-E-G
                            1, 9, 3, 4, 5, 8,    //triangle B-D-F
                            5, 8, 3, 4, 7, 3     //triangle F-D-H
                        );
                        mesh.getFaceSmoothingGroups().addAll(
                            0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5
                        );
                        return mesh;
                      }

                      @Override
                      public void start(Stage primaryStage) {
                        primaryStage.setTitle("MeshCubeSubScene");

                        Image diffuseMap = 
                          new Image(MeshCubeSubScene.class
                            .getResource("cbGn_pof-bm.png")
                            .toExternalForm());

                        PhongMaterial earthMaterial = new PhongMaterial();
                        earthMaterial.setDiffuseMap(diffuseMap);


                        MeshView cube =
                          new MeshView(createMesh(EDGE_LENGTH, EDGE_LENGTH, EDGE_LENGTH));
                        cube.setMaterial(earthMaterial);

                        final Group parent = new Group(cube);        
                        parent.setTranslateX(450);
                        parent.setTranslateY(400);
                        parent.setTranslateZ(0);

                        Rotate xRotate;
                        Rotate yRotate;
                        parent.getTransforms().setAll(
                          xRotate = new Rotate(0, Rotate.X_AXIS),
                          yRotate = new Rotate(0, Rotate.Y_AXIS)
                        );
                        xRotate.angleProperty().bind(angleX);
                        yRotate.angleProperty().bind(angleY);


                        final Group root = new Group(parent); 

                        final Scene scene = new Scene(root, 900, 900, true);
                        scene.setFill(Color.WHITE);

                        scene.setOnMousePressed(event -> {                
                          anchorX = event.getSceneX();                
                          anchorY = event.getSceneY();  
                          anchorAngleX = angleX.get();
                          anchorAngleY = angleY.get();
                        });         

                        scene.setOnMouseDragged(event -> {                
                          angleX.set(anchorAngleX - (anchorY -  event.getSceneY()));
                          angleY.set(anchorAngleY + anchorX -  event.getSceneX());
                        });         

                        PointLight pointLight = new PointLight(Color.WHITE);        
                        pointLight.setTranslateX(300);
                        pointLight.setTranslateY(200);
                        pointLight.setTranslateZ(-2000);

                        root.getChildren().add(pointLight);

                        scene.setCamera(camera);        
                        primaryStage.setScene(scene);


                        // SubScene code
                        PerspectiveCamera subSceneCamera = new PerspectiveCamera(false);
                        scene.setCamera(subSceneCamera);
                        Label zLabel = new Label("Z");
                        zSlider = new Slider(-200, 3000, -200);
                        meshCheckBox = new CheckBox("Wireframe");
                        comboBox = new ComboBox();
                        comboBox.getItems().add("test1");
                        comboBox.getItems().add("test2");
                        comboBox.getItems().add("test3");

                        HBox subSceneGroup = new HBox(70, new HBox(10, zLabel, zSlider), meshCheckBox,comboBox);
                        subSceneGroup.setPadding(new Insets(100, 100, 0, 100));
                        SubScene subScene = new SubScene(subSceneGroup, 600, 200);
                        subScene.setLayoutX(180);
                        subScene.setLayoutY(720);


                        root.getChildren().add(subScene);


                        // Setup binding
                        zSlider.valueProperty().bindBidirectional(zPos);
                        parent.translateZProperty().bind(zPos);


                        meshCheckBox.selectedProperty().bindBidirectional(showWireframe);
                        cube.drawModeProperty().bind(new When(showWireframe).then(DrawMode.LINE).otherwise(DrawMode.FILL));


                        primaryStage.show();    
                      }


                      /**
                       * @param args the command line arguments
                       */
                      public static void main(String[] args) {
                        launch(args);
                      }
                    }

1 个答案:

答案 0 :(得分:0)

只是添加上面的问题 subcene存在许多问题,包括但不限于(根据我的经验,仍尝试解决它们): - ComboBox不显示所选项目 - 如果子场景不位于0,0,则在单击下拉列表时,ComboBox弹出窗口会定位到其他位置。 - 选择时,子场景中的选项卡不会显示其组件。 - 其他gui显示问题的数量。