如何在JavaFX GridPane中定位跨多个列的按钮?

时间:2014-08-04 04:13:38

标签: 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;
    }
}

1 个答案:

答案 0 :(得分:1)

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

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

使用以下行

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

Gridpane's add方法的工作方式如下:

gridPane.add(child, columnIndex, rowIndex)