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

后端 未结 6 1473
傲寒
傲寒 2020-12-30 12:21

I\'m referring to this Nimbus reference.

I tried to set global Font to be slightly larger:

UIManager.put(\"defaultFont\", new Font(Font.SANS_SERIF,         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-30 13:07

    This works with JDK6 and JDK7. Copy+paste and have fun ;)

    Note: for JDK6, change
    javax.swing.plaf.nimbus to
    com.​sun.​java.​swing.​plaf.​nimbus.

    Code

    import java.awt.*;
    import java.lang.reflect.*;
    import javax.swing.*;
    import javax.swing.plaf.nimbus.*;
    
    public class Main {
    
     public static void main(String[] args)
       throws InterruptedException, InvocationTargetException {
    
      SwingUtilities.invokeAndWait(new Runnable() {
    
       @Override
       public void run() {
        try {
         UIManager.setLookAndFeel(new NimbusLookAndFeel() {
    
          @Override
          public UIDefaults getDefaults() {
           UIDefaults ret = super.getDefaults();
           ret.put("defaultFont",
             new Font(Font.MONOSPACED, Font.BOLD, 16)); // supersize me
           return ret;
          }
    
         });
    
         new JFrame("Hello") {
    
          {
           setDefaultCloseOperation(EXIT_ON_CLOSE);
           setLayout(new FlowLayout(FlowLayout.LEFT));
    
           setSize(500, 500);
           setLocationRelativeTo(null);
    
           add(new JLabel("someLabel 1"));
           add(new JButton("someButton 1"));
           add(new JLabel("someLabel 2"));
           add(new JButton("someButton 2"));
    
           setVisible(true);
          }
    
         };     
        } catch (Exception ex) {
         throw new Error(ex);
        }
       }
    
      });
     }
    
    }
    

提交回复
热议问题