Results 1 to 3 of 3
  1. #1
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default 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:

    Java 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);
        
       
        }
    }
    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 :p
    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    LuxoJr is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 05-29-2011, 10:45 PM
  2. Replies: 8
    Last Post: 04-22-2011, 02:55 PM
  3. Creating array of JButtons
    By tabchas in forum New To Java
    Replies: 20
    Last Post: 04-16-2011, 06:36 AM
  4. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  5. 2D Array of JButtons
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 12:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •