I want to set a specific font for all component in a JPanel but I prefer that the question is still more general and not limited to the JPanel. How can I set the font to a list of component in a container (JFrame or JPanel)?
解决方案
Here is a simple method that allows you to specify Font to the whole components tree under any Container (or just a simple Component, doesn't matter):
public static void changeFont ( Component component, Font font )
{
component.setFont ( font );
if ( component instanceof Container )
{
for ( Component child : ( ( Container ) component ).getComponents () )
{
changeFont ( child, font );
}
}
}
Simply pass your panel and specific Font into this method and you will get all childs refactored aswell.