JavaFX中的按钮矩阵(如何创建和排列)

时间:2014-07-27 10:51:35

标签: java matrix javafx stack pane

这是我用JavaFX编写的第一个程序(而不是Swing)。我只是通过在按钮矩阵中显示数据集来开始小规模(我不想使用表格)。

package matrixjavafx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.Random;    // required for random number generation
/**
 *
 * @author Matt
 */

public class MatrixJavaFX extends Application {

Button[][] matrix; //names the grid of buttons

@Override
public void start(Stage primaryStage) {

    int SIZE = 10;
    int length = SIZE;
    int width = SIZE;

    StackPane root = new StackPane();

    matrix = new Button[width][length]; // allocates the size of the matrix

    // runs a for loop and an embedded for loop to create buttons to fill the size of the matrix
    // these buttons are then added to the matrix
    for(int y = 0; y < length; y++)
    {
            for(int x = 0; x < width; x++)
            {
                Random rand = new Random(); // Creates new Random object for generating random numbers

                // The following variables are generated using Random object rand.
                // rand.nextInt(2) generates a double ranging from 0.000 to 0.999.
                // setting the target variable to be an integer  truncates the range to 0 - 1.
                int rand1 = rand.nextInt(2); // Declare and initialize random integer (0 - 1) + 1

                matrix[x][y] = new Button(/*"(" + rand1 + ")"*/); //creates new random binary button     
                matrix[x][y].setText("(" + rand1 + ")");   // Sets the text inside the matrix

                matrix[x][y].setOnAction(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent event) {
                        System.out.println("Random Binary Matrix (JavaFX)");
                    }
                });

                root.getChildren().add(matrix[x][y]);
            }
    }        


    Scene scene = new Scene(root, 500, 500);

    primaryStage.setTitle("Random Binary Matrix (JavaFX)");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    greeting(); // Greets the User and explains the purpose of the program

    launch(args); // Opens the window that generates and displays the matrix

    //new MatrixSwingGUI(width, length); // makes new MatrixSwing with 2 parameters

    thanks(); // Thanks User and indicates that the program is about to close

}

public static void greeting()
{
    // Greets the User and displays the program's purpose
    System.out.println("Welcome to the Random Matrix Generator\n");
    System.out.println("The purpose of this program is to generate and display "
        + "\na 10 x 10 matrix of random 0's, and 1's.");

}

public static void thanks() // Thanks User and indicates that the program is about to close
{
    System.out.println("\nGoodbye and Thank You for using the Random Matrix Generator\n\n");
} // End thanks method

}

问题的核心似乎是以下行,因为默认情况下,所有按钮都在窗格中心堆叠在一起。

StackPane root = new StackPane();

那就是说,我希望按钮能够在所有长度和宽度上并排排列,从而得到一个n x m矩阵。应该怎么做才能做到这一点?需要导入哪些额外的javafx.scene。*文件?

1 个答案:

答案 0 :(得分:0)

基本问题是您为项目选择的布局。您可以获得有关布局的深入知识,javafx支持here

StackPane会堆叠您传递的所有Nodes。我建议您使用GridPane代替StackPane

GridPane root = new GridPane();

要添加节点,请使用

root.add(matrix[x][y], y, x);

P.S。我还建议您重命名按钮,以便更清楚地表示矩阵的行和列的按钮。你可以使用某些东西,尽管它是完全可选的! :)

matrix[x][y].setText(x + "," + y);