8

I was trying to change the font size of JLabel, I tried to set Font but it is always the same!

Here is some of the code:

 @Override
 public void paint(Graphics g) {
 super.paint(g);
 Container cont=this.getContentPane();
 intro=new JLabel("משחק זיכרון");
 intro.setForeground(Color.YELLOW);
 intro.setFont(intro.getFont().deriveFont(64.0f));
 intro.setHorizontalAlignment( SwingConstants.CENTER );
 cont.add(intro);
     }
  • This has already been answered: http://stackoverflow.com/questions/2715118/how-to-change-the-size-of-the-font-of-a-jlabel-to-take-the-maximum-size – Undefined Jul 26 '13 at 15:05
  • I saw it ! but still not useful all the methods they describe I use it !!! – fayez abd-alrzaq deab Jul 26 '13 at 15:07
  • It's not a good idea to change your widget's properties in the `paint` method. At this point in the code, you should onyl draw the object as it is configured. If you want to change the label's properties, you should do it somewhere else in your code. – Laf Jul 26 '13 at 15:39
  • OK ! I did something wrong in my code !!! I write twice setfont and I didn't notice that !!!!! so it works perfectly ! thank you all ! – fayez abd-alrzaq deab Jul 26 '13 at 19:57

2 Answers2

15

You are calling the wrong deriveFont method.

The parameter in deriveFont(int) is the style (bold, italic, etc.). The method you are looking for is deriveFont(float).

In your case, the only change you need to make is intro.setFont(intro.getFont().deriveFont(64.0f));.

Here's a short code example that does display a label with font size 64:

JFrame frame = new JFrame ("Test");
JLabel label = new JLabel ("Font Test");
label.setFont (label.getFont ().deriveFont (64.0f));
frame.getContentPane ().add (label);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.pack ();
frame.setVisible (true);
Laf
  • 7,965
  • 4
  • 37
  • 52
6

Don't confuse the deriveFont method which expects a style argument over the one that expects a font size. The one that you're using uses the style argument and has no bearing on the actual font size. Instead use

intro.setFont(intro.getFont().deriveFont(64f)); 

Also don't add components in the paint method. Your currrent application will not display the JLabel until a repaint is done. Overriding paint (or more correctly paintComponent for Swing) is intended for custom painting but the adding components does not qualify as such. The application will have the overhead of the component being added every time a repaint is done.

Example:

enter image description here

public class LabelDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Label Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                JLabel intro = new JLabel("משחק זיכרון");
                frame.add(intro);
                intro.setFont(intro.getFont().deriveFont(64f));
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276