![]() |
for loop variables
Hello. I have a need to create 81 buttons (named btn1...btn81).
Instead of typing out :
JButton btn1 = new JButton("0")is there a way I can use a for loop to do this :
for(int i=0; i<=81; i++)How can I do this? or do I just have to type them all out? Thank you |
Use an array of JButton.
Edit: Just read that wrong. If you _really must_ have them named btn1..btn81 instead of just accessing the correct index of the array then yes, you'll have to type them out. |
Ah no, thats nasty :( just a huge block of buttons.
also. I just i can't add them to the frame using a for loop either. Oh, this means I have to put some effort in. |
Why not just use an array?
|
I didn't know that could be done with buttons.
Also, misread andros message. I didn't see the part saying if it was a must to have them named btn1 to btn81. I have this now :
JButton[][]btn = new JButton[9][9];but when I run it I just get a blank frame. |
What is Pane? Variable? If so What kind? Are you updating the frame? I'm not used to working with Java frames and Panels but I'm guessing you have to make sure you update the frame somehow.
|
For starters, you need to add:
:
btn[r][c] = new JButton("0");Before you go and try to add them. |
Quote:
You just solved my problem :D |
Is a 2D array necessary for your JButtons? Why not just make a single JButton with a length of 81?
|
| All times are GMT -5. The time now is 2:20 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC