Crenox

GUI Grid Java

Mar 13th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. /*
  2. The buttons that the user interacts with need to be made, but
  3. since we don't know how many we need, they have to be named first.
  4. So right below the line where you create frame create the
  5. buttons: JButton[][] grid; The two sets of square brackets are
  6. there to say that the JButton's in the grid are kept in a
  7. two-dimensional format, if there were only one set of square
  8. brackets then it would simply be a line of JButton's, which
  9. still works, it's just easier to reference which button is being
  10. created or interacted with when it's two-dimensional.
  11.  
  12. The JButton's have been named, but we still have to say how many
  13. buttons there are. You need to add a line of code in the
  14. constructor that sets the amount: grid=new JButton[width][length];
  15.  
  16. Now that it's been determined that there will be a certain number
  17. of buttons, each must be created. The easiest way to do this is
  18. with two for loops, one for the x-axis, one for the y-axis. Inside
  19. the two loops we make a new button, and for ease of reference the
  20. example puts text inside each button so we know which button in
  21. the two-dimensional array is where. To create a button, inside
  22. the loop you need to put grid[x][y] = new JButton ("("+x+","+y+")");
  23. */
  24.  
  25. /*
  26. LINKS:
  27. http://www.wikihow.com/Make-a-GUI-Grid-in-Java
  28. https://stackoverflow.com/questions/144892/how-to-centre-a-window-in-java
  29. */
  30.  
  31. package com.samkough.main;
  32.  
  33. import javax.swing.JFrame;
  34. import javax.swing.JButton;
  35. import java.awt.GridLayout;
  36.  
  37. public class Grid
  38. {
  39. JFrame frame = new JFrame(); // creates frame
  40. JButton[][] grid; // names the grid of buttons
  41.  
  42. public Grid(int width, int length)
  43. {
  44. // Sets the layout of frame
  45. frame.setLayout(new GridLayout(width, length));
  46. // Makes the frame close when you hit the 'x'
  47. // button in the top right hand corner
  48. grid = new JButton[width][length]; // allocate the size of grid
  49. for (int y = 0; y < length; y++)
  50. {
  51. for(int x = 0; x < width; x++)
  52. {
  53. grid[x][y] = new JButton("("+x+", "+y+")");
  54. frame.add(grid[x][y]); // adds button to grid
  55. }
  56. }
  57. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58. // Sets size of frame
  59. frame.setSize(800, 800);
  60. // Sets the frame visible
  61. frame.setVisible(true);
  62. frame.setLocationRelativeTo(null);
  63. frame.setTitle("GUI Grid");
  64.  
  65. for (int y = 0; y < length; y++)
  66. {
  67. for (int x = 0; x < width; x++)
  68. {
  69. grid[x][y] = new JButton("("+x+", "+y+")");
  70. frame.add(grid[x][y]); // adds button to grid
  71. }
  72. }
  73. }
  74.  
  75. public static void main(String args[])
  76. {
  77. new Grid(3,3); //makes new Grid with 2 parameters
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment