-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathButtonGrid.java
More file actions
33 lines (31 loc) · 1.51 KB
/
Copy pathButtonGrid.java
File metadata and controls
33 lines (31 loc) · 1.51 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
package fungrid;
import javax.swing.JFrame; //imports JFrame library
import javax.swing.JButton; //imports JButton library
import java.awt.GridLayout;
//import java.awt.color.*;
import java.awt.Color;//imports GridLayout library
//import static java.awt.Color.BLUE;
//import static javafx.scene.paint.Color.color;
class ButtonGrid {
Color[] c= {new Color(0xFF3333),new Color(0x7F00FF),new Color(0x009900),new Color(0xFFFFFF),new Color(0xFFFF33)};
JFrame frame=new JFrame(); //creates frame
JButton[][] grid; //names the grid of buttons
public ButtonGrid(int width, int length){ //constructor
frame.setLayout(new GridLayout(width,length)); //set layout
grid=new JButton[width][length];
//allocate the size of grid
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
int c1 = (int)(Math.random() * 5+0);
grid[x][y]=new JButton(" ");
grid[x][y].setBackground(c[c1]);
//creates new button
frame.add(grid[x][y]);
//frame.//adds button to grid
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
}