java - How to display a 10-by-10square matrix with JavaFX? -


javafx basics write program displays 10-by-10 square matrix. each element in matrix 0 or 1, randomly generated. display each number centered in text field. use textfield settext method set value 0 or 1 string. of can print 1 random number. how can make display 10-by-10 matrix?

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

you did of hard work , thing missed looking gridpaneapi. code add 40 buttons on top of each other because never change row or column of 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 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 index using 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);         }         } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -