简体   繁体   中英

How do I add JButtons dynamically in Netbeans?

I am creating a simple game where i want a grid type structure of JButtons. I want their size to be increasing as the level of the game, but I am unable to find the solution. Is it possible with just drag and drop?

You need to put your buttons in a matrix and create buttons as your program runs. Since you haven't supplied many details, I would suggest an array list of array lists, so that you can (theoretically) add as many buttons as you wish.

Since you haven't provided a very good description of your requirements, I can only say that you'd create buttons and assign them to the appropriate array list, referring to each by its coordinate.

// assuming you want a reference to the button at x, y coords
// also assuming that your "matrix" is a column array list holding row array lists

myButton = columnList.get(y).get(x);

Start with that idea and see where the code takes you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM