如何在GridPane中移动按钮?

时间:2017-01-27 07:57:56

标签: html button javafx row gridpane

我希望当A和C消失时按钮B和D将保留在他们的位置,但是它不会转移到中心。我怎么能把它们移回原位呢?

     fifty.setOnAction(e->{
            fifty.setDisable(false);
            int counter = 2;
            ArrayList<String> variants = new ArrayList<>(Arrays.asList("a","b","c","d"));
            variants.remove(trueAnswerIndex);
            String variant;
            int n = 2;

            while(counter>0){
                variant = variants.get(randInt(0,n));
                switch(variant){
                    case "a":
                        gridButtons.getChildren().remove(a);
                        variants.remove("a");
                        break;
                    case "b":
                        gridButtons.getChildren().remove(b);
                        variants.remove("b");
                        break;
                    case "c":
                        gridButtons.getChildren().remove(c);
                        variants.remove("c");
                        break;
                    case "d":
                        gridButtons.getChildren().remove(d);
                        variants.remove("d");
                        break;
                }
                counter--;
                n--;
            }
        });

https://i.stack.imgur.com/pPpC4.jpg

2 个答案:

答案 0 :(得分:1)

只需停用按钮的可见性,而不是删除按钮。

fifty.setOnAction(e->{
        fifty.setDisable(false);
        int counter = 2;
        ArrayList<String> variants = new ArrayList<>(Arrays.asList("a","b","c","d"));
        variants.remove(trueAnswerIndex);
        String variant;
        int n = 2;

        while(counter>0){
            variant = variants.get(randInt(0,n));
            switch(variant){
                case "a":
                    a.setVisible(false);
                    variants.remove("a");
                    break;
                case "b":
                    b.setVisible(false);
                    variants.remove("b");
                    break;
                case "c":
                    c.setVisible(false);
                    variants.remove("c");
                    break;
                case "d":
                    d.setVisible(false);
                    variants.remove("d");
                    break;
            }
            counter--;
            n--;
        }
    });

答案 1 :(得分:0)

您可以使用一些虚拟组件,例如空Text个节点,也可以为RowConstraints手动设置ColumnConstraintsGridPane

后者是我的首选方法。在下面的示例中,我为前两行和列设置了这些约束,因此即使按钮被隐藏或删除,其他列也不会自动调整大小。

我还将GridPane置于另一个Node内,否则会填满整个Scene

public class JavaFXTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Group root = new Group();
        GridPane pane = new GridPane();

        for (int i = 0; i < 2; i++) {
            RowConstraints rc = new RowConstraints();
            rc.setPrefHeight(100);

            ColumnConstraints cc = new ColumnConstraints();
            cc.setPercentWidth(100);

            pane.getRowConstraints().add(rc);
            pane.getColumnConstraints().add(cc);
        }

        Button b1 = new Button("1");
        b1.setPrefSize(100, 100);

        Button b2 = new Button("2");
        b2.setPrefSize(100, 100);

        Button b3 = new Button("3");
        b3.setPrefSize(100, 100);

        Button b4 = new Button("4");
        b4.setPrefSize(100, 100);

        b1.setOnAction(e -> {
            b2.setVisible(false);
        });

        b2.setOnAction(e -> {
            b3.setVisible(false);
        });

        b3.setOnAction(e -> {
            b4.setVisible(false);
        });

        b4.setOnAction(e -> {
            b1.setVisible(false);
        });

        pane.add(b1, 0, 0);
        pane.add(b2, 1, 0);
        pane.add(b3, 0, 1);
        pane.add(b4, 1, 1);

        root.getChildren().add(pane);
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
相关问题