Results 1 to 5 of 5
Thread: Button Glitch
- 02-13-2010, 04:42 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 26
- Rep Power
- 0
Button Glitch
Hi. I'm fairly new to java, so my code may seem "immature", but I am hoping to get advice on a problem I am having.
As you can see, I add a button called null after the button array, only because the buttons will glitch otherwise. I was wondering if there was a way of just being able to add the buttons to the frame, without having to add one more button at the end and setting it to invisible.Java Code:package merp; import javax.swing.*; import java.awt.event.*; public class TicTacToe { int nSize = 100, nTurn = 0, nSquares[][] = new int[3][3], Player1 = 0, Player2 = 0, nTurnCount = 0, nDraw = 0; JButton Squares[][] = new JButton[3][3], Null = new JButton(); JFrame Main = new JFrame(); final ImageIcon X = new ImageIcon("X.png"), O = new ImageIcon("O.png"); int MiddleX[] = {1, 1, 1, 0, 1, 2, 1, 1}, MiddleY[] = {0, 1, 2, 1, 1, 1, 1, 1}; int LeftX[] = {0, 0, 0, 0, 1, 2, 0, 0}, LeftY[] = {0, 1, 2, 0, 0, 0, 0, 2}; int RightX[] = {2, 2, 2, 0, 1, 2, 2, 2}, RightY[] = {0, 1, 2, 2, 2, 2, 2, 0}; public TicTacToe(){ Main.setSize(nSize*3 + 16, nSize*3 + 38); Main.setLocationRelativeTo(null); for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ final int x, y; x = i; y = j; Squares[i][j] = new JButton(); Main.add(Squares[i][j]); nSquares[i][j] = 2; Squares[i][j].setBounds(nSize*i, nSize*j, nSize, nSize); Squares[i][j].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (nSquares[x][y] == 2) Turn(x, y); } }); } } Main.add(Null); Null.setVisible(false); Main.setVisible(true); Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void Turn(int x, int y){ if (nTurn<1){ nTurn++; Squares[x][y].setIcon(X); nSquares[x][y] = 0; } else { nTurn = 0; Squares[x][y].setIcon(O); nSquares[x][y] = 1; } nTurnCount++; if (nTurnCount > 4) CheckGO(); } public void CheckGO(){ for (int i = 0; i < 8; i++){ if (nSquares[MiddleX[i]][MiddleY[i]] != 2 && nSquares[LeftX[i]][LeftY[i]] == nSquares[MiddleX[i]][MiddleY[i]] && nSquares[RightX[i]][RightY[i]] == nSquares[MiddleX[i]][MiddleY[i]]){ NewGame(); if (nTurn < 1) Player1++; else Player2++; nTurnCount = 0; System.out.println("Player O: " + Player1 + " wins" + "\n" + "Player X: " + Player2 + " wins" + "\n" + "Draws: " + nDraw); break; } } if (nTurnCount == 9){ NewGame(); nDraw++; nTurnCount = 0; System.out.println("Player O: " + Player1 + " wins" + "\n" + "Player X: " + Player2 + " wins" + "\n" + "Draws: " + nDraw); } } public void NewGame(){ for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ nSquares[i][j] = 2; Squares[i][j].setIcon(null); } } } public static void main(String args[]){ new TicTacToe(); } }
I would also greatly appreciate constructive criticism of my code.
Thanks!
Jamie
- 02-13-2010, 05:06 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Start by looking at the section from the Swing tutorial on How to Use Buttons
The examples will show you how to better structure your class. Also look at the proper naming conventions for variable names.
-
I may be missing it, but I don't see you using layout managers anywhere such as a GridLayout(3, 3). If this were my app, and I were adding a 3x3 grid of buttons, I'd put my buttons into a JPanel that used the GridLayout(3, 3), and then would add that JPanel to my JFrame's contentPane BorderLayout.CENTER.
So in addition to camicrk's excellent suggestions above, I suggest that you have a look at the Sun Swing tutorial on laying out components in a container.
Much luck!
- 02-16-2010, 05:03 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 26
- Rep Power
- 0
Is this better?
Java Code:package merp; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TicTacToe { int nSize = 100, nTurn = 0, nSquares[][] = new int[3][3], Player1 = 0, Player2 = 0, nTurnCount = 0, nDraw = 0; JButton Squares[][] = new JButton[3][3]; JFrame frame = new JFrame("Tic Tac Toe"); JPanel Layout = new JPanel(); final ImageIcon X = new ImageIcon("X.png"), O = new ImageIcon("O.png"); int MiddleX[] = {1, 1, 1, 0, 1, 2, 1, 1}, MiddleY[] = {0, 1, 2, 1, 1, 1, 1, 1}; int LeftX[] = {0, 0, 0, 0, 1, 2, 0, 0}, LeftY[] = {0, 1, 2, 0, 0, 0, 0, 2}; int RightX[] = {2, 2, 2, 0, 1, 2, 2, 2}, RightY[] = {0, 1, 2, 2, 2, 2, 2, 0}; public TicTacToe(){ Layout.setLayout(new GridLayout(3, 3)); Layout.setPreferredSize(new Dimension(nSize*3, nSize*3)); for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ Squares[i][j] = new JButton(); final int x = i, y = j; Layout.add(Squares[i][j]); Squares[i][j].addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ if (nSquares[x][y] == 2) Turn(x, y); } }); } } frame.getContentPane().add(Layout); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setLocationRelativeTo(null); NewGame(); } public void Turn(int x, int y){ if (nTurn<1){ nTurn++; Squares[x][y].setIcon(X); nSquares[x][y] = 0; } else { nTurn = 0; Squares[x][y].setIcon(O); nSquares[x][y] = 1; } nTurnCount++; if (nTurnCount > 4) CheckGO(); } public void CheckGO(){ for (int i = 0; i < 8; i++){ if (nSquares[MiddleX[i]][MiddleY[i]] != 2 && nSquares[LeftX[i]][LeftY[i]] == nSquares[MiddleX[i]][MiddleY[i]] && nSquares[RightX[i]][RightY[i]] == nSquares[MiddleX[i]][MiddleY[i]]){ NewGame(); if (nTurn < 1) Player1++; else Player2++; nTurnCount = 0; System.out.println("Player O: " + Player1 + " wins" + "\n" + "Player X: " + Player2 + " wins" + "\n" + "Draws: " + nDraw); break; } } if (nTurnCount == 9){ NewGame(); nDraw++; nTurnCount = 0; System.out.println("Player O: " + Player1 + " wins" + "\n" + "Player X: " + Player2 + " wins" + "\n" + "Draws: " + nDraw); } } public void NewGame(){ for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ nSquares[i][j] = 2; Squares[i][j].setIcon(null); } } } public static void main(String args[]){ new TicTacToe(); } }
- 02-16-2010, 04:06 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Similar Threads
-
Button
By Tb0h in forum New To JavaReplies: 6Last Post: 07-22-2009, 01:28 AM -
Please help on Button action
By michaelxiao in forum New To JavaReplies: 4Last Post: 04-24-2009, 10:55 PM -
Back button
By Tokajac in forum Web FrameworksReplies: 1Last Post: 11-27-2008, 05:27 AM -
Using SWT Button
By Java Tip in forum Java TipReplies: 0Last Post: 01-08-2008, 09:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks