0

这是我第一个用 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();

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

4

1 回答 1

0

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

StackPane 会堆叠Nodes您传递给它的所有内容。我建议你使用GridPane代替StackPane.

GridPane root = new GridPane();

要添加节点,请使用

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

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

matrix[x][y].setText(x + "," + y); 
于 2014-07-27T11:38:31.027 回答