【问题标题】:How to shift buttons in the GridPane?如何移动 GridPane 中的按钮?
【发布时间】:2017-01-27 07:57:55
【问题描述】:

当 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

【问题讨论】:

  • 请在您的问题中包含您的代码。

标签: html button javafx row gridpane


【解决方案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--;
        }
    });

【讨论】:

    【解决方案2】:

    您可以使用一些虚拟组件,例如空的Text 节点,或者为您的GridPane 手动设置RowConstraintsColumnConstraints

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

    我还将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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      • 2019-12-22
      • 2019-07-10
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 2015-11-19
      相关资源
      最近更新 更多