Changing default JLabel font

前端 未结 2 1789
猫巷女王i
猫巷女王i 2021-01-05 16:42

How would I go about setting the default font for all JLabel instances. Instead of setting the font for each JLabel independently.

2条回答
  •  隐瞒了意图╮
    2021-01-05 17:09

    Is this what you are looking for?

    import java.awt.FlowLayout;
    import java.awt.Font;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.UIManager;
    
    public class LabelFont {
    
       public static void main(String[] args) {
          Font oldLabelFont = UIManager.getFont("Label.font");
          UIManager.put("Label.font", oldLabelFont.deriveFont(Font.PLAIN));
    
          JFrame f = new JFrame("LabelFont Test");
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.getContentPane().setLayout(new FlowLayout());
    
          JLabel df = new JLabel("Default JLabel font");
          f.getContentPane().add(df);
    
          JLabel ef = new JLabel("Font explicitly set");
          ef.setFont(oldLabelFont);
          f.getContentPane().add(ef);
    
          f.pack();
          f.setVisible(true);
       }
    }
    

提交回复
热议问题