I am learning programming with Java through a textbook. A programming exercise asks you to:
(Swing common features) Display a frame that contains six labels. Set th
I can provide you a third answer. Create a method that takes a JLabel and configures it. That way you could use either of the other two and reduce your code complexity.
The answer is, it depends. It depends on what it is you want to achieve.
If you are making lots of changes, repeatedly to the same serious of components, such as you are here, I might be tempted to use a simple List or array. This is particularly useful if the number of elements is dynamic and or only accessiable internally to the class.
If, on the other hand you want to change or update the state of the components externally, it's normally easier to provide named access to the components. Yes, you could provide a method that uses a int parameter, but then you start running into issues with always having to validate the parameter :P
So I guess, the question is, what is it you need to achieve?