2017-01-27 37 views
0

我想要按钮B和D会保留在他们的位置,当A和C将会消失,而不是它会转移到中心。我怎么能把他们转移回他们的地方?如何在GridPane中移动按钮?

 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

+0

请在您的问题中包括您的代码。 – Nash

回答

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--; 
     } 
    }); 
0

您可以使用一些虚拟组件,如空Text节点,或者手动设置RowConstraintsColumnConstraintsGridPane

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

我也把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); 
    } 
}