Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.wit.cs.comp1050;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- public class Lab6P2 extends Application
- {
- @Override
- public void start(Stage primaryStage)
- {
- // declare variable gridLength for length of matrix
- // value of 10
- int gridLength = 10;
- // declare variable gridWidth for width of matrix
- // value of 10
- int gridWidth = 10;
- // declare GridPane grid to hold the matrix
- GridPane grid = new GridPane();
- // cycles until the gridLength value is reached
- for (int i = 0; i < gridLength; i++)
- {
- // cycles until the gridWidth value is reached
- for (int j = 0; j < gridWidth; j++)
- {
- // declare variable displayNum so that each number in the matrix is 0 or 1
- int displayNum = (int) Math.round(Math.random());
- // declare TextField textField to display the 0s and 1s
- TextField textField = new TextField();
- // set the size of each textField to be a 50x50 square
- textField.setPrefSize(50, 50);
- // set the textField so that the value cannot be changed
- // when the application is launched
- textField.setEditable(false);
- // set the alignment of the text in textField to be in the center
- textField.setAlignment(Pos.CENTER);
- // set the text displayed in each textField to be a 0 or 1
- textField.setText("" + displayNum);
- // create an individual horizontal textField for each value of i
- GridPane.setRowIndex(textField, i);
- // create an individual vertical textField for each value of j
- GridPane.setColumnIndex(textField, j);
- // call on each individual textField to display inside of the grid
- grid.getChildren().add(textField);
- }
- }
- // create a variable Scene scene to set the initial display of the application
- Scene scene = new Scene(grid, 500, 500);
- // set the title of the application
- primaryStage.setTitle("Lab6P2");
- // set the display of the application
- primaryStage.setScene(scene);
- // set the application to be seen
- primaryStage.show();
- }
- /**
- * The main method is only needed for the IDE with limited JavaFX support.
- * Not needed for running from the command line.
- */
- public static void main(String[] args)
- {
- // launch the application
- Application.launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement