3 by 3 grid of buttons

Reply

Join Date: Feb 2008
Posts: 29
Reputation: Hockeyfreak889 is an unknown quantity at this point 
Solved Threads: 0
Hockeyfreak889 Hockeyfreak889 is offline Offline
Light Poster

3 by 3 grid of buttons

 
0
  #1
Jun 14th, 2009
ok so have been assigned to make a 2 player tic tac toe game for my final project this year in school. However since this is a fisrt year java course we are using like "fake java" just to get the hang of it. Except now i have to work on the program from home cause i have been sick and the java compiler at my house doesnt understand the "fake java". so basically my question is how do u display a 3 by 3 grid of buttons? because at the moment i have a line of nine buttons that actually allow you to play the game correctly if you take the 4,5, and 6 buttons and make them the second row and the 7,8, and 9 and make it the thrid row, but i cant figure out how to do that
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: 3 by 3 grid of buttons

 
0
  #2
Jun 14th, 2009
What do you mean by "fake Java"? Do you mean Processing? if that is the case than you can simply download it as it is free.

If it is not processing, and you can't go back to what your used to, than please post your code.

If I had to give you one suggestion on how to layout the buttons in a grid, you might want to look into the GridLayout, which should be what your looking for.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 29
Reputation: Hockeyfreak889 is an unknown quantity at this point 
Solved Threads: 0
Hockeyfreak889 Hockeyfreak889 is offline Offline
Light Poster

Re: 3 by 3 grid of buttons

 
0
  #3
Jun 14th, 2009
ya i dont think its processing and i cant go back to what i am used to. The code i have i copied and pasted from an old progam to get the basic set up so thats y i have a lot of un used variable but here is what i have:

  1. public class client extends JPanel {
  2. private final static long serialVersionUID = 2000L;
  3. Server g1 = new Server();
  4. BufferedImage image1, image2, image3, image4, image5, image6, image7, image8,image9;
  5. private JLabel playerLabel, dealerLabel;
  6. private int[][] board = new int[3][3];
  7. private String message = "";
  8. private int dealing = 1, dealerHandValue = 0, playerHandValue = 0;
  9. JButton oneButton, twoButton, threeButton, fourButton, fiveButton, sixButton, sevenButton, eightButton, nineButton;
  10. JPanel labelDisplayArea = new JPanel ();
  11.  
  12. public client() {
  13. oneButton = new JButton(" ");
  14. twoButton = new JButton(" ");
  15. threeButton = new JButton(" ");
  16. fourButton = new JButton(" ");
  17. fiveButton = new JButton(" ");
  18. sixButton = new JButton(" ");
  19. sevenButton = new JButton(" ");
  20. eightButton = new JButton(" ");
  21. nineButton = new JButton(" ");
  22. for(int x=0;x<3;x++){
  23. for(int j=0;j<3;j++){
  24. board[x][j]=0;
  25. }
  26. }
  27. oneButton.addActionListener (new ActionListener () {
  28. public void actionPerformed (ActionEvent ae) {
  29. oneButton.setText(g1.getPlayer());
  30. oneButton.setEnabled(false);
  31. board[0][0] = g1.getBoard();
  32. message = g1.checkWin(board);
  33. if(message.equals("")){
  34.  
  35. }else{
  36. JOptionPane.showMessageDialog(null, message);
  37. }
  38. }
  39. });
  40.  
  41. twoButton.addActionListener (new ActionListener () {
  42. public void actionPerformed (ActionEvent ae) {
  43. twoButton.setText(g1.getPlayer());
  44. twoButton.setEnabled(false);
  45. board[0][1] = g1.getBoard();
  46. message = g1.checkWin(board);
  47. if(message.equals("")){
  48.  
  49. }else{
  50. JOptionPane.showMessageDialog(null, message);
  51. }
  52. }
  53. });
  54. threeButton.addActionListener (new ActionListener () {
  55. public void actionPerformed (ActionEvent ae) {
  56. threeButton.setText(g1.getPlayer());
  57. threeButton.setEnabled(false);
  58. board[0][2] = g1.getBoard();
  59. message = g1.checkWin(board);
  60. if(message.equals("")){
  61.  
  62. }else{
  63. JOptionPane.showMessageDialog(null, message);
  64. }
  65. }
  66. });
  67. fourButton.addActionListener (new ActionListener () {
  68. public void actionPerformed (ActionEvent ae) {
  69. fourButton.setText(g1.getPlayer());
  70. fourButton.setEnabled(false);
  71. board[1][0] = g1.getBoard();
  72. message = g1.checkWin(board);
  73. if(message.equals("")){
  74.  
  75. }else{
  76. JOptionPane.showMessageDialog(null, message);
  77. }
  78. }
  79. });
  80.  
  81. fiveButton.addActionListener (new ActionListener () {
  82. public void actionPerformed (ActionEvent ae) {
  83. fiveButton.setText(g1.getPlayer());
  84. fiveButton.setEnabled(false);
  85. board[1][1] = g1.getBoard();
  86. message = g1.checkWin(board);
  87. if(message.equals("")){
  88.  
  89. }else{
  90. JOptionPane.showMessageDialog(null, message);
  91. }
  92. }
  93. });
  94. sixButton.addActionListener (new ActionListener () {
  95. public void actionPerformed (ActionEvent ae) {
  96. sixButton.setText(g1.getPlayer());
  97. sixButton.setEnabled(false);
  98. board[1][2] = g1.getBoard();
  99. message = g1.checkWin(board);
  100. if(message.equals("")){
  101.  
  102. }else{
  103. JOptionPane.showMessageDialog(null, message);
  104. }
  105. }
  106. });
  107. sevenButton.addActionListener (new ActionListener () {
  108. public void actionPerformed (ActionEvent ae) {
  109. sevenButton.setText(g1.getPlayer());
  110. sevenButton.setEnabled(false);
  111. board[2][0] = g1.getBoard();
  112. message = g1.checkWin(board);
  113. if(message.equals("")){
  114.  
  115. }else{
  116. JOptionPane.showMessageDialog(null, message);
  117. }
  118. }
  119. });
  120.  
  121. eightButton.addActionListener (new ActionListener () {
  122. public void actionPerformed (ActionEvent ae) {
  123. eightButton.setText(g1.getPlayer());
  124. eightButton.setEnabled(false);
  125. board[2][1] = g1.getBoard();
  126. message = g1.checkWin(board);
  127. if(message.equals("")){
  128.  
  129. }else{
  130. JOptionPane.showMessageDialog(null, message);
  131. }
  132. }
  133. });
  134. nineButton.addActionListener (new ActionListener () {
  135. public void actionPerformed (ActionEvent ae) {
  136. nineButton.setText(g1.getPlayer());
  137. nineButton.setEnabled(false);
  138. board[2][2] = g1.getBoard();
  139. message = g1.checkWin(board);
  140. if(message.equals("")){
  141.  
  142. }else{
  143. JOptionPane.showMessageDialog(null, message);
  144. }
  145. }
  146. });
  147.  
  148.  
  149. JPanel buttonPanel = new JPanel();
  150. buttonPanel.add(oneButton);
  151. buttonPanel.add(twoButton);
  152. buttonPanel.add(threeButton);
  153. buttonPanel.add(fourButton);
  154. buttonPanel.add(fiveButton);
  155. buttonPanel.add(sixButton);
  156. buttonPanel.add(sevenButton);
  157. buttonPanel.add(eightButton);
  158. buttonPanel.add(nineButton);
  159.  
  160.  
  161.  
  162.  
  163. add(buttonPanel, BorderLayout.PAGE_START);
  164.  
  165. add (buttonPanel, BorderLayout.CENTER);
  166.  
  167. setSize(700,700);
  168.  
  169.  
  170. }
  171.  
  172. /**
  173.   * Creates and displays the GUI
  174.   */
  175. private static void createAndShowGUI() {
  176. JFrame frame = new JFrame("Tick Tack Toe");
  177. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  178. frame.getContentPane().add( new client());
  179. frame.pack();
  180. frame.setVisible(true);
  181. frame.setSize(500,200);
  182. }
  183. public static void main(String[] args) {
  184. SwingUtilities.invokeLater(new Runnable() {
  185. public void run() {
  186. UIManager.put("swing.boldMetal", Boolean.FALSE);
  187. createAndShowGUI();
  188. }
  189. });
  190. }
  191. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: 3 by 3 grid of buttons

 
0
  #4
Jun 15th, 2009
Without spending too much time looking at this because I need sleep, it looks like its Java code at first glance. The JButton and Jpanel etc are part of a part of java called Swing. What version of Java do you have? You might want to update.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 29
Reputation: Hockeyfreak889 is an unknown quantity at this point 
Solved Threads: 0
Hockeyfreak889 Hockeyfreak889 is offline Offline
Light Poster

Re: 3 by 3 grid of buttons

 
0
  #5
Jun 15th, 2009
ok ya i kno that is java swing i have been teaching myself it recently ... and for school we have a library that basically does all the formatting and what not for us so its still java just simplier. And i actually figured out how to do it but it wasnt very efficient. I used a GridBagLayout which worked as a mentioned just tediuos. my friend mentions a boxLayout with three panels, one for each row but i didnt try it as i already had done it this way. But heres wat i got:
  1. public class client extends JPanel {
  2. private final static long serialVersionUID = 2000L;
  3. Server g1 = new Server();
  4. private int[][] board = new int[3][3];
  5. private String message = "";
  6. JButton oneButton, twoButton, threeButton, fourButton, fiveButton, sixButton, sevenButton, eightButton, nineButton, endButton;
  7. JPanel buttonPanel = new JPanel();
  8. final static boolean shouldFill = true;
  9. final static boolean shouldWeightX = true;
  10. final static boolean RIGHT_TO_LEFT = false;
  11. //reset the button so that they are enabled and have no text
  12. public void reset(){
  13. oneButton.setText("");
  14. twoButton.setText("");
  15. threeButton.setText("");
  16. fourButton.setText("");
  17. fiveButton.setText("");
  18. sixButton.setText("");
  19. sevenButton.setText("");
  20. eightButton.setText("");
  21. nineButton.setText("");
  22. oneButton.setEnabled(true);
  23. twoButton.setEnabled(true);
  24. threeButton.setEnabled(true);
  25. fourButton.setEnabled(true);
  26. fiveButton.setEnabled(true);
  27. sixButton.setEnabled(true);
  28. sevenButton.setEnabled(true);
  29. eightButton.setEnabled(true);
  30. nineButton.setEnabled(true);
  31. message = "";
  32. for(int x=0;x<3;x++){
  33. for(int j=0;j<3;j++){
  34. board[x][j]=0;
  35. }
  36. }
  37. }
  38. //generate grid
  39. public client() {
  40. if (RIGHT_TO_LEFT) {
  41. buttonPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  42. }
  43. buttonPanel.setLayout(new GridBagLayout());
  44. GridBagConstraints c = new GridBagConstraints();
  45. if (shouldFill) {
  46. //natural height, maximum width
  47. c.fill = GridBagConstraints.HORIZONTAL;
  48. }
  49. oneButton = new JButton("");
  50. if (shouldWeightX) {
  51. c.weightx = 4;
  52. }
  53. c.fill = GridBagConstraints.HORIZONTAL;
  54. c.ipady = 50; //height
  55. c.gridx = 0; //grid location
  56. c.gridy = 0;//grid location
  57. buttonPanel.add(oneButton,c); //add to panel
  58. twoButton = new JButton("");
  59. c.fill = GridBagConstraints.HORIZONTAL;
  60. c.gridx = 1;
  61. c.gridy = 0;
  62. buttonPanel.add(twoButton,c);
  63.  
  64. threeButton = new JButton("");
  65. c.fill = GridBagConstraints.HORIZONTAL;
  66. c.gridx = 2;
  67. c.gridy = 0;
  68. buttonPanel.add(threeButton,c);
  69.  
  70. fourButton = new JButton("");
  71. c.fill = GridBagConstraints.HORIZONTAL;
  72. c.gridx = 0;
  73. c.gridy = 1;
  74. buttonPanel.add(fourButton,c);
  75.  
  76. fiveButton = new JButton("");
  77. c.fill = GridBagConstraints.HORIZONTAL;
  78. c.gridx = 1;
  79. c.gridy = 1;
  80. buttonPanel.add(fiveButton,c);
  81.  
  82. sixButton = new JButton("");
  83. c.fill = GridBagConstraints.HORIZONTAL;
  84. c.gridx = 2;
  85. c.gridy = 1;
  86. buttonPanel.add(sixButton,c);
  87.  
  88. sevenButton = new JButton("");
  89. c.fill = GridBagConstraints.HORIZONTAL;
  90. c.gridx = 0;
  91. c.gridy = 2;
  92. buttonPanel.add(sevenButton,c);
  93.  
  94. eightButton = new JButton("");
  95. c.fill = GridBagConstraints.HORIZONTAL;
  96. c.gridx = 1;
  97. c.gridy = 2;
  98. buttonPanel.add(eightButton,c);
  99.  
  100. nineButton = new JButton("");
  101. c.gridx = 2;
  102. c.gridy = 2;
  103. buttonPanel.add(nineButton,c);
  104. endButton = new JButton("End the game");
  105. c.fill = GridBagConstraints.HORIZONTAL;
  106. c.weighty = 1.0; //request any extra vertical space
  107. c.anchor = GridBagConstraints.PAGE_END; //bottom of space
  108. c.insets = new Insets(10,0,0,0); //top padding
  109. c.gridwidth = 3;
  110. c.ipady = 0;
  111. c.gridx = 0;
  112. c.gridy = 3;
  113. buttonPanel.add(endButton,c);
  114. //set the board array to all 0s
  115. for(int x=0;x<3;x++){
  116. for(int j=0;j<3;j++){
  117. board[x][j]=0;
  118. }
  119. }
  120. //if button 1 is clicked
  121. oneButton.addActionListener (new ActionListener () {
  122. public void actionPerformed (ActionEvent ae) {
  123.  
  124. oneButton.setText(g1.getPlayer()); //set the text of the button
  125. oneButton.setEnabled(false); // disable button
  126. board[0][0] = g1.getBoard(); //set the array to the specific player
  127. message = g1.checkWin(board); // check if someone won
  128. if(message.equals("")){
  129. }else{
  130. JOptionPane.showMessageDialog(null, message); //output if someone won or tied
  131. reset(); //reset
  132. g1.reset();
  133. }
  134. }
  135. });
  136. //if click two button
  137. twoButton.addActionListener (new ActionListener () {
  138. public void actionPerformed (ActionEvent ae) {
  139. twoButton.setText(g1.getPlayer());
  140. twoButton.setEnabled(false);
  141. board[0][1] = g1.getBoard();
  142. message = g1.checkWin(board);
  143. if(message.equals("")){
  144. }else{
  145. JOptionPane.showMessageDialog(null, message);
  146. reset();
  147. g1.reset();
  148. }
  149. }
  150. });
  151. //if click three button
  152. threeButton.addActionListener (new ActionListener () {
  153. public void actionPerformed (ActionEvent ae) {
  154. threeButton.setText(g1.getPlayer());
  155. threeButton.setEnabled(false);
  156. board[0][2] = g1.getBoard();
  157. message = g1.checkWin(board);
  158. if(message.equals("")){
  159. }else{
  160. JOptionPane.showMessageDialog(null, message);
  161. reset();
  162. g1.reset();
  163. }
  164. }
  165. });
  166. //if click four button
  167. fourButton.addActionListener (new ActionListener () {
  168. public void actionPerformed (ActionEvent ae) {
  169. fourButton.setText(g1.getPlayer());
  170. fourButton.setEnabled(false);
  171. board[1][0] = g1.getBoard();
  172. message = g1.checkWin(board);
  173. if(message.equals("")){
  174. }else{
  175. JOptionPane.showMessageDialog(null, message);
  176. reset();
  177. g1.reset();
  178. }
  179. }
  180. });
  181. //if click five button
  182. fiveButton.addActionListener (new ActionListener () {
  183. public void actionPerformed (ActionEvent ae) {
  184. fiveButton.setText(g1.getPlayer());
  185. fiveButton.setEnabled(false);
  186. board[1][1] = g1.getBoard();
  187. message = g1.checkWin(board);
  188. if(message.equals("")){
  189. }else{
  190. JOptionPane.showMessageDialog(null, message);
  191. reset();
  192. g1.reset();
  193. }
  194. }
  195. });
  196. //if click six button
  197. sixButton.addActionListener (new ActionListener () {
  198. public void actionPerformed (ActionEvent ae) {
  199. sixButton.setText(g1.getPlayer());
  200. sixButton.setEnabled(false);
  201. board[1][2] = g1.getBoard();
  202. message = g1.checkWin(board);
  203. if(message.equals("")){
  204. }else{
  205. JOptionPane.showMessageDialog(null, message);
  206. reset();
  207. g1.reset();
  208. }
  209. }
  210. });
  211. //if click seven button
  212. sevenButton.addActionListener (new ActionListener () {
  213. public void actionPerformed (ActionEvent ae) {
  214. sevenButton.setText(g1.getPlayer());
  215. sevenButton.setEnabled(false);
  216. board[2][0] = g1.getBoard();
  217. message = g1.checkWin(board);
  218. if(message.equals("")){
  219. }else{
  220. JOptionPane.showMessageDialog(null, message);
  221. reset();
  222. g1.reset();
  223. }
  224. }
  225. });
  226. //if click eight button
  227. eightButton.addActionListener (new ActionListener () {
  228. public void actionPerformed (ActionEvent ae) {
  229. eightButton.setText(g1.getPlayer());
  230. eightButton.setEnabled(false);
  231. board[2][1] = g1.getBoard();
  232. message = g1.checkWin(board);
  233. if(message.equals("")){
  234. }else{
  235. JOptionPane.showMessageDialog(null, message);
  236. reset();
  237. g1.reset();
  238. }
  239. }
  240. });
  241. //if click nine button
  242. nineButton.addActionListener (new ActionListener () {
  243. public void actionPerformed (ActionEvent ae) {
  244. nineButton.setText(g1.getPlayer());
  245. nineButton.setEnabled(false);
  246. board[2][2] = g1.getBoard();
  247. message = g1.checkWin(board);
  248. if(message.equals("")){
  249. }else{
  250. JOptionPane.showMessageDialog(null, message);
  251. reset();
  252. g1.reset();
  253. }
  254. }
  255. });
  256. //if click end button
  257. endButton.addActionListener (new ActionListener () {
  258. public void actionPerformed (ActionEvent ae) {
  259. JOptionPane.showMessageDialog(null, "You have ended the game. Goodbye");
  260. System.exit(0);//end
  261. }
  262. });
  263.  
  264. add(buttonPanel, BorderLayout.PAGE_START);
  265. add (buttonPanel, BorderLayout.CENTER);
  266. setSize(700,700);
  267. }
  268.  
  269. /**
  270.   * Creates and displays the GUI
  271.   */
  272. private static void createAndShowGUI() {
  273. JFrame frame = new JFrame("Tick Tack Toe");
  274. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  275. frame.getContentPane().add( new client());
  276. frame.pack();
  277. frame.setVisible(true);
  278. frame.setSize(150,310);
  279. }
  280. public static void main(String[] args) {
  281. SwingUtilities.invokeLater(new Runnable() {
  282. public void run() {
  283. UIManager.put("swing.boldMetal", Boolean.FALSE);
  284. createAndShowGUI();
  285. }
  286. });
  287. }
  288. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,568
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: 3 by 3 grid of buttons

 
1
  #6
Jun 15th, 2009
Use a GridLayout. If you have 3x3 grid, that's 3 rows and 3 columns. So Gridlayout whatever = new GridLayout (3,3). Problem hopefully solved. And..

Good luck.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC