Advertisement
Guest User

Lab6P2

a guest
Feb 14th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. package edu.wit.cs.comp1050;
  2. import javafx.application.Application;
  3. import javafx.geometry.Pos;
  4. import javafx.stage.Stage;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.TextField;
  7. import javafx.scene.layout.GridPane;
  8. public class Lab6P2 extends Application
  9. {
  10. @Override
  11. public void start(Stage primaryStage)
  12. {
  13. // declare variable gridLength for length of matrix
  14. // value of 10
  15. int gridLength = 10;
  16. // declare variable gridWidth for width of matrix
  17. // value of 10
  18. int gridWidth = 10;
  19. // declare GridPane grid to hold the matrix
  20. GridPane grid = new GridPane();
  21.  
  22. // cycles until the gridLength value is reached
  23. for (int i = 0; i < gridLength; i++)
  24. {
  25. // cycles until the gridWidth value is reached
  26. for (int j = 0; j < gridWidth; j++)
  27. {
  28. // declare variable displayNum so that each number in the matrix is 0 or 1
  29. int displayNum = (int) Math.round(Math.random());
  30. // declare TextField textField to display the 0s and 1s
  31. TextField textField = new TextField();
  32. // set the size of each textField to be a 50x50 square
  33. textField.setPrefSize(50, 50);
  34. // set the textField so that the value cannot be changed
  35. // when the application is launched
  36. textField.setEditable(false);
  37. // set the alignment of the text in textField to be in the center
  38. textField.setAlignment(Pos.CENTER);
  39. // set the text displayed in each textField to be a 0 or 1
  40. textField.setText("" + displayNum);
  41.  
  42. // create an individual horizontal textField for each value of i
  43. GridPane.setRowIndex(textField, i);
  44. // create an individual vertical textField for each value of j
  45. GridPane.setColumnIndex(textField, j);
  46. // call on each individual textField to display inside of the grid
  47. grid.getChildren().add(textField);
  48.  
  49. }
  50. }
  51.  
  52. // create a variable Scene scene to set the initial display of the application
  53. Scene scene = new Scene(grid, 500, 500);
  54. // set the title of the application
  55. primaryStage.setTitle("Lab6P2");
  56. // set the display of the application
  57. primaryStage.setScene(scene);
  58. // set the application to be seen
  59. primaryStage.show();
  60.  
  61. }
  62.  
  63. /**
  64. * The main method is only needed for the IDE with limited JavaFX support.
  65. * Not needed for running from the command line.
  66. */
  67. public static void main(String[] args)
  68. {
  69. // launch the application
  70. Application.launch(args);
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement