yoonsanha :
I am creating a quiz program using swing components. I want to make a code that I will declare all the designs like background color and I will use it to all the frames I created so my code will be simpler and shorter.
I already tried to declare one by one.
contentPane1.setBackground(Color.PINK);
contentPane2.setBackground(Color.PINK);
contentPane3.setBackground(Color.PINK);
contentPane4.setBackground(Color.PINK);
contentPane5.setBackground(Color.PINK);
I have to create 10 frames, and with that kind of code it will be very long. I don't know how to do it I am just a beginner. Thank you :)
Elliott Frisch :
You could use a Stream
of your content pane(s) and invoke setBackground
with a forEach
like;
Stream.of(contentPane1, contentPane2, contentPane3, contentPane4, contentPane5)
.forEach(p -> p.setBackground(Color.PINK));
It might be even better (for ten) to use an array; perhaps like
JPanel[] panels = new JPanel[] { contentPane1, contentPane2, contentPane3,
contentPane4, contentPane5, contentPane6, contentPane7,
contentPane8, contentPane9, contentPane10
};
Arrays.stream(panels).forEach(p -> p.setBackground(Color.PINK));