button - 如何在 JavaFX GridPane 中定位跨越多列的按钮?

标签 button layout javafx gridpane

我试图在第一行并排放置 3 个按钮,但它们都堆叠在一起。实际的按钮过程似乎工作正常,但按钮定位都是错误的。如何使用 GridPane 功能让这些按钮间隔开?

// Import the classes required
import java.util.Random;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class EventMatrix2 extends Application 
{
    public static void main(String[] args) 
    {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) 
    { 
        //Create Border Pane   
        BorderPane border = new BorderPane();

        //Add GridPane
        GridPane grid = addGridPane();
        border.setTop(grid);

        //Add Bottom HBox
        HBox bottomHBox = addBottomHBox();
        border.setBottom(bottomHBox);       

        // Title the stage
        primaryStage.setTitle("Zero/One Matrix"); 

        // Create a Scene object and set its size.
        Scene scene = new Scene(border);
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setWidth(300);
        primaryStage.setHeight(400);

        scene.setOnKeyPressed(ESCAPE ->
        {
           primaryStage.close(); 
        });
    }

    public GridPane addGridPane() 
    {        
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setPadding(new Insets(15, 12, 15, 12));
        pane.setHgap(1);
        pane.setVgap(1); 
        pane.setStyle("-fx-background-color: #336699;");

        //Create buttons
        Button resetZeroButton = new Button("Set To 0");
        Button resetOneButton = new Button("Set To 1");
        Button resetRandomButton = new Button("Set Random");

        //Create Empty grid
        for(int i = 0; i < 10; i++) 
                { 
                    for(int j = 0; j < 10; j++) 
                    {                 
                        //Create new text field
                        TextField number = new TextField();
                        pane.add(number, i , j + 1);                 
                    }
                }

        //Reset all to 0 Button
        resetZeroButton.setPrefSize(100, 20);
        resetZeroButton.setOnAction(new EventHandler<ActionEvent>() 
        {
            // Handle the event
            public void handle(ActionEvent event) 
            { 
                //Define local variables
                String fillZero;
                fillZero = String.valueOf(0);

                for(int i = 0; i < 10; i++) 
                { 
                    for(int j = 0; j < 10; j++) 
                    {                 
                        //Create new text field
                        TextField number = new TextField(fillZero);
                        pane.add(number, i , j + 1);
                        number.setAlignment(Pos.CENTER);  
                    }
                }
            }
        });

        //Reset all to 1 Button
        resetOneButton.setPrefSize(100, 20);
        resetOneButton.setOnAction(new EventHandler<ActionEvent>() 
        {
            // Handle the event
            public void handle(ActionEvent event) 
            { 
                //Define local variables
                String fillOne;
                fillOne = String.valueOf(1);

                for(int i = 0; i < 10; i++) 
                { 
                    for(int j = 0; j < 10; j++) 
                    {                 
                        //Create new text field
                        TextField number = new TextField(fillOne);
                        pane.add(number, i, j + 1);
                        number.setAlignment(Pos.CENTER);
                    }
                }
            }
        });

        //Reset all to random Button
        resetRandomButton.setPrefSize(125, 20);
        resetRandomButton.setOnAction(new EventHandler<ActionEvent>() 
        {
            // Handle the event
            public void handle(ActionEvent event) 
            { 
                //Declare local variables
                int intZeroOrOne;
                String fillRandom;

                //Create and Fill 10 x 10 Grid
                for(int i = 0; i < 10; i++) 
                { 
                    for(int j = 0; j < 10; j++) 
                    { 
                        //Randomly generate 0 or 1 
                        Random randomNumber = new Random();
                        intZeroOrOne = randomNumber.nextInt(2);
                        fillRandom = String.valueOf(intZeroOrOne);

                        //Create new text field
                        TextField number = new TextField(fillRandom);
                        pane.add(number, i, j + 1);
                        number.setAlignment(Pos.CENTER);
                    }
                }
            }
        });

        pane.getChildren().addAll(resetZeroButton, resetOneButton, resetRandomButton);

        return pane;
    }

    public HBox addBottomHBox() 
    {
        HBox bottomHBox = new HBox();
        bottomHBox.setPadding(new Insets(15, 12, 15, 12));
        bottomHBox.setSpacing(10);
        bottomHBox.setStyle("-fx-background-color: #336699;");
        bottomHBox.setAlignment(Pos.CENTER);

        Button quitButton = new Button("Quit");
        quitButton.setPrefSize(100, 20);
        quitButton.setOnAction(new EventHandler<ActionEvent>() // Fire event on button click
        {
            // Handle the event
            public void handle(ActionEvent event) 
            { 
                System.exit(0);
            }
        });

        bottomHBox.getChildren().addAll(quitButton);        

        return bottomHBox;
    }
}

最佳答案

由于您使用的是 GridPane,因此您需要在向网格 Pane 添加子项时指定行和列索引。尝试替换

pane.getChildren().addAll(resetZeroButton, resetOneButton, resetRandomButton);

下面一行

pane.add(resetZeroButton, 0, 0);
pane.add(resetOneButton, 1, 0);
pane.add(resetRandomButton, 2, 0);

Gridpane 的 add 方法以下列方式工作:

gridPane.add(child, columnIndex, rowIndex)

关于button - 如何在 JavaFX GridPane 中定位跨越多列的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25111665/

相关文章:

java - 使用按钮关闭 JavaFX 中的对话框

带有进度指示器的 JavaFX 线程不旋转,在非 FXThread 中完成的工作

带有文本和向下箭头的 C# 按钮

button - PyQt按钮点击区域(非矩形区域)

javascript - 我可以使用 javascript 强制浏览器 "flush"任何未决的布局更改吗?

java - 如何使用 javafx 绘制具有特定坐标的路径?

c# - 如何在中继器内动态按钮/我总是得到相同的值

java - 如何禁用 JFrame 窗口菜单栏中的图标化按钮?

swift - 将 View 添加为 subview 时,ContentInset 未更新

css - 网络表单布局 : Table or something else