单元格中的JavaFX GridPane按钮彼此距离太远

单元格中的JavaFX GridPane按钮彼此距离太远,javafx,constraints,fxml,gridpane,Javafx,Constraints,Fxml,Gridpane,我编写了一个在网格窗格中添加按钮的代码,当我运行代码时,一切都正常工作。唯一的问题是,它们单元格中的按钮彼此距离太远,我得到的“最佳”结果是下面的结果,但这并不好,因为按钮之间仍然存在障碍(而且它们不在网格的中心)。 创建按钮并将其添加到网格的代码: for (int i = 0; i < columns; i++) { for (int j = 0; j < rows; j++) { b = new Butto

我编写了一个在网格窗格中添加按钮的代码,当我运行代码时,一切都正常工作。唯一的问题是,它们单元格中的按钮彼此距离太远,我得到的“最佳”结果是下面的结果,但这并不好,因为按钮之间仍然存在障碍(而且它们不在网格的中心)。 创建按钮并将其添加到网格的代码:

        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                b = new Button(" ");
                b.setMinSize(30, 30);
                b.setMaxSize(30, 30);
                b.setStyle("-fx-font-size:11");
                bCont.TheBoard.add(b, i, j);

//              bCont.TheBoard.setVgap(5.0);
//              bCont.TheBoard.setHgap(5.0);

//              GridPane.setMargin(b, new Insets(5, 5, 5, 5));
                GridPane.setHalignment(b, HPos.CENTER);
                GridPane.setValignment(b, VPos.CENTER);
                GridPane.setVgrow(b, Priority.ALWAYS);
                GridPane.setHgrow(b, Priority.ALWAYS);
            }
        }
        
        ColumnConstraints constraints = new ColumnConstraints();
        constraints.setPercentWidth(85 / columns);
        RowConstraints rowconst = new RowConstraints();
        rowconst.setPercentHeight(85 / rows);
        for (int t = 0; t < columns; t++) {
            bCont.TheBoard.getColumnConstraints().add(constraints);
        }
        for (int t = 0; t < rows; t++) 
            bCont.TheBoard.getRowConstraints().add(rowconst);
for(int i=0;i
输出示例(第一幅图像为3x3的板尺寸,第二幅为10x10):

如果还需要董事会的FXML代码:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="853.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MS.BoardCONTROLLER">
   <children>
      <GridPane fx:id="TheBoard" alignment="TOP_CENTER" layoutX="200.0" layoutY="118.0" AnchorPane.bottomAnchor="90.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
      </GridPane>
      <HBox alignment="BOTTOM_LEFT" fillHeight="false" layoutX="14.0" layoutY="560.0" prefHeight="51.0" prefWidth="290.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="700.0" AnchorPane.topAnchor="560.0">
         <children>
            <Button fx:id="BackMenu" mnemonicParsing="false" onAction="#BackMenu" text="Back to menu" />
            <Button fx:id="ResetBoard" mnemonicParsing="false" onAction="#ResetBoard" text="Reset" textFill="RED">
               <HBox.margin>
                  <Insets left="10.0" />
               </HBox.margin>
            </Button>
            <Button fx:id="Music" mnemonicParsing="false" onAction="#Music" text="Music">
               <HBox.margin>
                  <Insets left="10.0" />
               </HBox.margin>
            </Button>
         </children>
      </HBox>
   </children>
</AnchorPane>

试试这段代码

Parent root = FXMLLoader.load(App.class.getResource("App.fxml"));
Scene scene = new Scene(root);

primaryStage.setTitle("Example");
primaryStage.setScene(scene);
primaryStage.show();

int i=0;
int j=0;
GridPane gridPane = (GridPane)scene.lookup("#mygrid");
for(i=0;i<30;i++) {
    for(j=0;j<12;j++) {
        Button button1 = new Button(" ");
        gridPane.add(button1, i, j, 1, 1);
    }
}
Parent root=fxmloader.load(App.class.getResource(“App.fxml”);
场景=新场景(根);
primaryStage.setTitle(“示例”);
初级阶段。场景(场景);
primaryStage.show();
int i=0;
int j=0;
GridPane GridPane=(GridPane)scene.lookup(“#mygrid”);
对于(i=0;i请尝试此代码

Parent root = FXMLLoader.load(App.class.getResource("App.fxml"));
Scene scene = new Scene(root);

primaryStage.setTitle("Example");
primaryStage.setScene(scene);
primaryStage.show();

int i=0;
int j=0;
GridPane gridPane = (GridPane)scene.lookup("#mygrid");
for(i=0;i<30;i++) {
    for(j=0;j<12;j++) {
        Button button1 = new Button(" ");
        gridPane.add(button1, i, j, 1, 1);
    }
}
Parent root=fxmloader.load(App.class.getResource(“App.fxml”);
场景=新场景(根);
primaryStage.setTitle(“示例”);
初级阶段。场景(场景);
primaryStage.show();
int i=0;
int j=0;
GridPane GridPane=(GridPane)scene.lookup(“#mygrid”);

对于(i=0;i您期望的输出是什么?)这是一个扫雷板,我试图使按钮彼此更接近调试布局问题的第一步是删除所有约束,硬编码的min/max/pref和css-并使用非常小的示例(如3*3)为了了解布局是如何工作的。或者换句话说,请。您期望的输出是什么?这是一个扫雷板,我正在尝试让按钮彼此靠近第一步(在阅读了javadoc:)在调试布局问题时,需要删除所有约束、硬编码的min/max/pref和css,并使用非常小的示例(如3*3)来理解布局的工作方式。或者换句话说,请。我添加了这行“gridPane.add(button1,I,j,1,1);”并且还更改了FXML文件,使其具有与您相同的属性,但不幸的是,它不起作用。删除约束,然后CSSThank you!删除所有约束,并将FXML文件更改为与您的类似,这样做了。我添加了此行“gridPane.add(button1,I,j,1,1);”并且还更改了FXML文件,使其具有与您相同的属性,但不幸的是,它不起作用。删除约束,然后CSSThank you!删除我所有的约束,并将FXML文件更改为与您的类似,这样做了。