Originariamente inviato da JunkyFunki
su windowlistener non mi sembra ci sia il metodo opportuno
Infatti non devi usare WindowListener ma ComponentListener (JFrame è un Component) che ha il metodo componentResized().
Ecco un esempio abbastanza banale (e forse stupido). Ho stabilito che la dimensione del font è il 5% dell'altezza della finestra.
codice:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFrame extends JFrame
{
private JButton b1, b2, b3, b4, b5;
public TestFrame ()
{
super ("Test Frame");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setSize (300, 300);
b1 = new JButton ("Uno");
b2 = new JButton ("Due");
b3 = new JButton ("Tre");
b4 = new JButton ("Quattro");
b5 = new JButton ("Cinque");
add (b1, BorderLayout.NORTH);
add (b2, BorderLayout.EAST);
add (b3, BorderLayout.SOUTH);
add (b4, BorderLayout.WEST);
add (b5, BorderLayout.CENTER);
addComponentListener (new ComponentAdapter ()
{
public void componentResized (ComponentEvent e)
{
adjustFont (b1, 5.0f);
adjustFont (b2, 5.0f);
adjustFont (b3, 5.0f);
adjustFont (b4, 5.0f);
adjustFont (b5, 5.0f);
}
});
}
private void adjustFont (Component c, float percentual)
{
float fontSize = getHeight () * percentual / 100.0f;
Font f = c.getFont ();
f = f.deriveFont (fontSize);
c.setFont (f);
}
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
TestFrame f = new TestFrame ();
f.setVisible (true);
}
});
}
}