Results 1 to 3 of 3
Thread: JFrame: JButtons in a grid
- 06-09-2010, 07:13 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
JFrame: JButtons in a grid
Hi, I am trying to make a checkers game using a JFrame. So far I was able to display each space on the checker board as a JButton. But when I try to remove all the buttons from the grid and put new ones in by using the removeAll method it only displays a transparent JFrame.
Here is my code:
Java Code:public class BoardOperations { public static void initializeBoard(JFrame checkerBoard, ImageIcon blkBackground, ImageIcon redBackground, ImageIcon whiteChkr, ImageIcon redChkr, boolean chkrBlockWhite, boolean chkrBlockRed) { checkerBoard.setSize(1000,1000); checkerBoard.setTitle("CheckerBoard"); checkerBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); int row = 8; int col = 8; Container pane = checkerBoard.getContentPane(); pane.setLayout(new GridLayout(row,col, 1, 1)); int count = 0; //Populates the board with spaces and checkers for (int x = 1; x <=(row*col); x++) { int altr = 0; altr = (x-1) % col; altr += (x-1) / col; //Inserts red spaces in checker board if (altr % 2 == 0) { pane.add(new JButton(redBackground)); } //Inserts black spaces or checkers in checker board else { //Inserts white checkers if(chkrBlockWhite == true && count < 12) pane.add(new JButton(whiteChkr)); //Inserts red checkers else if(chkrBlockRed == true && count > 19) pane.add(new JButton(redChkr)); //Inserts black spaces else pane.add(new JButton(blkBackground)); count++; } } checkerBoard.setVisible(true); } public static void movePiece(JFrame checkerBoard, ImageIcon blkBackground, ImageIcon redBackground, ImageIcon whiteChkr, ImageIcon redChkr, boolean chkrBlockWhite, boolean chkrBlockRed) { checkerBoard.removeAll(); Container pane = checkerBoard.getContentPane(); pane.setLayout(new GridLayout(8,8, 1, 1)); for (int x = 1; x <=(8*8); x++) { int altr = 0; altr = (x-1) % 8; altr += (x-1) / 8; //Inserts red spaces in checker board if (altr % 2 == 0) { pane.add(new JButton(redBackground)); } //Inserts black spaces or checkers in checker board else { //Inserts white checkers if(chkrBlockWhite == true) pane.add(new JButton(whiteChkr)); //Inserts red checkers else if(chkrBlockRed == true) pane.add(new JButton(redChkr)); //Inserts black spaces else{ pane.add(new JButton(blkBackground)); } } } } }
- 06-09-2010, 07:53 PM #2
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
Never mind I figured out my problem, I was using removeAll() on the JFrame when I should have been using it on the Container.
- 06-09-2010, 07:56 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
How do you draw a grid in a JFrame
By Singing Boyo in forum New To JavaReplies: 2Last Post: 03-11-2009, 02:31 AM -
A Simple way to use JButtons and place objects in a JFrame
By Singing Boyo in forum Java TipReplies: 0Last Post: 03-10-2009, 09:55 AM -
[SOLVED] Problems with JButtons and JFrame.EXIT_ON_CLOSE. Please Help!!!
By Singing Boyo in forum New To JavaReplies: 5Last Post: 03-09-2009, 03:09 AM -
Help with JButtons...
By ashton in forum New To JavaReplies: 8Last Post: 01-26-2009, 09:38 AM -
JButtons
By fgasimzade in forum SWT / JFaceReplies: 1Last Post: 12-25-2007, 05:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks