如何使用JavaFX显示10×10的方形矩阵?

时间:2015-10-02 21:45:23

标签: java matrix javafx

JavaFX Basics编写一个显示10×10方阵的程序。矩阵中的每个元素是0或1,随机生成。显示文本字段中心的每个数字。使用TextField setText方法将值0或1设置为字符串。 截至目前,我只能打印一个随机数。如何显示10×10矩阵?

    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.GridPane;
    import javafx.stage.Stage;

    import java.util.Random; 

    public class Matrix extends Application {
            public class Matrix extends Application {

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

    @Override
    public void start(Stage primaryStage) {

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

GridPane root = new GridPane();


matrix = new Button[width][length]; 
for(int y = 0; y < length; y++)
{
        for(int x = 0; x < width; x++)
        {
            Random rand = new Random(); 
            int rand1 = rand.nextInt(2); 

            matrix[x][y] = new Button(/*"(" + rand1 + ")"*/); 
            matrix[x][y].setText("(" + rand1 + ")");   //

            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();
    }

    public static void main(String[] args) {

        launch(args); 
            }

    }        

1 个答案:

答案 0 :(得分:2)

您实际上已经完成了大部分艰苦的工作,而您唯一错过的就是查找GridPane API。您的代码所做的是在彼此之上添加40个按钮,因为您永远不会更改GridPane的行或列!

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;    
import javax.xml.soap.Text;
import java.util.ArrayList;
import java.util.Random;

    public class Main extends Application {    

        @Override
        public void start(Stage primaryStage) {

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

            GridPane root = new GridPane();    

            for(int y = 0; y < length; y++){
                for(int x = 0; x < width; x++){

                    Random rand = new Random();
                    int rand1 = rand.nextInt(2);

                    // Create a new TextField in each Iteration
                    TextField tf = new TextField();
                    tf.setPrefHeight(50);
                    tf.setPrefWidth(50);
                    tf.setAlignment(Pos.CENTER);
                    tf.setEditable(false);
                    tf.setText("(" + rand1 + ")");

                    // Iterate the Index using the loops
                    root.setRowIndex(tf,y);
                    root.setColumnIndex(tf,x);    
                    root.getChildren().add(tf);
                }
            }

            Scene scene = new Scene(root, 500, 500);    
            primaryStage.setTitle("Random Binary Matrix (JavaFX)");
            primaryStage.setScene(scene);
            primaryStage.show();
        }

        public static void main(String[] args) {    
            launch(args);
        }    
    }