Results 1 to 8 of 8
Thread: JPanel "for" loops Help
- 09-17-2010, 11:45 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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...
Thanks!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();Last edited by javaman1; 09-17-2010 at 11:52 PM.
- 09-18-2010, 12:44 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
JPanel [] pane is assigned 64 JPanels but where is it actually declared?
- 09-18-2010, 01:06 AM #3
Did you try running this code?
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.Java Code:JPanel pane[counter] = new JPanel(); pane[counter].setBackground(aColor);
Here is some info on declaring arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
- 09-18-2010, 07:28 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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?
- 09-19-2010, 01:10 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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'
- 09-21-2010, 01:59 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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?
- 09-21-2010, 04:15 AM #7
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
- 09-21-2010, 10:33 PM #8
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
hey im new to java, and i need some help with "while loops"
By neilmx7 in forum New To JavaReplies: 16Last Post: 12-09-2008, 02:05 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
Using for loops to create a "bridge" made out of hyphens
By carlodelmundo in forum New To JavaReplies: 7Last Post: 09-21-2008, 11:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks