使用复选框

时间:2017-08-23 11:40:25

标签: javafx

用户可以根据需要添加任意数量的根和坐标的树视图。坐标生成 矩形,在根节点上水平显示,这里是代码

import javafx.scene.Node;

abstract class MyNode {
    private final String label;
    private Node rectangle;

    MyNode(String label) {
        this.label = label;
    }

    String getLabel() {
        return label;
    }

    Node getRectangle() {
        return rectangle;
    }

    void setRectangle(Node rectangle) {
        this.rectangle = rectangle;
    }
}

class MyRootNode extends MyNode {
    MyRootNode(String label) {
        super(label);
    }
}
class MyCoordinateNode extends MyNode {
    MyCoordinateNode(String label) {
        super(label);
    }
}

,Main类是

public class Main extends Application {
    private static int rootNr = 0;
    private static int coordinateNr = 0;

    public static void main(String[] args) {
        launch(args);
    }

    private static final Map<TreeItem<MyNode>, BorderPane> map = new HashMap<>();

    @Override
    public void start(Stage primaryStage) {
        BorderPane root = new BorderPane();

        TreeItem<MyNode> mainTree = new TreeItem<>(new MyRootNode("Main System"));
        mainTree.setExpanded(true);

        TreeView<MyNode> treeView = new TreeView<>(mainTree);
        treeView.setCellFactory(p -> new AddMenuTreeCell());
        treeView.setOnMouseClicked((event) -> {
            final TreeItem<MyNode> treeItem = treeView.getSelectionModel().getSelectedItem();
            if (treeItem.getValue() instanceof MyRootNode) {
                root.setCenter(getRootsPanel(treeItem));
            } else {
                root.setCenter(map.get(treeItem));
            }
        });

        root.setLeft(treeView);

        Scene scene = new Scene(root, 700, 700);
        primaryStage.setTitle("Tree View");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private static class AddMenuTreeCell extends TextFieldTreeCell<MyNode> {
        private ContextMenu menu = new ContextMenu();

        AddMenuTreeCell() {
            MenuItem newitem1 = new MenuItem("Insert Root");
            MenuItem newitem2 = new MenuItem("Insert Coordinates");
            menu.getItems().addAll(newitem1, newitem2);
            newitem1.setOnAction(arg0 -> {
                TreeItem<MyNode> item = new TreeItem<>(new MyRootNode("Root" + rootNr++));
                getTreeItem().getChildren().add(item);
            });
            newitem2.setOnAction(arg0 -> {
                TreeItem<MyNode> uxItem1 = new TreeItem<>(new MyCoordinateNode("X"));
                map.put(uxItem1, getRightPane(uxItem1));

                TreeItem<MyNode> uyItem1 = new TreeItem<>(new MyCoordinateNode("Y"));
                map.put(uyItem1, getRightPane(uyItem1));

                TreeItem<MyNode> newLeaf = new TreeItem<>(new MyRootNode("Coordinates" + coordinateNr++));
                newLeaf.getChildren().add(uxItem1);
                newLeaf.getChildren().add(uyItem1);
                getTreeItem().getChildren().add(newLeaf);
            });
        }

        @Override
        public void updateItem(MyNode item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                if (!isEditing()) {
                    setText(item.getLabel());
                    setGraphic(getTreeItem().getGraphic());
                    if (item instanceof MyRootNode) {
                        setContextMenu(menu);
                    }
                }
            }
        }
    }

    private static BorderPane getRightPane(final TreeItem<MyNode> curTreeItem) {
        TextField textf1 = new TextField();
        TextField textf2 = new TextField();
        BorderPane root1 = new BorderPane();
        VBox vbox = new VBox(20);
        vbox.setPadding(new Insets(10));
        HBox h1 = new HBox(7);
        HBox h2 = new HBox(7);

        textf1.setPrefWidth(100);
        textf1.setPromptText("Enter Height");
        textf1.setOnKeyReleased(event -> {
            if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
                Rectangle rect = getRectangle(textf1, textf2, Color.BLUE);
                root1.setCenter(rect);
                curTreeItem.getValue().setRectangle(rect);
            }
        });
        textf2.setPrefWidth(100);
        textf2.setPromptText("Enter Width");
        textf2.setOnKeyReleased(event -> {
            if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
                Rectangle rect = getRectangle(textf1, textf2, Color.RED);
                root1.setCenter(rect);
                curTreeItem.getValue().setRectangle(rect);
            }
        });

        h1.getChildren().addAll(new Label("Y:"), textf1);
        h2.getChildren().addAll(new Label("X:"), textf2);
        vbox.getChildren().addAll(h1, h2);
        root1.setLeft(vbox);
        return root1;
    }

    private static Rectangle getRectangle(TextField textf1, TextField textf2, final Color blue) {
        Rectangle rect = new Rectangle();
        rect.setHeight(Double.parseDouble(textf1.getText()));
        rect.setWidth(Double.parseDouble(textf2.getText()));
        rect.setFill(null);
        rect.setStroke(blue);

        return rect;
    }

    private static BorderPane getRootsPanel(final TreeItem<MyNode> treeItem) {
        BorderPane root = new BorderPane();


        CheckBox box1 = new CheckBox("Change order");
        CheckBox box2 = new CheckBox("Change View");
        VBox vbox = new VBox(30, box1,box2);



        HBox hbox = new HBox(10);
        hbox.setPadding(new Insets(40));
        hbox.setAlignment(Pos.TOP_CENTER);

        final List<MyNode> coordinateNodes = getCoordinateNodes(treeItem);
        for (final MyNode coordinateNode : coordinateNodes) {
            if (coordinateNode.getRectangle() != null) {
                Platform.runLater(() -> hbox.getChildren().addAll(coordinateNode.getRectangle()));
            }
        }

        Platform.runLater(() -> root.setLeft(hbox));

        return root;
    }

    private static List<MyNode> getCoordinateNodes(final TreeItem<MyNode> treeItem) {
        final List<MyNode> result = new ArrayList<>();

        if (treeItem.getValue() instanceof MyRootNode) {
            for (final TreeItem<MyNode> child : treeItem.getChildren()) {
                result.addAll(getCoordinateNodes(child));
            }
        } else {
            result.add(treeItem.getValue());
        }

        return result;
    }
}

我的问题是,如何用root这两个复选框绑定所有新生成的矩形,如果选中第一个复选框,那么矩形的顺序应该改为(1,2,3到3,2, 1),第二个复选框矩形从水平视图移动到垂直视图,如果两个都选中,则应用两个特征。

由于用户可以根据需要添加矩形,因此我很难绑定所有矩形。

我试着把赏金放在问题上,但仍然没有得到任何回应。请我拼命寻找这个问题的帮助。请

我将非常感激。

0 个答案:

没有答案