SWT中是否有一种方法可以简单地获得等宽字体,适用于各种操作系统?
例如.这适用于Linux,但不适用于Windows:
Font mono = new Font(parent.getDisplay(), "Mono", 10, SWT.NONE);
或者我需要一个尝试加载不同字体(Consolas,Terminal,Monaco,Mono)的方法,直到一个不为空?或者,我可以在启动时在属性文件中指定它.
我尝试从Display获取系统字体,但这不是等宽的.
我需要在JTextPane中修改字母间距(字体跟踪),我无法让它工作.
当我使用JTextArea时,我可以这样做:
Font font = new Font("Courier New", Font.PLAIN, 10);
HashMap <TextAttribute, Object> attrs = new HashMap<TextAttribute, Object>();
attrs.put(TextAttribute.TRACKING, -0.1);
font = font.deriveFont(attrs);
textArea.setFont(font);
Run Code Online (Sandbox Code Playgroud)
但由于我需要更改行间距,我需要使用JTextPane,并执行:
textPane.setFont(font)
Run Code Online (Sandbox Code Playgroud)
正如我在JTextArea中所做的那样不起作用.我试过的另一件事是:
MutableAttributeSet set = new SimpleAttributeSet();
StyleConstants.setLineSpacing(set, -0.2);
StyleConstants.setFontFamily(set,"Courier New");
StyleConstants.setFontSize(set, 10);
set.addAttribute(TextAttribute.TRACKING, -0.1);
ta.setParagraphAttributes(set, true);
Run Code Online (Sandbox Code Playgroud)
但跟踪属性不起作用.
我究竟做错了什么?