0

I have manually set the font of jlabel in swing,but when i run it then the font changes to default.So i searched on stackOverflow and modified my code accordinly but nothing changed.Here is my code:

import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.UIManager;


public class Alarms extends javax.swing.JFrame {

public Alarms() {
    initComponents();
    this.setLocation(200, 400);
    new Thread() {
        public void run() {
            while (true) {
                Calendar cal = new GregorianCalendar();
                int hour = cal.get(Calendar.HOUR);
                int min = cal.get(Calendar.MINUTE);
                int sec = cal.get(Calendar.SECOND);
                int am_pm = cal.get(Calendar.AM_PM);
                String day;
                if (am_pm == 1) {
                    day = "P.M";
                } else {
                    day = "A.M";
                }
                String text = hour + " : " + min + " : " + sec + " " + day;
                Clock.setText(text);
            }
        }
    }.start();
}

@SuppressWarnings("unchecked")
    private void initComponents() {

    Clock = new java.awt.Label();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setAutoRequestFocus(false);
    setBackground(new java.awt.Color(0, 0, 0));
    setFocusableWindowState(false);
    setFont(new java.awt.Font("DS-Digital", 1, 64)); // NOI18N
    setForeground(new java.awt.Color(51, 51, 55));
    setUndecorated(true);
    setPreferredSize(new java.awt.Dimension(400, 250));
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    Clock.setAlignment(java.awt.Label.CENTER);
    Clock.setFont(new java.awt.Font("DS-Digital", 1, 48)); // NOI18N
    Clock.setForeground(new java.awt.Color(0, 165, 255));
    Clock.setText("7:45:30 P.M");
    getContentPane().add(Clock, new org.netbeans.lib.awtextra.AbsoluteConstraints(8, 50, 380, 150));

    pack();
}                       

public static void main(String args[]) {

    UIManager.put("Label.font", "DS-DIGITAL");
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                UIManager.put("Label.font", "DS-DIGITAL");
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Alarms.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Alarms.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Alarms.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Alarms.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Alarms().setVisible(true);

        }
    });
}                   
private java.awt.Label Clock;                  

} This is how it should have looked like when i run it.But the font changes when i actually run the program.enter image description here

Can you tell me what mistake am i doing?

ani
  • 181
  • 1
  • 2
  • 14

1 Answers1

0

I have found this answer (the link points you to the answer, not the question)

Java: Altering UI fonts (Nimbus) doesn't work!

Briefly, it suggests to set Nimbus LAF first and then getDefaults() to set the font you want. Also, try to wrap your font with new FontUIResource(font).

WesternGun
  • 11,303
  • 6
  • 88
  • 157