![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
for loop variables
Hello. I have a need to create 81 buttons (named btn1...btn81).
Instead of typing out JButton btn1 = new JButton("0")
JButton btn2 = new JButton("0")
...
JButton btn81 = new JButton("0")is there a way I can use a for loop to do this for(int i=0; i<=81; i++)
{
JButton btni = new JButton("0")
}How can I do this? or do I just have to type them all out? Thank you |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Why not just use an array?
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
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];
for(int r=0;r<9; r++)
{
for(int c=0;c<9; c++)
{
btn[r][c].setSize(25,25);
btn[r][c].setText("0");
pane.add(btn[r][c]);
}
}but when I run it I just get a blank frame. |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
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.
|
|
|
|
|
|
#7 |
|
Professional Programmer
|
For starters, you need to add:
btn[r][c] = new JButton("0");Before you go and try to add them. |
|
|
|
|
|
#8 | |
|
Programmer
Join Date: Jul 2004
Location: Hampshire
Posts: 56
Rep Power: 5
![]() |
Quote:
You just solved my problem ![]() |
|
|
|
|
|
|
#9 |
|
Sexy Programmer
|
Is a 2D array necessary for your JButtons? Why not just make a single JButton with a length of 81?
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Storing Variables with Multiple Dialogs/Forms | PhilBon | Visual Basic .NET | 6 | Oct 11th, 2006 7:14 AM |
| [Efficiency] Variables vs. Calculations | kurt | C | 7 | Dec 29th, 2005 2:39 PM |
| Assignment of numbers to variables without asking | Haz | C# | 26 | May 23rd, 2005 10:30 AM |
| Is it possible for me using session variables into a class? | see07 | C# | 1 | Mar 9th, 2005 5:32 PM |
| global variables | uman | C++ | 2 | Feb 20th, 2005 10:39 PM |