Swing(Nimbus L& F)中Source Sans Pro的TTF和OTF版本不同, [英] TTF and OTF versions of Source Sans Pro are differently displayed in Swing (Nimbus L&F)

查看:142
本文介绍了Swing(Nimbus L& F)中Source Sans Pro的TTF和OTF版本不同,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ttf:



otf:< img src =https://i.stack.imgur.com/TBdKi.pngalt =otf>



都是相同的字体,但是不同的字体文件类型



我在Windows 7中运行Java 7根据Adobe的Miguel Sousa的说法,这个bug并不在字体
https://github.com/adobe / source-sans-pro / issues / 32#issuecomment-23319673



我只是将默认字体设置为新的字体。 TTF版本没有任何问题

  Font font_o = Font.createFont(Font.TRUETYPE_FONT,fonts.class.getResourceAsStream(fonts.class.getResourceAsStream /TTF/SourceSansPro-Regular.ttf)); 
// Font font_o = Font.createFont(Font.TRUETYPE_FONT,fonts.class.getResourceAsStream(fonts / OTF / SourceSansPro-Regular.otf));
Font font_n = font_o.deriveFont(Font.PLAIN,UIManager.getLookAndFeelDefaults()。getFont(defaultFont)。getSize());
UIManager.getLookAndFeelDefaults()。put(defaultFont,font_n);

为什么在 lineheight 中存在差异?

$

解决方案

结论(forums.oracle.com/thread/2573652相似于他的帖子那里,没什么特别的)

b
$ b


  • 不是真正的Java,也不是Nimbus的bug,在Font中这个bug,可以看到在Metal,Win,WinClassic和Nimbus Laf,OTF_Font在字形上方缺少2像素



。 Windows。



。金属。

。雨云



。经典。






在Java6上的WinXp



。与 。







  • 这段代码将帮助您找到两种字体之间的差异(并将所有差异报告给作者)

  • 从(java.awt.font。)TextAttribute返回所有可用的属性给具体的Font


  • ,你可以从几乎)所有TextAttributes




      java.awt.font.TextAttribute(family)
    java.awt.font.TextAttribute(权重)
    java.awt.font.TextAttribute(width)
    java.awt.font。 TextAttribute(状态)
    java.awt.font.TextAttribute(size)
    java.awt.font.TextAttribute(转换)
    java.awt.font.TextAttribute(上标)
    java.awt.font.TextAttribute(char_replacement)
    java.awt.font.TextAttribute(前台)
    java.a wt.font.TextAttribute(背景)
    java.awt.font.TextAttribute(下划线)
    java.awt.font.TextAttribute(删除线)
    java.awt.font.TextAttribute(run_direction)
    java.awt.font.TextAttribute(bidi_embedding)
    java.awt.font.TextAttribute(对齐)
    java.awt.font.TextAttribute(输入法高亮显示)
    java。 awt.font.TextAttribute(输入法下划线)
    java.awt.font.TextAttribute(swap_colors)
    java.awt.font.TextAttribute(numeric_shaping)
    java.awt.font.TextAttribute( kerning)
    java.awt.font.TextAttribute(连字)
    java.awt.font.TextAttribute(跟踪)

    代码Nimbus L& F

      import java.awt。*; 
    import java.awt.event。*;
    import java.io.IOException;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing。*;
    import javax.swing.plaf.FontUIResource;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;

    public class SystemFontDisplayer {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame(Nimbus UIDeafaults and Font);
    私人JComboBox fontsBox;
    private javax.swing.Timer timer = null;
    private JButton testButton = new JButton(testButton);
    private JTextField testTextField = new JTextField(testTextField,JLabel.CENTER);
    private JLabel testLabel = new JLabel(testLabel);
    私人字体font1,font2;
    private JMenuBar menuBar1 = new JMenuBar();
    private JMenu menu1 = new JMenu(Menu 1);
    private JMenu menu2 = new JMenu(Menu 2);
    private JMenuItem menuItem1 = new JMenuItem(MenuItem 1);
    private JMenuItem menuItem2 = new JMenuItem(MenuItem 2);
    $ b $ public SystemFontDisplayer(){
    try {
    font1 = Font.createFont(Font.TRUETYPE_FONT,SystemFontDisplayer.class.getResourceAsStream(/ Images / SourceSansPro-Regular.ttf) );
    font2 = Font.createFont(Font.TRUETYPE_FONT,SystemFontDisplayer.class.getResourceAsStream(/ Images / SourceSansPro-Regular.otf));
    catch(FontFormatException ex){
    Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE,null,ex);
    catch(IOException ex){
    Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE,null,ex);
    }
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    //ge.registerFont(font1);
    ge.registerFont(font2);
    String [] fontFamilyNames = ge.getAvailableFontFamilyNames(Locale.getDefault());
    fontsBox = new JComboBox(fontFamilyNames);
    fontsBox.setSelectedItem(0);
    fontsBox.setRenderer(new ComboRenderer());
    fontsBox.addItemListener(new ItemListener(){
    @Override
    public void itemStateChanged(ItemEvent e){
    if(e.getStateChange()== ItemEvent.SELECTED){
    final String fontName = fontsBox.getSelectedItem()。toString();
    fontsBox.setFont(new Font(fontName,Font.PLAIN,16));
    start();
    }
    }
    });
    fontsBox.setSelectedItem(0);
    fontsBox.getEditor()。selectAll();

    menu1.add(menuItem1);
    menuBar1.add(menu1);
    menu2.add(menuItem2);
    menuBar1.add(menu2);
    frame.setJMenuBar(menuBar1);
    frame.setLayout(new GridLayout(4,0,5,5));
    frame.add(fontsBox);
    frame.add(testButton);
    frame.add(testTextField);
    frame.add(testLabel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(200,105);
    frame.pack();
    public void run(){
    fontsBox.setPopupVisible(true);
    fontsBox。 setPopupVisible(false);
    }
    });
    frame.setVisible(true);

    $ b $ private void start(){
    timer = new javax.swing.Timer(250,updateCol());
    timer.setRepeats(false);
    timer.start();

    $ b $ public updateCol(){
    return new AbstractAction(text load action){
    private static final long serialVersionUID = 1L;
    $ b @Override
    public void actionPerformed(ActionEvent e){
    final Font fnt = new Font(fontsBox.getSelectedItem()。toString(),Font.PLAIN,16);
    尝试{
    LookAndFeel lnf = UIManager.getLookAndFeel()。getClass()。newInstance();
    final FontUIResource res = new FontUIResource(fnt);
    UIDefaults uiDefaults = lnf.getDefaults();
    uiDefaults.put(defaultFont,res);
    UIManager.getLookAndFeel()。uninitialize();
    UIManager.setLookAndFeel(lnf);
    } catch(InstantiationException ex){
    Logger.getLogger(SystemFontDisplayer.class.getName())。log(Level.SEVERE,null,ex);
    catch(IllegalAccessException ex){
    Logger.getLogger(SystemFontDisplayer.class.getName())。log(Level.SEVERE,null,ex);
    } catch(UnsupportedLookAndFeelException ex){
    Logger.getLogger(SystemFontDisplayer.class.getName())。log(Level.SEVERE,null,ex);
    }
    UIDefaults defaults = UIManager.getDefaults();
    final FontUIResource res = new FontUIResource(fnt);
    Object [] obj = res.getAvailableAttributes();
    for(Object objs:obj){
    System.out.println(objs); //返回java.awt.font.TextAttribute
    }
    defaults.put(defaultFont,res);
    SwingUtilities.updateComponentTreeUI(frame);
    frame.pack();
    }
    }; (UIManager.LookAndFeelInfo laf:UIManager.getInstalledLookAndFeels()){


    $ b public static void main(String arg []){
    try {
    $ b if(Nimbus.equals(laf.getName())){
    UIManager.setLookAndFeel(laf.getClassName());

    $ b} catch(Exception e){
    e.printStackTrace(); (){
    }
    java.awt.EventQueue.invokeLater(new Runnable(){
    @Override
    public void run(){
    SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
    }
    });


    private class ComboRenderer extends BasicComboBoxRenderer {

    private static final long serialVersionUID = 1L;

    @Override
    public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus){
    super.getListCellRendererComponent(list,value,index,isSelected, cellHasFocus);
    final Object fntObj = value;
    final String fontFamilyName =(String)fntObj;
    setFont(new Font(fontFamilyName,Font.PLAIN,16));
    返回这个;


    $ b $ / code $ / pre

    from Oldies L& Fs

      import java.awt。*; 
    import java.awt.event。*;
    import java.io.IOException;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing。*;
    import javax.swing.plaf.FontUIResource;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;

    public class SystemFontDisplayer extends JFrame {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame(Nimbus UIDeafaults and Font);
    私人JComboBox fontsBox;
    private javax.swing.Timer timer = null;
    private JButton testButton = new JButton(testButton);
    private JTextField testTextField = new JTextField(testTextField);
    private JLabel testLabel = new JLabel(testLabel);
    私人字体font1,font2;
    private JMenuBar menuBar1 = new JMenuBar();
    private JMenu menu1 = new JMenu(Menu 1);
    private JMenu menu2 = new JMenu(Menu 2);
    private JMenuItem menuItem1 = new JMenuItem(MenuItem 1);
    private JMenuItem menuItem2 = new JMenuItem(MenuItem 2);
    $ b $ public SystemFontDisplayer(){
    try {
    font1 = Font.createFont(Font.TRUETYPE_FONT,SystemFontDisplayer.class.getResourceAsStream(/ Images / SourceSansPro-Regular.ttf) );
    font2 = Font.createFont(Font.TRUETYPE_FONT,SystemFontDisplayer.class.getResourceAsStream(/ Images / SourceSansPro-Regular.otf));
    catch(FontFormatException ex){
    Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE,null,ex);
    catch(IOException ex){
    Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE,null,ex);
    }
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    //ge.registerFont(font1);
    ge.registerFont(font2);
    String [] fontFamilyNames = ge.getAvailableFontFamilyNames(Locale.getDefault());
    fontsBox = new JComboBox(fontFamilyNames);
    fontsBox.setSelectedItem(0);
    fontsBox.setRenderer(new ComboRenderer());
    fontsBox.addItemListener(new ItemListener(){
    @Override
    public void itemStateChanged(ItemEvent e){
    if(e.getStateChange()== ItemEvent.SELECTED){
    final String fontName = fontsBox.getSelectedItem()。toString();
    fontsBox.setFont(new Font(fontName,Font.PLAIN,16));
    start();
    }
    }
    });
    fontsBox.setSelectedItem(0);
    fontsBox.getEditor()。selectAll();
    menu1.add(menuItem1);
    menuBar1.add(menu1);
    menu2.add(menuItem2);
    menuBar1.add(menu2);
    frame.setJMenuBar(menuBar1);
    frame.setLayout(new GridLayout(4,0,20,20));
    frame.add(fontsBox);
    frame.add(testButton);
    frame.add(testTextField);
    frame.add(testLabel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(200,105);
    frame.pack();
    public void run(){
    fontsBox.setPopupVisible(true);
    fontsBox。 setPopupVisible(false);
    }
    });
    frame.setVisible(true);

    $ b $ private void start(){
    timer = new javax.swing.Timer(750,updateCol());
    timer.setRepeats(false);
    timer.start();

    $ b $ public updateCol(){
    return new AbstractAction(text load action){
    private static final long serialVersionUID = 1L;
    $ b @Override
    public void actionPerformed(ActionEvent e){
    final Font fnt = new Font(fontsBox.getSelectedItem()。toString(),Font.PLAIN,12);
    final FontUIResource res = new FontUIResource(fnt);
    UIManager.getLookAndFeelDefaults()。put(Button.font,res);
    UIManager.getLookAndFeelDefaults()。put(TextField.font,res);
    UIManager.getLookAndFeelDefaults()。put(Label.font,res);
    SwingUtilities.updateComponentTreeUI(frame);
    frame.pack();
    }
    };

    $ b $ public static void main(String arg []){
    / * try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch(Exception e){
    e.printStackTrace();
    } * /
    java.awt.EventQueue.invokeLater(new Runnable(){
    @Override
    public void run(){
    SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer );
    }
    });


    private class ComboRenderer extends BasicComboBoxRenderer {

    private static final long serialVersionUID = 1L;

    @Override
    public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus){
    super.getListCellRendererComponent(list,value,index,isSelected, cellHasFocus);
    final Object fntObj = value;
    final String fontFamilyName =(String)fntObj;
    setFont(new Font(fontFamilyName,Font.PLAIN,16));
    返回这个;





    $ hr









    ttf:

    otf:

    both are the same font but different fontfile types

    I am running Java 7 in Windows 7

    According to Miguel Sousa by Adobe the bug is not in the fonts https://github.com/adobe/source-sans-pro/issues/32#issuecomment-23319673

    I am just setting the default font to the new font. The TTF version works without any issues

    Font font_o = Font.createFont(Font.TRUETYPE_FONT, fonts.class.getResourceAsStream("fonts/TTF/SourceSansPro-Regular.ttf"));
    //Font font_o = Font.createFont(Font.TRUETYPE_FONT, fonts.class.getResourceAsStream("fonts/OTF/SourceSansPro-Regular.otf"));
    Font font_n=font_o.deriveFont(Font.PLAIN, UIManager.getLookAndFeelDefaults().getFont("defaultFont").getSize());
    UIManager.getLookAndFeelDefaults().put("defaultFont",font_n);
    

    Why is there a difference in the lineheight?

    解决方案

    conclusion (forums.oracle.com/thread/2573652 similair as many his post there, nothing special)


    • not true that Java, nor Nimbus bug, this bug in Font, you can to see that in Metal, Win, WinClassic and Nimbus Laf, OTF_Font missing 2pixels above glyph

    . Windows .

    . Metal .

    . Nimbus .

    . Classic .


    WinXp on Java6

    . versus .


    • this code will help you to find difference(ies) between two fonts (and to report all diff's to author)

    • returns all available atributes from (java.awt.font.)TextAttribute for concrete Font

    • you can to get detailed properties from (almost) all TextAttributes

    with output

    java.awt.font.TextAttribute(family)
    java.awt.font.TextAttribute(weight)
    java.awt.font.TextAttribute(width)
    java.awt.font.TextAttribute(posture)
    java.awt.font.TextAttribute(size)
    java.awt.font.TextAttribute(transform)
    java.awt.font.TextAttribute(superscript)
    java.awt.font.TextAttribute(char_replacement)
    java.awt.font.TextAttribute(foreground)
    java.awt.font.TextAttribute(background)
    java.awt.font.TextAttribute(underline)
    java.awt.font.TextAttribute(strikethrough)
    java.awt.font.TextAttribute(run_direction)
    java.awt.font.TextAttribute(bidi_embedding)
    java.awt.font.TextAttribute(justification)
    java.awt.font.TextAttribute(input method highlight)
    java.awt.font.TextAttribute(input method underline)
    java.awt.font.TextAttribute(swap_colors)
    java.awt.font.TextAttribute(numeric_shaping)
    java.awt.font.TextAttribute(kerning)
    java.awt.font.TextAttribute(ligatures)
    java.awt.font.TextAttribute(tracking)
    

    from code Nimbus L&F

    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import javax.swing.plaf.FontUIResource;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    
    public class SystemFontDisplayer {
    
        private static final long serialVersionUID = 1L;
        private JFrame frame = new JFrame("Nimbus UIDeafaults and Font");
        private JComboBox fontsBox;
        private javax.swing.Timer timer = null;
        private JButton testButton = new JButton("testButton");
        private JTextField testTextField = new JTextField("testTextField", JLabel.CENTER);
        private JLabel testLabel = new JLabel("testLabel");
        private Font font1, font2;
        private JMenuBar menuBar1 = new JMenuBar();
        private JMenu menu1= new JMenu("Menu 1");
        private JMenu menu2= new JMenu("Menu 2");
        private JMenuItem menuItem1= new JMenuItem("MenuItem 1");
        private JMenuItem menuItem2= new JMenuItem("MenuItem 2");
    
        public SystemFontDisplayer() {
            try {
                font1 = Font.createFont(Font.TRUETYPE_FONT, SystemFontDisplayer.class.getResourceAsStream("/Images/SourceSansPro-Regular.ttf"));
                font2 = Font.createFont(Font.TRUETYPE_FONT, SystemFontDisplayer.class.getResourceAsStream("/Images/SourceSansPro-Regular.otf"));
            } catch (FontFormatException ex) {
                Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
            }
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            //ge.registerFont(font1);
            ge.registerFont(font2);
            String[] fontFamilyNames = ge.getAvailableFontFamilyNames(Locale.getDefault());
            fontsBox = new JComboBox(fontFamilyNames);
            fontsBox.setSelectedItem(0);
            fontsBox.setRenderer(new ComboRenderer());
            fontsBox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        final String fontName = fontsBox.getSelectedItem().toString();
                        fontsBox.setFont(new Font(fontName, Font.PLAIN, 16));
                        start();
                    }
                }
            });
            fontsBox.setSelectedItem(0);
            fontsBox.getEditor().selectAll();
    
            menu1.add(menuItem1);
            menuBar1.add(menu1);
            menu2.add(menuItem2);
            menuBar1.add(menu2);
            frame.setJMenuBar(menuBar1);
            frame.setLayout(new GridLayout(4, 0, 5, 5));
            frame.add(fontsBox);
            frame.add(testButton);
            frame.add(testTextField);
            frame.add(testLabel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocation(200, 105);
            frame.pack();
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    fontsBox.setPopupVisible(true);
                    fontsBox.setPopupVisible(false);
                }
            });
            frame.setVisible(true);
        }
    
        private void start() {
            timer = new javax.swing.Timer(250, updateCol());
            timer.setRepeats(false);
            timer.start();
        }
    
        public Action updateCol() {
            return new AbstractAction("text load action") {
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    final Font fnt = new Font(fontsBox.getSelectedItem().toString(), Font.PLAIN, 16);
                    try {
                        LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
                        final FontUIResource res = new FontUIResource(fnt);
                        UIDefaults uiDefaults = lnf.getDefaults();
                        uiDefaults.put("defaultFont", res);
                        UIManager.getLookAndFeel().uninitialize();
                        UIManager.setLookAndFeel(lnf);
                    } catch (InstantiationException ex) {
                        Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalAccessException ex) {
                        Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (UnsupportedLookAndFeelException ex) {
                        Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    UIDefaults defaults = UIManager.getDefaults();
                    final FontUIResource res = new FontUIResource(fnt);
                    Object[] obj = res.getAvailableAttributes();
                    for (Object objs : obj) {
                        System.out.println(objs); //returns java.awt.font.TextAttribute
                    }
                    defaults.put("defaultFont", res);
                    SwingUtilities.updateComponentTreeUI(frame);
                    frame.pack();
                }
            };
        }
    
        public static void main(String arg[]) {
            try {
                for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(laf.getName())) {
                        UIManager.setLookAndFeel(laf.getClassName());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
                }
            });
        }
    
        private class ComboRenderer extends BasicComboBoxRenderer {
    
            private static final long serialVersionUID = 1L;
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                final Object fntObj = value;
                final String fontFamilyName = (String) fntObj;
                setFont(new Font(fontFamilyName, Font.PLAIN, 16));
                return this;
            }
        }
    }
    

    from code Oldies L&Fs

    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import javax.swing.plaf.FontUIResource;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    
    public class SystemFontDisplayer extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private JFrame frame = new JFrame("Nimbus UIDeafaults and Font");
        private JComboBox fontsBox;
        private javax.swing.Timer timer = null;
        private JButton testButton = new JButton("testButton");
        private JTextField testTextField = new JTextField("testTextField");
        private JLabel testLabel = new JLabel("testLabel");
        private Font font1, font2;
        private JMenuBar menuBar1 = new JMenuBar();
        private JMenu menu1 = new JMenu("Menu 1");
        private JMenu menu2 = new JMenu("Menu 2");
        private JMenuItem menuItem1 = new JMenuItem("MenuItem 1");
        private JMenuItem menuItem2 = new JMenuItem("MenuItem 2");
    
        public SystemFontDisplayer() {
            try {
                font1 = Font.createFont(Font.TRUETYPE_FONT, SystemFontDisplayer.class.getResourceAsStream("/Images/SourceSansPro-Regular.ttf"));
                font2 = Font.createFont(Font.TRUETYPE_FONT, SystemFontDisplayer.class.getResourceAsStream("/Images/SourceSansPro-Regular.otf"));
            } catch (FontFormatException ex) {
                Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(SystemFontDisplayer.class.getName()).log(Level.SEVERE, null, ex);
            }
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            //ge.registerFont(font1);
            ge.registerFont(font2);
            String[] fontFamilyNames = ge.getAvailableFontFamilyNames(Locale.getDefault());
            fontsBox = new JComboBox(fontFamilyNames);
            fontsBox.setSelectedItem(0);
            fontsBox.setRenderer(new ComboRenderer());
            fontsBox.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        final String fontName = fontsBox.getSelectedItem().toString();
                        fontsBox.setFont(new Font(fontName, Font.PLAIN, 16));
                        start();
                    }
                }
            });
            fontsBox.setSelectedItem(0);
            fontsBox.getEditor().selectAll();
            menu1.add(menuItem1);
            menuBar1.add(menu1);
            menu2.add(menuItem2);
            menuBar1.add(menu2);
            frame.setJMenuBar(menuBar1);
            frame.setLayout(new GridLayout(4, 0, 20, 20));
            frame.add(fontsBox);
            frame.add(testButton);
            frame.add(testTextField);
            frame.add(testLabel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocation(200, 105);
            frame.pack();
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    fontsBox.setPopupVisible(true);
                    fontsBox.setPopupVisible(false);
                }
            });
            frame.setVisible(true);
        }
    
        private void start() {
            timer = new javax.swing.Timer(750, updateCol());
            timer.setRepeats(false);
            timer.start();
        }
    
        public Action updateCol() {
            return new AbstractAction("text load action") {
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    final Font fnt = new Font(fontsBox.getSelectedItem().toString(), Font.PLAIN, 12);
                    final FontUIResource res = new FontUIResource(fnt);
                    UIManager.getLookAndFeelDefaults().put("Button.font", res);
                    UIManager.getLookAndFeelDefaults().put("TextField.font", res);
                    UIManager.getLookAndFeelDefaults().put("Label.font", res);
                    SwingUtilities.updateComponentTreeUI(frame);
                    frame.pack();
                }
            };
        }
    
        public static void main(String arg[]) {
            /*try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }*/
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer();
                }
            });
        }
    
        private class ComboRenderer extends BasicComboBoxRenderer {
    
            private static final long serialVersionUID = 1L;
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                final Object fntObj = value;
                final String fontFamilyName = (String) fntObj;
                setFont(new Font(fontFamilyName, Font.PLAIN, 16));
                return this;
            }
        }
    }
    


    这篇关于Swing(Nimbus L&amp; F)中Source Sans Pro的TTF和OTF版本不同,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆