Results 1 to 3 of 3
- 02-27-2012, 02:54 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
How can I make an array of JButtons?
Hey everyone!
I have a quick question. I'm designing a 'Memory Game' and I have one class called Board that creates the cards for the game and holds the logic of the game. Here's what I have so far:
Basically what I need to do is to store the above 24 buttons into an array, because I will be using an actionPerformed(ActionEvent e) method on all of them. I hope that makes sense :pJava Code:public class Board extends JPanel implements ActionListener{ private int buttonsVisible; JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16, button17, button18, button19, button20, button21, button22, button23, button24; public Board() { this.setLayout(new GridLayout (4, 6, 0, 0)); button1 = new JButton(newIcon); this.add(button1); button2 = new JButton(newIcon); this.add(button2); button3 = new JButton(newIcon); this.add(button3); button4 = new JButton(newIcon); this.add(button4); button5 = new JButton(newIcon); this.add(button5); button6 = new JButton(newIcon); this.add(button6); button7 = new JButton(newIcon); this.add(button7); button8 = new JButton(newIcon); this.add(button8); button9 = new JButton(newIcon); this.add(button9); button10 = new JButton(newIcon); this.add(button10); button11 = new JButton(newIcon); this.add(button11); button12 = new JButton(newIcon); this.add(button12); button13 = new JButton(newIcon); this.add(button13); button14 = new JButton(newIcon); this.add(button14); button15 = new JButton(newIcon); this.add(button15); button16 = new JButton(newIcon); this.add(button16); button17 = new JButton(newIcon); this.add(button17); button18 = new JButton(newIcon); this.add(button18); button19 = new JButton(newIcon); this.add(button19); button20 = new JButton(newIcon); this.add(button20); button21 = new JButton(newIcon); this.add(button21); button22 = new JButton(newIcon); this.add(button22); button23 = new JButton(newIcon); this.add(button23); button24 = new JButton(newIcon); this.add(button24); } }
Is there a way I can do this? I tried using a for loop, but whenever I compile and run it, the cards don't seem to show up. Also, I have another class that creates the main game panel and implements the Board() constructor.
-
Re: How can I make an array of JButtons?
Yes, you not only can do this but should create a button array. Please show us your best attempt at doing this and any error messages, exceptions or misbehavior so we can better help you.
- 02-27-2012, 03:51 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
How to make JButtons as big as possible and rezise when resizing JFrame?
By equal in forum AWT / SwingReplies: 4Last Post: 05-29-2011, 09:45 PM -
Keeping Only The JButtons In a Selected Row Active In A 2D Array
By java999 in forum Advanced JavaReplies: 8Last Post: 04-22-2011, 01:55 PM -
Creating array of JButtons
By tabchas in forum New To JavaReplies: 20Last Post: 04-16-2011, 05:36 AM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
2D Array of JButtons
By stevemcc in forum AWT / SwingReplies: 1Last Post: 02-16-2008, 11:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks