-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaFXGrid.java
More file actions
122 lines (97 loc) · 3.85 KB
/
JavaFXGrid.java
File metadata and controls
122 lines (97 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Programmer: Dan Hopp
Date: 05-MAR-2020
Description: Implement the following in one JavaFx application file (one
application/file, but make it create two stages):
Stage 1: Display a 10x10 grid of buttons with no labels. Buttons should
all be of same size, centered neatly in a grid pattern,
using a nested loop for the button placement.
Stage 2: Display a 10x10 grid of randomly-generated 0’s and 1’s. Each
number should be centered in a text field, using TextField’s
setText() method.
*/
package gridclick;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Hopp_lab5a extends Application{
@Override
public void start(Stage primaryStage){
//Show the first stage
primaryStage.setTitle("Stage 1");
//Populate with buttons
primaryStage.setScene(createGridOfButtons());
primaryStage.show();
//Set stage for Stage 2
Stage stage02 = new Stage();
//Show the second stage
stage02.setTitle("Stage 2");
//Populate with text fields with a random 1 or 0
stage02.setScene(createGridOfTextFields());
stage02.show();
}
//10x10 grid of buttons with no labels
private Scene createGridOfButtons(){
//Create a grid pane
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
//Create an array of buttons
Button[] btArray = new Button[100];
for (int i = 0; i < btArray.length; i++){
btArray[i] = new Button(" "); //Adding space to make the buttons more square
}
//Add some buttons
//go down the line of rows
int arrIndex = 0; //Button #
for (int row = 0; row < 10; row++){
//Populate the row with buttons
for (int col = 0; col < 10; col++){
gridPane.add(btArray[arrIndex], col, row);
arrIndex++;
}
}
//Make a scene to put in the main stage
Scene scene = new Scene(gridPane);
return scene;
}
//10x10 grid of randomly-generated 0’s and 1’s
private Scene createGridOfTextFields(){
//Create a grid pane
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
//Create an array of text fields
TextField[] tfArray = new TextField[100];
for (int i = 0; i < tfArray.length; i++){
tfArray[i] = new TextField();
//Set text field size to make the fields more square
tfArray[i].setPrefColumnCount(1);
}
//Add the text fields to the grid
//go down the line of rows
int i = 0; //Button #
for (int row = 0; row < 10; row++){
//Populate the row with buttons
for (int col = 0; col < 10; col++){
gridPane.add(tfArray[i], col, row);
//Populate with a 1 or 0
tfArray[i].setText(((int)(Math.random() * 2)) + "");
//Align text to center
tfArray[i].setAlignment(Pos.CENTER);
// //Set width to make it look a little better
// tfArray[i].setColumns();
i++;
}
}
//Make a scene to put it in the second stage
Scene scene = new Scene(gridPane);
return scene;
}
//Launch app
public static void main(String[] args){
Application.launch(args);
}
}