Results 1 to 8 of 8
  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default JPanel "for" loops Help

    So this is what i want to do. I want to create 64 JPanel "panes" with a for loop and set each to a different color. the output i want is a 8x8 screen of random colors. i don't know how to create 64 panes inside a for loop. any help? i used the '[]' because i saw it another program, but i have no idea what it means.

    i have this so far...

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    
    class Checkerboard2
    {
        public static void main(String [] args)
        {
            JFrame theGUI = new JFrame();
            theGUI.setTitle("Fourth GUI Program");
            theGUI.setSize(300, 200);
            theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            Random generator = new Random();
    
            int counter;
    
            for(counter = 0; counter <= 64; counter++)
            {
                 Color aColor = new Color(generator.nextInt(),generator.nextInt(),generator.nextInt());
    
                 JPanel pane[counter] = new JPanel();
                 pane[counter].setBackground(aColor);
            }
    
            Container pane = theGUI.getContentPane();
    Thanks!
    Last edited by javaman1; 09-18-2010 at 12:52 AM.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    JPanel [] pane is assigned 64 JPanels but where is it actually declared?

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Did you try running this code?
    Java Code:
                 JPanel pane[counter] = new JPanel();
                 pane[counter].setBackground(aColor);
    That will provide a compiler error. As al_Marshy said you need to declare the array using JPanel[] pane (as well as a new declaration) in the earlier part of the code. You can then assign elements to that array in your loop.

    Here is some info on declaring arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  4. #4
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Well I'm not that great at Java yet. i dont quite understand arrays. Could you explain how i could declare the "pane" object 64 times with a loop?

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    how to declare an array:
    int [] myIntArray=new int [64];

    JPanel [] pane =new JPanel[64];

    and do please read Zack's link so in future you are not 'coding in the dark'

  6. #6
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    thanks. i got the array to compile. however, how do i make the code set the background of each pane, the add the panes to the JFrame?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    You have already been given a link to a tutorial on the Oracle site. Don't you think you should scout around and see what other tutorials that site has to offer?

    Believe me, learning systematically is much more fun than having to ask a question about each and every step of your process.

    db

  8. #8
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    OK! Ill try and see if i can do it on my own! thanks everyone!

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  2. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  3. Replies: 16
    Last Post: 12-09-2008, 03:05 AM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Using for loops to create a "bridge" made out of hyphens
    By carlodelmundo in forum New To Java
    Replies: 7
    Last Post: 09-21-2008, 12:20 PM

Tags for this Thread

Posting Permissions

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